Aim:
To implement classes with Arrays as data members
Algorithm:
- Start the program.
- Include the header files.
- Create the class array with data member function getdata(), putdata().
- Create an array of the object for the class name array in the main function.
- Get input for max size of array elements.
- Get the input for elements to be shared in an array.
- Print incremented value of array elements using member function
- 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.