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
#include <iostream>
#include <string>
using namespace std;
class Employee{
private:
string name;
public:
Employee(string name){
this->name=name;
}
void display(){
cout<<"Name: "<<name<<"\n";
}
};
class FullTime:Employee{
private:
double basic_salary,allowances;
public:
FullTime(double basic_salary,double allowances,string name):Employee(name){
this->basic_salary=basic_salary;
this->allowances=allowances;
}
void display(){
Employee::display();
cout<<"Bsic salary: "<<basic_salary<<"\n";
cout<<"Allowances: "<<allowances<<"\n";
cout<<"Salary: "<<getSalary()<<"\n";
}
double getSalary(){
return basic_salary+allowances;
}
};
class PartTime:Employee{
private:
double rate_per_day;
int no_days_worked;
public:
PartTime(double rate_per_day,double no_days_worked,string name):Employee(name){
this->rate_per_day=rate_per_day;
this->no_days_worked=no_days_worked;
}
void display(){
Employee::display();
cout<<"Rate per day: "<<rate_per_day<<"\n";
cout<<"No. of days worked : "<<no_days_worked<<"\n";
cout<<"Salary: "<<getSalary()<<"\n";
}
double getSalary(){
return rate_per_day*no_days_worked;
}
};
int main() {
FullTime ft(180,500,"Peter");
ft.display();
PartTime pt(10,10,"Mary");
pt.display();
system("pause");
return 0;
}
Comments
Leave a comment