The purpose of this tutorial is to illustrate how to use some of the traditional aspects of C or C++ with classes and objects. Pointers to an object as well as pointers within an object will be illustrated. Arrays embedded within an object, and an array of objects will be illustrated. Since objects are simply another C++ data construct, all of these things are possible and can be used if needed.

In order to have a systematic study, we will use the program named BOXES1.CPP from the last tutorial as a starting point and we will add a few new constructs to it for each example program. You will recall that it was a very simple program with the class definition, the class implementation, and the main calling program all in one file.

Here is that program again.

This was selected as a starting point because we will eventually make changes to all parts of the program and it will be convenient to have it all in a single file for illustrative purposes.


Examine the file named OBJARRAY.CPP for our first example of an array of objects. This file is nearly identical to the file named BOX1.CPP until we come to line 44 where an array of 4 boxes are declared.

Recalling the operation of the constructor you will remember that each of the four box objects will be initialized to the values defined within the constructor since each box will go through the constructor as they are declared. In order to declare an array of objects, a constructor for that object must not require any parameters. (We have not yet illustrated a constructor with initializing parameters, but we will in the next program.) This is an efficiency consideration since it would probably be an error to initialize all elements of an array of objects to the same value. We will see the results of executing the constructor when we compile and execute the file later.

Line 49 defines a for loop that begins with 1 instead of the normal starting index for an array leaving the first object, named group[0], to use the default values stored when the constructor was called. You will observe that sending a message to one of the objects uses the same construct as is used for any object. The name of the array followed by its index in square brackets is used to send a message to one of the objects in the array. This is illustrated in line 50 and the operation of that code should be clear to you. The other method is called in the output statement in lines 57 and 58 where the area of the four boxes in the group array are listed on the monitor.

Another fine point should be pointed out. The integer variable named index is declared in line 49 and is still available for use in line 56 since we have not yet left the enclosing block which begins in line 43 and extends to line 65.


An extra variable was included for illustration, the one named extra_data in line seven. Since the keyword static is used to modify this variable in line 7, it is an external variable and only one copy of this variable will ever exist. All seven objects of this class share a single copy of this variable which is global to the objects defined in line 44.

The variable is actually only declared here which says it will exist somewhere, but it is not defined. A declaration says the variable will exist and gives it a name, but the definition actually defines a place to store it somewhere in the computers memory space. By definition, a static variable can be declared in a class header but it cannot be defined there, so it is defined somewhere outside of the header, usually in the implementation file. In this case it is defined in line 16 and can then be used throughout the class.

Line 23 of the constructor sets the single global variable to 1 each time an object is declared. Only one assignment is necessary so the other six are actually wasted code. To illustrate that there is only one variable shared by all objects of this class, the method to read its value also increments it. Each time it is read in lines 60 through 64, it is incremented and the result of the execution proves that there is only a single variable shared by all objects of this class. You will also note that the method named get_extra() is defined within the class declaration so it will be assembled into the final program as inline code. Be sure you understand this program and especially the static variable, then compile and execute it to see if you get the same result as listed at the end of the program.