While C++ is a language, which fully supports object–oriented programming (and we will be making extensive use of this in subsequent chapters), algorithms still need to be implemented using iteration, conditional statements, and the like. These basic features of the language will be introduced in this section.
1.2.1 Built-in data types and the definition and declaration of variables
In C++, the definition of a variable associates a name with a type and allocates space in machine memory for the variable. In contrast, a declaration only associates a name with a type; the requisite space for the variable is assumed to have been allocated elsewhere. Each variable must be defined exactly once, while its declaration may (need to) be repeated. In most cases, declaration and definition are identical, and we will use the two roughly synonymously in the sequel.4
The following fundamental data types are available in C++:
Integers, declared as int.
Floating point numbers, declared as float or double (these days, most programs use double).
Booleans (variables which can hold the value true or false), declared as bool.
Characters, declared as char.
Integers can also be declared using the additional modifiers unsigned (for non-negative integer variables) and long or short. Variables declared as long or short may represent a greater or smaller range of possible integers, but the actual implementation is machine and/or compiler dependent (See Stroustrup (1997)).
Within a given scope, all variables used in a C++ program must be declared exactly once. Variables can be declared anywhere within a program, but the scope of a declaration is limited by the brackets { }. The outermost scope is given by a source file itself “ variables declared outside any { } block are called global. Variables are assigned values using the operator =. Listing 1.2 gives examples of variable declarations.
C++ also allows the declaration of pointers and references to variables. A pointer is the machine memory address holding a variable of a particular type, and a reference refers to contents of a particular block of machine memory. The statement
doublefe x;
declares x to be a reference to a variable of type double. Thus the following statements
double y = 0.0; double& x = y; x = 1.0; cout ≪ y ≪ endl;
will print 1, whereas omitting the ampersand & in the declaration of x would result in 0 being displayed. The statement
double* p;
declares p to be a pointer variable to hold the address of a double. The ampersand operator can be used to determine the pointer to an existing variable. Conversely, a pointer is de-referenced (i.e. the contents of the memory to which it points is accessed) using the operator *. Thus
double y = 0.0; double* x = &y; *x = 1.0; cout ≪ y ≪ endl;
will again print 1.
Arrays of any data type can be created using square brackets. Thus
int integer_array[5];
creates an array of five integers. Array elements can be accessed using the operator [ ], e.g.
int integer_array[5];
Note that the base index of such arrays is zero, i.e. the first element of the above array is accessed by integer_array[0]. The variable integer_array itself is a pointer to the first element in the array; thus
int integer_array[5]; int* p = integer_array; p[0] = 105; cout ≪ integer_array[0] ≪ endl;
will display 105. Note also that the size of the array must be a constant known at compile time. Arrays, the size of which is only known at run time, must be dynamically allocated using the new operator (see Section 1.2.5). However, the object–oriented features of C++ allow such dynamic memory allocation to be encapsulated in utility classes,5 e.g. for vectors and matrices, obviating the need to worry about these issues when writing higher level programs. Constants can be declared using the qualifier const , e.g.
const int one = 1;
They must be initialised (assigned a value) at the time of declaration and cannot be modified. Additionally, sets of constant integer values can be defined as so–called enumerations using enum , e.g.
enum currency {USD, EUR, JPY, AUD}; defines a new type currency, which may take the values USD , EUR , JPY or AUD (defined by default to be represented as integers 0, 1, 2, 36). We can then declare a variable domestic to be of type currency and initi...