#include <iostream>
#include <string>
using namespace std;
// Base class
class Employee {
protected:
string name;
int id;
public:
Employee(string n, int i) {
name = n;
id = i;
}
virtual void calculateSalary() = 0; // Pure virtual function
void displayInfo() {
cout << "Name: " << name << ", ID: " << id << endl;
}
};
// Derived class for Faculty
class Faculty : public Employee {
private:
int coursesTaught;
double salaryPerCourse;
public:
Faculty(string n, int i, int courses, double perCourse)
: Employee(n, i), coursesTaught(courses), salaryPerCourse(perCourse) {}
void calculateSalary() {
double salary = coursesTaught * salaryPerCourse;
cout << "Faculty Salary: $" << salary << endl;
}
};
// Derived class for Staff
class Staff : public Employee {
private:
double hourlyWage;
int hoursWorked;
public:
Staff(string n, int i, double wage, int hours)
: Employee(n, i), hourlyWage(wage), hoursWorked(hours) {}
void calculateSalary() {
double salary = hourlyWage * hoursWorked;
cout << "Staff Salary: $" << salary << endl;
}
};
// Main function to demonstrate the hierarchy
int main() {
// Creating Faculty object
Faculty faculty("Dr. Smith", 101, 3, 2000.0);
faculty.displayInfo();
faculty.calculateSalary();
// Creating Staff object
Staff staff("John Doe", 201, 15.0, 160);
staff.displayInfo();
staff.calculateSalary();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gQmFzZSBjbGFzcwpjbGFzcyBFbXBsb3llZSB7CnByb3RlY3RlZDoKICAgIHN0cmluZyBuYW1lOwogICAgaW50IGlkOwoKcHVibGljOgogICAgRW1wbG95ZWUoc3RyaW5nIG4sIGludCBpKSB7CiAgICAgICAgbmFtZSA9IG47CiAgICAgICAgaWQgPSBpOwogICAgfQoKICAgIHZpcnR1YWwgdm9pZCBjYWxjdWxhdGVTYWxhcnkoKSA9IDA7IC8vIFB1cmUgdmlydHVhbCBmdW5jdGlvbgoKICAgIHZvaWQgZGlzcGxheUluZm8oKSB7CiAgICAgICAgY291dCA8PCAiTmFtZTogIiA8PCBuYW1lIDw8ICIsIElEOiAiID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