Like an array of other user-defined data types, an array of type classes can be created as well. The array of the type class always contains the objects of the class as its individual elements.
In addition, an array of a class type is also referred to as an array of objects. However, an array of objects is declared in the same manner as an array of any built-in data type.
Syntax:
class_name array_name [size] ;
Example:
#include <iostream>
class MyClass {
int x;
public:
void setX(int i) { x = i; }
int getX() { return x; }
};
void main()
{
MyClass obs[4];
int i;
for(i=0; i < 4; i++)
obs[i].setX(i);
for(i=0; i < 4; i++)
cout << "obs[" << i << "].getX(): " << obs[i].getX() << "n";
getch();
}