2.7 Arrays of Objects




An array of objects, all of whose elements are of the same class, can be declared just as an array of any built-in type. Each element of the array is an object of that class. Being able to declare arrays of objects in this way underscores the fact that a class is similar to a type.

Declaring Arrays of Objects

The simplest way to create an array of Frame objects is with the following declaration:
   Frame windowList[5];	    // an array of 5  Frame objects

An 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.

Manipulating Objects in an Array

An object in an array can be manipulated by a combination of the subscripting operator "[]" - to select which object of the array is to be manipulated - and the "." (dot) operator - to apply the operation to the selected object. For example:
   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);
    }


Next Stop


Exercises

  1. Do you think it is possible to have a two dimensional array of objects? Write a small test program to verify your theory.

  2. Frame Wave: Write a program that has ten windows. The windows are the same shape and are aligned horizontally with the vertical edges of adjacent windows touching. from left to right, each window has one of the numbers from 1 to 10 written in it. Each "wave" beings with the leftmost window moving up vertically and then back to its original position. This same action is repeated for each window from left to right. Repeat this for a few waves. In this version declare the array so that the constructor with no arguments is used.

  3. Modify the "Frame Wave" program so that each object in the array is constructed by giving values for each constructor argument.

  4. Modify either version of the "Frame Wave" program so that a new wave begins when the previous wave has reached the middle of the line of windows.

  5. Moving Ball: Write a program that has 10 windows all of the same size and arranged in a horizontal (or vertical) line. The first (leftmost or topmost) window has a circle drawn in it. The circle should move from the current window to the adjacent window. When it reaches the last (rightmost or bottommost) window, it reappers in the first window. Repeat this process indefinitely.

  6. Tic-Tac-Toe: Write a program with ten windows. Nine windows (the game windows) are arranged in a three by three grid pattern and the tenth window (the restart window) is placed off to one side. Initially all the game windows are empty. When a left mouse click occurs in an empty game window either an "O" or an "X" will appear beginning with an "O" and alternating thereafter. Make the "O" with a circle and the "X" out of two lines. Clicking in a game window that is not empty has no effect. Clicking in the tenth window causes all of the game windows to become empty.

  7. Leap Frog: Write a program that has five windows. The windows are of the same size and all have the same y-coordinate. The first window is placed near the left edge of the screen. The second window is placed somewhat to the right of the first window and overlapping the first window; the third window is similarly placed to the right of and overlapping the second window; and so on. On successive iterations the left-most window is moved so it is somewhat to the right and overlapping the right-most window. The window just moved is now the new right-most window. Continue this movement for some number of iterations.

Last Updated: July 23, 1996 / kafura@cs.vt.edu