1.8. Getting Started with Data

C++ considers data to be the focal point of the problem-solving process. We stated above that C++ supports the object-oriented programming paradigm. In C++, as well as in any other object-oriented programming language, we define a class to be a description of what the data looks like (the state) and what the data can do (the behavior). Classes are analogous to abstract data types because a user of a class only sees the state and behavior of a data item. Data items are called objects in the object-oriented paradigm. An object is an instance of a class.

1.9. Built-in Atomic Data Types

C++ requires users to specify the data type of each variable before it is used. The primary C++ built-in atomic data types are: integer (int), floating point (float), double precision floating point (double), Boolean (bool), and character (char). There is also a special type which holds a memory location called pointer. C++ also has collection or compound data types, which will be discussed in a future chapter.

1.9.1. Numeric Data

Numeric C++ data types include int for integer, float for floating point, double for double precision floating point.

The standard arithmetic operations, +, -, *, and / are used with optional parentheses to force the order of operations away from normal operator precedence.

When two integers are divided in C++, the integer portion of the quotient is returned and the fractional portion is removed. i.e. When two integers are divided, integer division is used. To get the whole quotient, declaring one of the numbers as a float will convert the entire result into floating point.

Exponentiation in C++ is done using pow() from the cmath library and the remainder (modulo) operator is done with %.

Run the following code to see that you understand each result.

When declaring numeric variables in C++, modifiers like short, long, and unsigned can optionally be used to help to ensure space is used as efficiently as possible.

1.9.2. Boolean Data

Boolean data types are named after George Boole who was an English mathematician, so the word “Boolean” should be capitalized. However, the Boolean data type, in C++ uses the keyword bool which is not capitalized. The possible state values for a C++ Boolean are lower case true and false.

C++ uses the standard Boolean operators, “and” is given by && , “or” is given by ||, and “not” is given by !. Note that the internally stored values representing true and false are actually 1 and 0 respectively. Hence, we see this in output as well.

Boolean data objects are also used as results for comparison operators such as equality (==) and greater than (\(>\)). In addition, relational operators and logical operators can be combined together to form complex logical questions. Table 1 shows the relational and logical operators with examples shown in the session that follows.

Table 1: C++ Relational and Logical Operators

Operation Name

Operator

Explanation

less than

\(<\)

Less than operator

greater than

\(>\)

Greater than operator

less than or equal

\(<=\)

Less than or equal to operator

greater than or equal

\(>=\)

Greater than or equal to operator

equal

\(==\)

Equality operator

not equal

\(!=\)

Not equal operator

logical and

\(&&\)

Both operands true for result to be true

logical or

\(||\)

One or the other operand is true for the result to be true

logical not

\(!\)

Negates the truth value, false becomes true, true becomes false

When a C++ variable is declared space in memory is set aside to hold this type of value. A C++ variable can optionally be initialized in the declaration by using a combination of a declaration and an assignment statement.

Consider the following session:

The declaration int theSum = 0; creates a variable called theSum and initializes it to hold the data value of 0. The right-hand side of each assignment statement is evaluated and the resulting data value is “assigned” to the variable named on the left-hand side. Here the type of the variable is integer.

In C++, the data type cannot change. This is a characteristic of C++’s static typing. A variable can hold ever only one type of data. Pitfall: C++ will often simply try to do the assignment you requested without complaining. Note what happened in the code above in the final output.

1.9.3. Character Data

In C++ single quotes are used for the character (char) data type, and double quotes are used for the string data type.

Consider the following code.

Try the following question.

1.9.4. Pointers

A C++ pointer is a variable that stores a memory address.

We know that variables in a computer program are used to label data with a descriptive identifier so that the data can be accessed and used by that computer program. How C++ variables are implemented is worthy of discussion.

In C++ the value of each variable is stored directly in memory without the need for either a reference or an object. This makes access faster, but it is one of the reasons we need to declare each variable because different types take differing amounts of space in memory!

The following code declares a variable called varN that has in it a value of 100:

// C++ variable declaration and assignment of an integer value
int varN = 100;

In C++ the results of running this code will look like the diagram below:

"Box named varN containing value of 100"

Figure 4: C++ variable

When we want to output the value to the console, we use the variable name to do so.

But, we can also identify the memory location of the variable, which is sometimes very valuable. This address may change each time the program is run. In C++, this will always look odd because it will be the actual memory address written in a hexadecimal code which is a base 16 code like 0x7ffd93f25244.

In C++ we use the address-of operator, & to reference the address.

Variables are stored in memory locations which are dependent upon the run itself. If you repeatedly run the above code you may see the location change.

In C++, variables store values directly, making them faster to reference.

If in C++, we want to create a reference to a memory location in C++, we must use a special syntax called a pointer.

1.9.4.1. Pointer Syntax

When declaring a pointer in C++ that will “point” to the memory address of some data type, you will use the same rules of declaring variables and data types. The key difference is that there must be an asterisk (*) between the data type and the identifier.

variableType *identifier; // syntax to declare a C++ pointer
int *ptrx; // example of a C++ pointer to an integer

White space in C++ generally does not matter, so the following pointer declarations are identical:

SOMETYPE *variablename; // preferable
SOMETYPE * variablename;
SOMETYPE* variablename;

However, the first declaration is preferable because it is clearer to the programmer that the variable is in fact a pointer because the asterisk is closer to the variable name.

1.9.4.2. The address-of operator, &

Now that we know how to declare pointers, how do we give them the address of where the value is going to be stored? One way to do this is to have a pointer refer to another variable by using the address-of operator, which is denoted by the ampersand symbol, &. The address-of operator & does exactly what it indicates, namely it returns the address.

The syntax is shown below, where varN stores the value, and ptrN stores the address of where varN is located:

variableType varN;  // a variable to hold the value
variableType *ptrN = &varN;  // a variable pointing to the address of varN

Keep in mind that when declaring a C++ pointer, the pointer needs to reference the same type as the variable or constant to which it points.

Expanding on the example above where varN has the value of 100.

//variable declaration for a single integer value
int varN = 100;
int *ptrN;
ptrN = &varN;

The results of running this C++ code will look like the diagram below.

image

Figure 5: View into memory with pointers

1.9.4.3. Accessing Values from Pointers

So, once you have a C++ pointer, how do you access the values associated with that location? You use the asterisk before the pointer variable, which goes to that address, effectively dereferencing the pointer, meaning that it will find the location of the value stored where the pointer was pointing.

In other words, varN and *ptrN (note the asterisk in front!) reference the same value in the code above.

Let’s extend the example above to output the value of a variable and its address in memory:

… _dereferencing:

Compiling and running the above code will have the program output the value in varN, what is in ptrN (the memory address of varN), and what value is located at that memory location.

The second output sentence is the address of varN, which would most likely be different if you run the program on your machine.

WARNING: What happens if you forget the asterisk when assigning a value to a pointer and had the following instructions instead?

This is BAD, BAD, BAD!

image

Figure 6: dangling pointer reference

If your compiler does not catch that error (the one for this class may), the first cout instruction outputs:

After changing *ptrN, varN now has: 50

which is expected because you changed where ptrN is pointing to and NOT the contents of where it is pointing.

The second cout instruction is a disaster because

  1. You don’t know what is stored in location 100 in memory, and

(2) that location is outside of your segment (area in memory reserved for your program), so the operating system will jump in with a message about a “segmentation fault”. Although such an error message looks bad, a “seg fault” is in fact, a helpful error because unlike the elusive logical errors, the reason is fairly localized.

1.9.4.4. The NULL pointer

The NULL pointer in C++ points to nothing and is often denoted by the keyword NULL (all caps) or by 0. The NULL pointer is often used in conditions and/or in logical operations.

The following example demonstrates how the NULL pointer works. The variable ptrx initially has the address of x when it is declared. On the first iteration of the loop, it is assigned the value of NULL (i.e. 0) thereby ending the loop:

Helpful Tip: The NULL pointer becomes very useful when you must test the state of a pointer, such as whether the assignment to an address is valid or not.

You have attempted of activities on this page