/    /  CPP – Arrays within a Class

Arrays within a Class

 

Arrays can be used as member variables in a class. 

The array variable a[ ] declared as a private member of the class sample can be used in member functions, similar to any other array variable. 

 

For instance, the following class definition is valid.

class sample

{
int a[10]; // array within a class.
public:
void setval();
void display();
};

 

Array of Objects

An array may be of any kind of data, including structure. 

Similarly, we may also have arrays of variables that are of the type class. These variables are known as arrays of objects.

For example,

class Employee

{
char name[20];
float age;
public:
void getdata();
void display();
};
int main()
{
Employee SBI[3];
Employee foreman[4]; 
- - - 
return 0;
}

 

The identifier Employee is a user-defined data type and can be used to create objects for various categories of employees. For example,