Create an abstract class `Employee` with a pure virtual function `calculateSalary()`. Derive classes `FullTime` and `PartTime` and implement salary calculations.
SOLUTION....
#include <iostream>
using namespace std;
// Abstract class
class Employee {
protected:
string name;
int empID;
public:
Employee(string n, int id) : name(n), empID(id) {}
virtual void calculateSalary() = 0; // Pure virtual function
virtual void display() {
cout << "Employee ID: " << empID << endl;
cout << "Name: " << name << endl;
}
};
// Derived class: FullTime Employee
class FullTime : public Employee {
private:
float basicPay, allowance;
public:
FullTime(string n, int id, float b, float a)
: Employee(n, id), basicPay(b), allowance(a) {}
void calculateSalary() override {
float salary = basicPay + allowance;
display();
cout << "Employment Type: Full Time" << endl;
cout << "Salary: " << salary << endl;
cout << "-------------------------" << endl;
}
};
// Derived class: PartTime Employee
class PartTime : public Employee {
private:
int hoursWorked;
float payPerHour;
public:
PartTime(string n, int id, int h, float p)
: Employee(n, id), hoursWorked(h), payPerHour(p) {}
void calculateSalary() override {
float salary = hoursWorked * payPerHour;
display();
cout << "Employment Type: Part Time" << endl;
cout << "Salary: " << salary << endl;
cout << "-------------------------" << endl;
}
};
// Main function
int main() {
Employee* e; // Base class pointer
// Full-time employee
FullTime f1("Hardik", 101, 30000, 5000);
e = &f1;
e->calculateSalary();
// Part-time employee
PartTime p1("Raj", 102, 40, 200);
e = &p1;
e->calculateSalary();
return 0;
}
OUTPUT
