CSCI 201 Study Aid: Objects and Data Types

Objects

Objects correspond to locations within the computer's memory where data may be stored. Every object has a name, type, and value and is stored in a certain amount of memory. Objects are "declared" within a program using declaration statements (also called deinitions in the text) and "assigned" values using assignment statements, as shown in the example below:

Example 1:


1   #include <iostream.h>
2   #include <stdlib.h>

3   main()
4   {

5   int num1 = 10;
6   int num2 = 5;
7   int sum;

8   sum = num1 + num2;

9   cout << num1 << " + " << num2 << " = " << sum << endl;

10  }

Lines 5, 6, and 7 are declaration statements. The words num1, num2, and sum are the names of objects. The declarations specify that the objects num1, num2, and sum are data of type int, which means that these objects will hold integer values (i.e: whole numbers such as 12, -33, 0, 32123). In C++, the keyword int is reserved to declare integer objects.

All objects must be declared with a name and data type before they can be used in a program. Several objects may be declared in one declaration statement, for example, lines 5, 6, and 7 could have been written as a single line as follows:


   int num1 = 10, num2 = 5, sum;

Objects may also be declared and initialized to a known value as shown in the program above. Note that the object sum was not initialized so the value of the object is not known until it is assigned a value. Line 8 assigns the object sum the result of adding num1 and num2 together.

A object name is any valid identifier. An identifier is a series of characters consisting of letters, digits and underscores (_) that does not begin with a digit. C++ allows identifiers of any length, but your C++ environment may impose some restrictions on the length of identifiers. C++ is case sensitive, uppercase and lowercase letters are different, so the objects a1 and A1 are considered to be two different objects.

Declarations can be placed almost anywhere in a function. However, the declaration of a object must appear before the object is used in a program.


Data Types

C++ supports a variety of data types used when declaring objects. The standard data types are shown in the table below:

Type
keyword(s)
Description Range and Precision
char One byte integer, unsigned 0 to 255
short Two-byte integer -32,768 to 32,767
unsigned short Two-byte integer, unsigned 0 to 65,535
long Four-byte integer Approx. -2.147 billion to 2.127 billion
unsigned long Four-byte integer, unsigned 0 to Approx. 4.292 billion
int Same as short or long
depending on implementation
 
unsigned int or
unsigned
Same as unsigned short or
unsigned long, depending
on implementation
 
float Four-byte floating point Approx. plus or minus 3.4 times 10 to the 38th. Seven digits of precision.
double Eight-byte floating point Approx. plus or minus 1.7 times 10 to the 308th. 15 digits of precision.

Note: Character strings in C++ are implemented as an array of char. We will discuss arrays and strings in a subsequent topic.


Example 2:

The program below declares a number of objects of different types and then displays the values of each:

#include <iostream.h>
#include <stdlib.h>

main()
{

signed   char   v1 = 'A';
unsigned char   v2 = 'B';
char            v3 = 67;

short           v4 = 100;
unsigned short  v5 = 200;

long            v6 = 1000;
unsigned long   v7 = 2000;

int             v8 = 3000;
unsigned int    v9 = 4000;
unsigned       v10 = 5000;

float          v11 = 3.141592;

double         v12 = 1.1710394893;

cout << " v1 = " << v1 << endl;
cout << " v2 = " << v2 << endl;
cout << " v3 = " << v3 << endl;
cout << " v4 = " << v4 << endl;
cout << " v5 = " << v5 << endl;
cout << " v6 = " << v6 << endl;
cout << " v7 = " << v7 << endl;
cout << " v8 = " << v8 << endl;
cout << " v9 = " << v9 << endl;
cout << "v10 = " << v10 << endl;
cout << "v11 = " << v11 << endl;
cout << "v12 = " << v12 << endl;

return 0;

}

const Objects

The const keyword lets you create a object while ensuring that its value will not change. This keyword modifies a declaration, informing the compiler that it must watch for and prevent all assignments to the item. Initialization, however, is allowed. Once a object is defined with const, the compiler flags all statements in which the object is the target of an assignment.


const int id = 12345;  // This is valid; id may be initialized.
...

id = 1000;   // ERROR! Attempt to assign a const item a new value.