CPP Program to Implement Classes with Arrays as Data Members

Aim:

To implement classes with Arrays as data members

Algorithm:

  1. Start the program.
  2. Include the header files.
  3. Create the class array with data member function getdata(), putdata().
  4. Create an array of the object for the class name array in the main function.
  5. Get input for max size of array elements.
  6. Get the input for elements to be shared in an array.
  7. Print incremented value of array elements using member function
  8. Stop the program.

Program:

#include<iostream>
using namespace std;
class emplo{
private:
string name;
string id;
string pos;
long sal;
public:
void getdata(string n,string i,string p,long s)
{
name=n;
id=i;
pos=p;
sal=s;
}	
void disp();
};
void emplo::disp(){
cout<<"NAME     :"<<name<<endl;
cout<<"ID       :"<<id<<endl; 
cout<<"POSITION :"<<pos<<endl;
cout<<"SALAR    :"<<sal<<endl; 
cout<<"G.SALAR  :"<<12*sal<<endl<<endl;
}
int main()
{
emplo a[3];
string name,id,pos;
long k;
for(int i=0;i<3;i++)
{
cout<<"ENTER THE NAME ID POSITION SALARY\n";
cin>>name>>id>>pos>>k;
a[i].getdata(name,id,pos,k);		
}
for(int i=0;i<3;i++)
{
a[i].disp();		
}
return 0;
}

Execution:

INPUT :

ENTER THE NAME ID POSITION SALARY
XXXX
AB101
MANAGER
10000
ENTER THE NAME ID POSITION SALARY
YYYY
CD199
LABOUR
3000
ENTER THE NAME ID POSITION SALARY
ZZZZ
XY144
LABOUR
1000

OUTPUT : 

NAME     : XXXX 
ID       :AB101
POSITION :MANAGER
SALARY    :10000
G.SALARY:120000

NAME: YYYY
ID: CD199
POSITION: LABOUR
SALARY:3000
G.SALARY  :36000

NAME: ZZZZ
ID: XY144
POSITION: LABOUR
SALARY:1000
G.SALARY  :12000

Result:

Thus, the Implementation Classes with Arrays as Data Members was executed successfully.

Leave a Comment