We know that an array can be of any data type including struct. Similarly, we can also have arrays of variables that are of the type class. Such variables are called arrays of objects . Consider the following class definition:
Class employee
{
char name[40];
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 that relate to different categories of the employees.
For example
Employee manager[10];
The array manager contains ten objects, namely manager[0], manager[1] ..
manager[9] of type employee class.
Sample Program
#include <iostream.h>
class employee
{
char name[40];
float age;
public:
void getdata(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<<endl;
cout<<”Age “<<age<<endl;
}
const int size = 3;
void main()
{
employee manager[size];
for(int i =0;i<size;i++)
{
cout<<”Details of manager “<<i+1<<endl;
manager[i].getdata();
}
for(int i =0;i<size;i++)
{
cout<<”Manager “<<i+1<<endl;
manager[i].putdata();
}
}
No comments:
Post a Comment