Write the program, which has three classes one, is StudentRecord having members (degree,year) and functions (getdata), the 2nd class is called EmployeeRecordwhich has data members (emp_id and salary). The 3rd class is Manager having title, dues and an object of EmployeeRecord as well as StudentRecord. Create a function of getdata in all of these classes and call where necessary. Using this scenario, show how the concept of Composition works.
#include<iostream>
using namespace std;
class StudentRecord{
private:
string degree;
int year;
public:
void getData(){
cout<<"Enter the degree"<<endl;
cin>>this->degree;
cout<<"Enter the year"<<endl;
cin>>this->year;
cout<<"The degree is\t"<<this->degree<<endl;
cout<<"The year is\t"<<this->year<<endl;
}
};
class EmployeeRecord{
private:
int emp_id;
double salary;
public:
getData(){
cout<<"Enter the employee id"<<endl;
cin>>this->emp_id;
cout<<"Enter the employee salary"<<endl;
cin>>this->salary;
cout<<"The employee id is\t"<<this->emp_id<<endl;;
cout<<"The employee salary is\t"<<this->salary<<endl;
}
};
class Manager{
private:
string title;
string dues;
EmployeeRecord record;
StudentRecord student;
public:
void getData(){
record.getData();
student.getData();
cout<<"Enter the employee title"<<endl;
cin>>this->title;
cout<<"Enter the employee dues"<<endl;
cin>>this->dues;
cout<<"The employeee title is\t"<<this->title<<endl;
cout<<"The employee due\t" <<this->dues<<endl;
}
};
int main(){
Manager mg;
mg.getData();
}
In composition, a class can access all the instances of other classes and all the variables within other classes can be used in that particular class. In the example above, the class Manager uses the objects of StudentRecord and EmployeeRecord. The getData() functions in the EmployeeRecord and StudentRecord is being accessed at the class Manager.
Comments
Leave a comment