Create an abstract base class called Employee. Use this class to store employee details such as name,designation, basic_pay, DA, HRA, salary. Include a virtual method to compute salary. Derive two classes called Manager and Workers to include pay and allowances of employees to calculate total salary.
#include <iostream>
#include <string>
using namespace std;
class Employee{
private:
//name,designation, basic_pay, DA, HRA, salary
string name;
string designation;
double basic_pay;
double DA;
double HRA;
public:
//Constructor
Employee(){}
Employee(string name,string designation,double basic_pay,double DA,double HRA){
this->name=name;
this->designation=designation;
this->basic_pay=basic_pay;
this->DA=DA;
this->HRA=HRA;
}
~Employee(){}
//virtual method to compute salary.
virtual double calculateSalary(){
return basic_pay+DA+HRA;
}
virtual void display(){
cout<<"Employee name: "<<name<<"\n";
cout<<"Employee designation: "<<designation<<"\n";
cout<<"Employee basic pay: "<<basic_pay<<"\n";
cout<<"Employee DA: "<<designation<<"\n";
cout<<"Employee HRA: "<<HRA<<"\n";
}
};
//Derive two classes called Manager and Workers to include pay and allowances of employees to calculate total salary.
class Manager:public Employee{
private:
double pay;
public:
//Constructor
Manager(){}
Manager(string name,string designation,double basic_pay,double DA,double HRA,double pay):Employee(name,designation,basic_pay,DA,HRA){
this->pay=pay;
}
~Manager(){}
double calculateSalary(){
return Employee::calculateSalary()+pay;
}
void display(){
Employee::display();
cout<<"Manager pay: "<<pay<<"\n";
}
};
class Worker:public Employee{
private:
double allowances;
public:
//Constructor
Worker(){}
Worker(string name,string designation,double basic_pay,double DA,double HRA,double allowances):Employee(name,designation,basic_pay,DA,HRA){
this->allowances=allowances;
}
~Worker(){}
double calculateSalary(){
return Employee::calculateSalary()+allowances;
}
void display(){
Employee::display();
cout<<"Manager pay: "<<allowances<<"\n";
}
};
int main (){
Manager manager("Mary Clark","Operator",1500,50,50,150);
Worker worker("Mike Smith","Programmer",2500,150,150,550);
manager.display();
cout<<"Salary: "<<manager.calculateSalary()<<"\n\n";
worker.display();
cout<<"Salary: "<<worker.calculateSalary()<<"\n\n";
system("pause");
return 0;
}
Comments
Leave a comment