C++ chapter 1
Chapter 1
Manual b'asico
In this Chapter we introduce the language C ++ program- ming by example. Each example is accompanied by a brief explicaci'on to hacer'enfasi in the new commands in the program. These explanations are intended to be simply'utiles, leaving band any consideration of completeness or thoroughness.
Anyone truly interested in learning to program should complete these explanations with two things: a good book for the C ++ language, and many hours of playing pr'actica and by modifying the sample programs or writing new programs.
1.1. Input, output and variables
Our first program:
hola.cc program
#include using namespace std;
int main () {
} Cout << "Hello world!" << Endl;
Departure
Hello World !
S'olo one of the program l'ıneas interests us now: that begins with cout. (M'as will explain the l'ıneas dem'as porqu'e are necessary.) The cout command is used to write things on the screen. Another example:
calculos1.cc program
#include using namespace std;
int main () {
cout << "Some calculations:" << endl; cout << "2 + 8 * (3 + 1) =";
cout << "2 + 8 *" << << 3 + 1 "="; cout << "2+" << 8 * (3 + 1) << "="; } Cout << 2 + 8 * (3 + 1) << endl;
1
Some calculations: 2 + 8 * (3 + 1) = 2 + 8 * 4 = 2 + 32 = 34
All instructions of our programs end with a semi-colon (;). We can write several things using un'unico cout, provided the conveniently split up by s'ımbolos. We can write text strings (strings calls tambi'en) surrounded by quotation marks (), which appear on screen literally; expressions, like 8 * (3 + 1), which are calculated before appearing on the screen; and endl, which means jump-de-l'ınea.
calculos2.cc program
#include using namespace std;
int main () {int x = 317;
cout << "The number x is" << x << "." << Endl; cout << "Your square and cube are" << x * x << "and"
<< X * x * x << "respectively." << Endl;
cout << "The polynomial 25x + 12x ^ 3 ^ + 2 evaluated 2-8X 'x' da" << 25 * x * x * x + 12 * x * x-8 * x + 2 << endl;
}
Departure
The number x is 317.
Its square and cube are 100,489 and 31,855,013 respectively. 25x polynomial x ^ 3 + 12 + 2 ^ 2-8X evaluated 'x' da 797,578,659
We can break l'ıneas and add spaces in our program to make it easier to read M'as, provided we indicate the final of the instructions with the s'ımbolo;. Spaces another are imported into text strings, such as "i".
In this program, x is a variable. A variable is a piece of computer memory that stores data. The l'ınea (int x = 317;) calls for a chunk of memory, called from now x, with sufficient capacity to store n'umeros integers (int, the English integer), and initially containing the n reserves 'umero 317. from this point on, whenever x appears in the program, the corresponding piece of memory to know qu'e contains integer x is consulted.
magia.cc program
#include using namespace std;
int main () {
cout << "Pick a number 'n'." << Endl; int n = 74;
cout << "[choose the" << n << "]" << endl;
cout << "Fold." << Endl; n = 2 * n;
cout << "[da me" << n << "]" << endl;
cout << "Sumale 6." << Endl; n = n + 6;
cout << "[I get" << n << "]" << endl;
cout << "divide by 2 and subtract 3." << Endl;
n = n / 2-3;
} Cout << "[surprise! I get the initial number," << n << "]." << Endl;
Departure
Pick a number 'n'.
[Choose 74] bend it.
[Gives me 148] Sumale 6.
[Get 154]
Divide by 2 and subtract 3.
[Surprise! I get the initial number, 74].
The first time a variable appears as n in the previous program, it is necessary to declare, as is done in lal'ınea int n = 74 ;. Declaring a variable means cu'al choose your type (in this case, int, ie integer) and name, to refer to it later you M'as.
The piece stored in the memory of a variable value can canviar during program execution. At first the n variable is 74, but despu'es is 2 times what it was worth n (ie, 2 · 74 = 148), despu'es worth $ 6 M'as what val'ıa n (6 + 148 =
154), and half of n por'ultimo least 3 (154/2 - 3 = 74).
edad.cc program
#include #include
using namespace std;
int main () {
cout << "Hello, what's your name?" << Endl; string name; cin >> name;
cout << "Hello," << name << ". When you were born?" << Endl; int nac; cin >> nac;
cout << "Your age If we are in 2007, is" -1 << <<-nac 2007 "or" 2007-nac << << "." << Endl;
}
Output (interactive)
Hello what's your name ? Pepito
Hello, Pepito. When you were born ? 1997
If we are in 2007, your age is 9 or 10.
In this program we have two variables, name and nac. String type variables store text strings. Notice that at the very beginning we have added a new l'ınea, #include . Whenever we use variables of type string is convenient to add this l'ınea. The #include is used to indicate that we want to use, among other things, the cout or cin (in our case, we will use forever). The l'ınea using namespace std; tambi'en should always be written.
In the example are defined to initialize variables without ning'un value, because leer'an to continuaci'on with cin. The command works cin cout like manner except that we use >> instead of << as cin puts data in a variable, rather than remove them.