An Employee has a name and a monthly salary; pay() returns the salary. A Manager is an Employee with a bonus; its pay() returns salary plus bonus. Complete Manager and print what each person earns.
What to build
A Manager class derived from Employee that adds a bonus to the pay.
Requirements
- Derive
Managerpublicly fromEmployee. - Pass the name and salary to the
Employeeconstructor. - Make the manager's
pay()return the salary plus the bonus.
Input
The first line has n (1 ≤ n ≤ 100). Each of the next n lines is E name salary or M name salary bonus (whole numbers up to 1,000,000).
Output
One line per person: NAME earns PAY.
Constraints
1 ≤ n ≤ 100, and the amounts are whole numbers up to 1,000,000.
Example
Input:
2
E Rahim 30000
M Nadia 50000 10000
Output:
Rahim earns 30000
Nadia earns 60000
Why it works
A manager is an employee, so it inherits the name and salary. Only the pay rule differs, so that is the only part Manager adds.
Examples
2 E Rahim 30000 M Nadia 50000 10000
Rahim earns 30000 Nadia earns 60000
1 E Karim 0
Karim earns 0
Hints
Hint 1
Manager derives from Employee: class Manager : public Employee.
Hint 2
Its constructor passes name and salary to Employee's: Manager(string n, int s, int b) : Employee(n, s), bonus(b) {}
Hint 3
Make salary protected so Manager::pay can use it.
Solution
Show one correct solution (try it yourself first)
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name;
int salary;
public:
Employee(string n, int s) : name(n), salary(s) {}
string getName() const { return name; }
int pay() const { return salary; }
};
class Manager : public Employee {
int bonus;
public:
Manager(string n, int s, int b) : Employee(n, s), bonus(b) {}
int pay() const { return salary + bonus; }
};
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string kind, name;
int salary;
cin >> kind >> name >> salary;
if (kind == "M") {
int bonus;
cin >> bonus;
Manager m(name, salary, bonus);
cout << m.getName() << " earns " << m.pay() << endl;
} else {
Employee e(name, salary);
cout << e.getName() << " earns " << e.pay() << endl;
}
}
return 0;
}