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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | class box { int length; int width; public: box(void); //Constructor void set(int new_length, int new_width); int get_area(void); }; box::box(void) //Constructor implementation { length = 8; width = 8; } // This method will set a box size to the two input parameters void box::set(int new_length, int new_width) { length = new_length; width = new_width; } // This method will calculate and return the area of a box instance int box::get_area(void) { return (length * width); } main() { box small; small.set(5, 7); cout << "The small box area is " << small.get_area() << "\n"; } // Result of execution // // The small box area is 35 |
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.
AN ARRAY OF OBJECTS
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | // objarray.cpp #include <iostream.h> class box { int length; int width; static int extra_data; // Declaration of extra_data public: box(void); //Constructor void set(int new_length, int new_width); int get_area(void); int get_extra(void) {return extra_data++;} }; int box::extra_data; // Definition of extra_data box::box(void) //Constructor implementation { length = 8; width = 8; extra_data = 1; } // This method will set a box size to the two input parameters void box::set(int new_length, int new_width) { length = new_length; width = new_width; } // This method will calculate and return the area of a box instance int box::get_area(void) { return (length * width); } main() { box small, medium, large, group[4]; //Seven boxes to work with small.set(5, 7); large.set(15, 20); for (int index = 1 ; index < 4 ; index++) //group[0] uses default group[index].set(index + 10, 10); cout << "The small box area is " << small.get_area() << "\n"; cout << "The medium box area is " << medium.get_area() << "\n"; cout << "The large box area is " << large.get_area() << "\n"; for (index = 0 ; index < 4 ; index++) cout << "The array box area is " << group[index].get_area() << "\n"; cout << "The extra data value is " << small.get_extra() << "\n"; cout << "The extra data value is " << medium.get_extra() << "\n"; cout << "The extra data value is " << large.get_extra() << "\n"; cout << "The extra data value is " << group[0].get_extra() << "\n"; cout << "The extra data value is " << group[3].get_extra() << "\n"; } // Result of execution // // The small box area is 35 // The medium box area is 64 // The large box area is 300 // The array box area is 64 // The array box area is 110 // The array box area is 120 // The array box area is 130 // The extra data value is 1 // The extra data value is 2 // The extra data value is 3 // The extra data value is 4 // The extra data value is 5 |
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.
DECLARATION AND DEFINITION OF A VARIABLE
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.