An array can be of any data type including struct. Similarly, we can also have arrays of variables of the type class. Such variables are called arrays of objects.
Class Definition:
class employee
{
char name[30];
float age;
public:
void getdata(void);
void putdata(void);
};
The identifier employee is a user-defined data type and can be used to create objects related to different employee categories.
employee manage[3]; //aray of managers
employee foreman[15]; //array of foreman
employee worker[75]; // array of worker
the array manager contains three objects(managers), namely, manager[0], manager[1], and manager[2], of type employee class similarly, the foreman array contains 15 objects. and the worker array contains 75 objectives.(workers).
#include <iostream>
using namespace std;
class employee
{
char name[30]; //string as class members
float age;
public:
void getdat(void);
void putdata(void);
};
void employee::getdata(void)
{
cout <<"Enter name: ";
cin >> name;
cout << " Enter age: ";
cin >> age;
}
void employee :: putdata(void)
{
cout<< "Name: "<<name<<"\n";
cout<<"Age:"<<age<<"\n";
}
const int size=3;
int main()
{
employee manager[size];
for(int i=0; i<size; i++)
{
cout<< "\nDetails of manger" <<i+1 << "\n";
manager[i].getdata();\
}
cout <<"\n";
for(i=0;i<size;i++)
{
cout <<"\nManager" << i+1<<"\n";
manager[i].putdata();
}
return 0;
}
Comments