#include <iostream>
#include <string>
using namespace std;
// Base class
class Employee {
protected:
string Ename;
int EmpID;
public:
Employee() : Ename(""), EmpID(0) {}
Employee(const string& name, int id) : Ename(name), EmpID(id) {}
virtual void accept() {
cout << "Enter employee name: ";
cin >> Ename;
cout << "Enter employee ID: ";
cin >> EmpID;
}
virtual void display() const {
cout << "Employee Name: " << Ename << "\nEmployee ID: " << EmpID << endl;
}
virtual double earnings() const = 0;
};
// Derived class for salaried employees
class SalariedEmployee : public Employee {
private:
double weeklySalary;
public:
SalariedEmployee() : Employee(), weeklySalary(0.0) {}
SalariedEmployee(const string& name, int id, double salary)
: Employee(name, id), weeklySalary(salary) {}
void accept() override {
Employee::accept();
cout << "Enter weekly salary: ";
cin >> weeklySalary;
}
void display() const override {
Employee::display();
cout << "Weekly Salary: " << weeklySalary << endl;
}
double earnings() const override {
return weeklySalary;
}
};
// Derived class for hourly employees
class HourlyEmployee : public Employee {
private:
double wage;
double hours;
public:
HourlyEmployee() : Employee(), wage(0.0), hours(0.0) {}
HourlyEmployee(const string& name, int id, double wageRate, double hrs)
: Employee(name, id), wage(wageRate), hours(hrs) {}
void accept() override {
Employee::accept();
cout << "Enter wage per hour: ";
cin >> wage;
cout << "Enter hours worked: ";
cin >> hours;
}
void display() const override {
Employee::display();
cout << "Wage: " << wage << "\nHours Worked: " << hours << endl;
}
double earnings() const override {
if (hours < 40) {
return hours * wage;
} else {
return 40 * wage + ((hours - 40) * wage) * 1.5;
}
}
};
// Derived class for commission employees
class CommissionEmployee : public Employee {
private:
double grossSales;
double commissionRate;
public:
CommissionEmployee() : Employee(), grossSales(0.0), commissionRate(0.0) {}
CommissionEmployee(const string& name, int id, double sales, double rate)
: Employee(name, id), grossSales(sales), commissionRate(rate) {}
void accept() override {
Employee::accept();
cout << "Enter gross sales: ";
cin >> grossSales;
cout << "Enter commission rate: ";
cin >> commissionRate;
}
void display() const override {
Employee::display();
cout << "Gross Sales: " << grossSales << "\nCommission Rate: " << commissionRate << endl;
}
double earnings() const override {
return grossSales * commissionRate;
}
};
int main() {
SalariedEmployee salariedEmployee;
HourlyEmployee hourlyEmployee;
CommissionEmployee commissionEmployee;
cout << "Enter details for Salaried Employee:\n";
salariedEmployee.accept();
cout << "\nSalaried Employee Details:\n";
salariedEmployee.display();
cout << "Earnings: " << salariedEmployee.earnings() << "\n\n";
cout << "Enter details for Hourly Employee:\n";
hourlyEmployee.accept();
cout << "\nHourly Employee Details:\n";
hourlyEmployee.display();
cout << "Earnings: " << hourlyEmployee.earnings() << "\n\n";
cout << "Enter details for Commission Employee:\n";
commissionEmployee.accept();
cout << "\nCommission Employee Details:\n";
commissionEmployee.display();
cout << "Earnings: " << commissionEmployee.earnings() << "\n\n";
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: