Develop a C++ program implementing hierarchical inheritance upon a base class "Employee", and derived classes "Full-time" containing basic salary and allowances for calculating the salary of the employee and "Part-time" containing rate_per_day and no. of days worked for calculating the pay.
#include <iostream>
#include <string>
using namespace std;
class Employee{
private:
string name;
public:
Employee(string name){
this->name=name;
}
void print(){
cout<<"Name: "<<name<<"\n";
}
};
class FullTime:Employee{
private:
double basicSalary,allowances;
public:
FullTime(double basicSalary,double allowances,string name):Employee(name){
this->basicSalary=basicSalary;
this->allowances=allowances;
}
void print(){
Employee::print();
cout<<"Bsic salary: "<<basicSalary<<"\n";
cout<<"Allowances: "<<allowances<<"\n";
cout<<"Salary: "<<calculateSalary()<<"\n";
}
double calculateSalary(){
return basicSalary+allowances;
}
};
class PartTime:Employee{
private:
double rate_per_day;
int noDaysWorked;
public:
PartTime(double rate_per_day,double noDaysWorked,string name):Employee(name){
this->rate_per_day=rate_per_day;
this->noDaysWorked=noDaysWorked;
}
void print(){
Employee::print();
cout<<"Rate per day: "<<rate_per_day<<"\n";
cout<<"No. of days worked : "<<noDaysWorked<<"\n";
cout<<"Salary: "<<calculateSalary()<<"\n";
}
double calculateSalary(){
return rate_per_day*noDaysWorked;
}
};
int main() {
FullTime fullTime(2000,400,"John");
fullTime.print();
PartTime partTime(40,20,"Mike");
partTime.print();
system("pause");
return 0;
}
Comments
Leave a comment