Arrays of Objects
In C++, arrays of objects can be declared and used just like any other type of array. The syntax for creating an object array is the same as for any other array. For instance, a three-element array of objects can be created using the following code
copy
class MyClass {
// class definition
};
MyClass objArray[3];
If a class has a parameterized constructor, each object in the array can be initialized using an initialization list. The initialization list can be defined with the normal array initialization syntax. For objects whose constructors have only one parameter, a list of initial values can be specified. For instance, consider the following code
copy
class MyClass {
int i;
public:
MyClass(int j) { i=j; } // constructor
int get_i() { return i; }
};
int main() {
MyClass ob[3] = {1, 2, 3}; // initializers
int i;
for(i=0; i<3; i++)
cout << ob[i].get_i() << "\n";
return 0;
}
This program displays the numbers 1, 2, and 3 on the screen. The initialization syntax used in the above program is shorthand for the longer form
copy
MyClass ob[3] = { MyClass(1), MyClass(2), MyClass(3) };
The short form works because of the automatic conversion that applies to constructors taking only one argument. Thus, it can only be used to initialize object arrays whose constructors only require one argument.
If an object's constructor requires two or more arguments, the longer initialization form must be employed. For example
copy
class MyClass {
int i;
int j;
public:
MyClass(int x, int y) { i=x; j=y; } // constructor
int get_i() { return i; }
int get_j() { return j; }
};
int main() {
MyClass ob[3] = { MyClass(1, 2), MyClass(3, 4), MyClass(5, 6) };
int i;
for(i=0; i<3; i++)
cout << ob[i].get_i() << ", " << ob[i].get_j() << "\n";
return 0;
}
This program displays the values of i and j for each object in the array.
One thing to note is that if you intend to create both initialized and uninitialized arrays of objects, you need to overload the constructor by adding one that takes no parameters. This allows you to declare arrays that are initialized and those that are not. For instance
copy
class MyClass {
int i;
public:
MyClass() { i=0; } // overloaded constructor
MyClass(int j) { i=j; } // constructor
int get_i() { return i; }
};
int main() {
MyClass a[9]; // uninitialized array
MyClass b[3] = {1, 2, 3}; // initialized array
int i;
for(i=0; i<9; i++)
cout << a[i].get_i() << "\n";
for(i=0; i<3; i++)
cout << b[i].get_i() << "\n";
return 0;
}
In this example, the overloaded constructor in the MyClass class allows us to declare an uninitialized array a without getting any compilation errors. When an object is created without any explicit initialization, the default constructor is called. In this case, since we have provided an overloaded constructor with no parameters, it serves as the default constructor.
When we declare an array 'a' of 'MyClass' objects without any initialization, the default constructor is used to initialize each element to its default value, which in this case is '0'. Thus, when we loop through the array 'a' and print out the value of 'i' for each element, we see that it is '0' for all elements.
On the other hand, when we declare an array 'b' of 'MyClass' objects with explicit initialization values, the constructor that takes an integer parameter is used to initialize each element with the specified value. In this case, the elements are initialized to '1', '2', and '3'. When we loop through the array b and print out the value of 'i' for each element, we see that it is the corresponding initialized value for each element.