Create a class employee which stores name, id and salary of an employee. Derive two [5]
classes ‘regular’ and ‘part-time’ from it. The class ‘regular’ stores TA, DA and grade-pay
and class ‘part-time‘ stores number of hours and pay-per-hour for an employee. Display
all the information for a regular and a part-time employee, using the concept of virtual
function.
#include <iostream>
#include <string>
using namespace std;
class Employee
{
private:
string name;
int id;
float salary;
public:
Employee(){}
Employee(string name,int id,float salary){
this->name=name;
this->id=id;
this->salary=salary;
}
virtual void display(){
cout<<"Name: "<<name<<"\n";
cout<<"Id: "<<id<<"\n";
cout<<"Salary: "<<salary<<"\n";
}
};
class RegularEmployee: public Employee
{
private:
float TA;
float DA;
float gradePay;
public:
RegularEmployee(string name,int id,float salary,float TA,float DA,float gradePay):Employee(name,id,salary){
this->TA=TA;
this->DA=DA;
this->gradePay=gradePay;
}
void display(){
cout<<"Regular employee:\n";
Employee::display();
cout<<"TA: "<<TA<<"\n";
cout<<"DA: "<<DA<<"\n";
cout<<"Grade pay: "<<gradePay<<"\n\n";
}
};
class PartTimeEmployee : public Employee
{
private:
float numberHours;
float payPerHour;
public:
PartTimeEmployee(string name,int id,float salary,float numberHours,float payPerHour):Employee(name,id,salary){
this->numberHours=numberHours;
this->payPerHour=payPerHour;
}
void display(){
cout<<"Part time employee:\n";
Employee::display();
cout<<"Number of Hours: "<<numberHours<<"\n";
cout<<"Pay per hour: "<<payPerHour<<"\n\n";
}
};
int main(){
Employee** employees = new Employee*[2];
employees[0]=&RegularEmployee("Peter Smith",456123,5600,500,600,150);
employees[1]= &PartTimeEmployee("Mary Clark",13246,4400,40,10);
employees[0]->display();
employees[1]->display();
int l;
cin>>l;
return 0;
}
Comments
Leave a comment