Arrays of Objects Flashcards
How do you declare and initialize an array of objects so that all the objects in the array run the default constructor?
Declare the array of the object type and its size but don’t specify any objects.
// Creates an array of 10 Objects using the default constructor
Object myArray[10];
How do you declare and initialize an array of objects such that each object in the array can use a specific constructor?
Declare the array of the object type and its size and then explicitly call the constructor in the assignment.
// Creates an array of 3 Objects using different constructors for each
Object myArray[3] = { Object(2,1), Object(), Object(48) };
When you call a constructor in an object array’s initialization, what must you do for default objects?
Call them explicitly.
// The first object in the array uses the default constructor, which is called there explicitly.
Object myArray[2] = { Object(), Object(5,8) };
How do you call member data/functions for an object contained in an array?
There are several ways, but it’s the same basic syntax as calling normally, except that the objects don’t have variable names.
objectName.memberName;
The most basic way is through array notation:
arr[1].Show();
arr[2].value1;
You can also use pointers:
Object * oPtr = &arr[3];
// These are functionally identical
oPtr->Show();
(*oPtr).value1;