• No results found

Variables and Assignment

In document C++ (Page 34-37)

3.2

Variables and Assignment

In algebra, variables are used to represent numbers. The same is true in C++, except C++variables also can represent values other than numbers. Listing 3.4 (variable.cpp) uses a variable to store an integer value and then prints the value of the variable.

Listing 3.4: variable.cpp 1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int x; 8 x = 10; 9 cout << x << endl; 10 }

The main function in Listing 3.4 (variable.cpp) contains three statements: • int x;

This is a declaration statement. All variables in a C++ program must be declared. A declaration specifies the type of a variable. The word intindicates that the variable is an integer. The name of the integer variable is x. We say that variable x has typeint. C++ supports types other than integers, and some types require more or less space in the computer’s memory. The compiler uses the declaration to reserve the proper amount a memory to store the variable’s value. Due to the declaration the compiler also can check to see if the variable is being used properly; for example, we will see that integers can be added together just like in mathematics. For some other data types, however, addition is not possible and so is not allowed. The compiler can ensure that a variable involved in an addition is compatible with addition. It can report an error if it is not.

The compiler will issue an error if a programmer attempts to use an undeclared variable. The com- piler cannot deduce the storage requirements and cannot verify the variable’s proper usage if it not declared. Once declared, a particular variable cannot be redeclared in the same context. A variable may not change its type during its lifetime.

• x = 10;

This is an assignment statement. An assignment statement associates a value with a variable. The key to an assignment statement is the symbol = which is known as the assignment operator. Here the value 10 is being assigned to the variable x. This means the value 10 will be stored in the memory location the compiler has reserved for the variable named x. We need not be concerned about where the variable is stored in memory; the compiler takes care of that detail.

After it has been declared, a variable may be assigned and reassigned as often as necessary. • cout << x << endl;

3.2. VARIABLES AND ASSIGNMENT 24

Note that the lack of quotation marks here is very important. If x has the value 10, the statement

cout << x << endl;

prints 10, the value of the variable x, but the statement

cout << "x" << endl;

prints x, the message containing the single letter x.

The meaning of the assignment operator (=) is different from equality in mathematics. In mathematics,

=asserts that the expression on its left is equal to the expression on its right. In C++, = makes the variable on its left take on the value of the expression on its right. It is best to read x = 5 as “x is assigned the value 5,” or “x gets the value 5.” This distinction is important since in mathematics equality is symmetric: if x = 5, we know 5 = x. In C++, this symmetry does not exist; the statement

5 = x;

attempts to reassign the value of the literal integer value 5, but this cannot be done because 5 is always 5 and cannot be changed. Such a statement will produce a compiler error:

error C2106: ’=’ : left operand must be l-value

Variables can be reassigned different values as needed, as Listing 3.5 (multipleassignment.cpp) shows.

Listing 3.5: multipleassignment.cpp 1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int x; 8 x = 10; 9 cout << x << endl; 10 x = 20; 11 cout << x << endl; 12 x = 30; 13 cout << x << endl; 14 }

Observe the each print statement in Listing 3.5 (multipleassignment.cpp) is identical, but when the program runs the print statements produce different results.

A variable may be given a value at the time of its declaration; for example, Listing 3.6 (variable-init.cpp) is a variation of Listing 3.4 (variable.cpp).

Listing 3.6: variable-init.cpp

1 #include <iostream> 2

3.2. VARIABLES AND ASSIGNMENT 25 4 5 int main() 6 { 7 int x = 10; 8 cout << x << endl; 9 }

Notice that in Listing 3.6 (variable-init.cpp) the declaration and assignment of the variable x is performed in one statement instead of two. This combined declaration and immediate assignment is called initialization.

C++supports another syntax for initializing variables as shown in Listing 3.7 (alt-variable-init.cpp).

Listing 3.7: alt-variable-init.cpp 1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int x(10); 8 cout << x << endl; 9 }

This alternate form is not commonly used for simple variables, but it necessary for initializing more com- plicated kinds of variables called objects. Objects are introduced in Chapter 13 and Chapter 14.

Multiple variables of the same type can be declared and, if desired, initialized in a single statement. The following statements declare three variables in one declaration statement:

int x, y, z;

The following statement declares three integer variables and initializes two of them: int x = 0, y, z = 5;

Here y’s value is undefined.

The compiler maps a variable to a location in the computer’s memory. We can visualize a variable and its corresponding memory location as a box as shown in Figure 3.1.

5

a

Figure 3.1: Representing a variable and its memory location as a box

We name the box with the variable’s name. Figure 3.2 shows how the following sequence of C++code affects memory.

3.3. IDENTIFIERS 26

In document C++ (Page 34-37)