Arrays of Objects Flashcards

1
Q

How do you declare and initialize an array of objects so that all the objects in the array run the default constructor?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare and initialize an array of objects such that each object in the array can use a specific constructor?

A

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) };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When you call a constructor in an object array’s initialization, what must you do for default objects?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you call member data/functions for an object contained in an array?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly