Frame windowList[5]; // an array of 5 Frame objectsAn important aspect of declaring arrays of objects in this way is that all of the objects in the array must be constructed in the same way. It is not possible with this declaration to give each different object in the array a different set of constructor values. Furthermore, since no constructor arguments are given, the class must contain a constructor that has no arguments. Arrays of this form are useful when all of the objects should be constructed in a uniform way or when the "real" constructor information will not be know until sometime during the computation. In the later case, the array can be declared and the individual objects manipulated when the information is discovered. For example, the user may be asked to supply the name of a file which contains the desired locations and shapes for each of the windowList objects. This information can be read and each array element can then be moved and resized accordingly.
In other cases, it is desired that each of the objects in an array be specifically and individually constructed at the time the array is declared. This can be done as follows:
Frame windowList[5] = {Frame("Window 0", 0, 100, 100, 100), Frame("Window 1", 25, 100, 100, 100), Frame("Window 2", 50, 100, 100, 100), Frame("Window 3", 75, 100, 100, 100), Frame("Window 4", 100, 100, 100, 100) };Each object in the array is constructed using explicitly specified values for each constructor argument. This allows the programmer complete control over the initialization of the objects in the array.
It is not necessary to specifiy all of the constructor arguments if
there are overloaded constructors, as there are for the Frame
class. An object in an array can be constructed using any of the
constructors. For example, if it was only desired to specify the name
and initial location, but not the shape, for each object in the array
then the following declaration would suffice:
Frame windowList[5] = {Frame("Window 0", 0, 100), Frame("Window 1", 25, 100), Frame("Window 2", 50, 100), Frame("Window 3", 75, 100), Frame("Window 4", 100, 100) };In this case the overload constructor will determine the shape of each object. It is also possible to use different constructors for each objects as shown here:
Frame windowList[5] = {Frame("Window 0", 0, 100, 100, 100), Frame("Window 1", 25, 100), Frame("Window 2"), Frame(), Frame("Window 4", 100, 100, 100, 100) };In this version, the first and last objects in the array are constructed by explicitly providing each constructor argument. The constructor for the object named "Window 1" specifies only the location. The constructor for the object named "Window2" specifies only the name. The constructor for the object as subscript position 3 specifies no constructor arguments, allowing all defaults to apply, including the name.
windowList[3].MoveTo(100, 50);moves the object with subscript 3 to a new position. Remember that the subscripts begin with 0.
One of the advantages of working with arrays of objects is that it is easy to program the same operation over all of the objects. For example, a single loop can shrink all of windows by 10% as follows:
for (int i = 0; i++; i<5) windowList[i].resize(0.9);More complex operations involving the elements in the array are also possible. For example the following loop positions the windows along a diagonal from upper left toward lower right, makes them all of the size:
for (int i = 0; i++; i<5) { windowList[i].MoveTo(10*i+1, 10*i+1); windowList[i].Resize(50, 50); }
|
Exercises |