Array of Type Class


  Array of Type Class
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 array of objects. Consider the following class definition:

class employee
{
            char name[30];
            float age;
            public:
                        void getdata();
                        void putdata();
};
The identifier employee is a user-defined data type and can be used to create objects that relate to different categories of the employees. Example:
employee manager[3[];                 // array of manager
employee foreman[15];                 // array of foreman
employee worker[75];                   // array of worker
The array manager contains 3 objects of manager of type employee class. Similarly, the foreman array contains 15 objects and the worker array contains 75 objects.

Example:
#include<iostream.h>  
#include<conio.h>  

class employee 
{   
char name[50];
int age;     
public:
    void getdata();    
    void showdata();  
};  
void  employee :: getdata()
   {   
cout<<“enter name=“;  
cin>>name;   
cout<<“enter age=“;   
cin>>age;  
}  
void employee :: showdata()  
  {   
cout<<name<<endl<<age;  
  }

void main() 
  {   
clrscr();  
int  size=4;  
employee  manager[size];  
for(int i=0; i<size;i++)  
  {
cout<<“Enter Details of managers”;   
manager[i].getdata();  
  }  
cout<<endl;  
for(i=0;i<size;i++)  
  {   
cout<<“Details of Managers  is=“;   
manager[i].putdata();  
  }  
   getch();

 }





Comments

Popular posts from this blog

ASCII / ISCII / UNICODE

CISC / RISC / EPIC

Evolution of Computers