I am new to C++ and OOP and was given a problem that I struggled with. I was asked to design classes to represent employees and managers. My implementation is below.
class Manager;
class Employee {
private:
int salary;
public:
std::string name;
Manager* manager;
Employee(int salary, std::string name, Manager* manager) : salary(salary), name(name), manager(manager) {}
int getSalary() {
return this->salary;
}
friend int Manager::giveRaise(Employee, int); // incomplete type manager named in nested name specifier
};
class Manager : Employee {
private:
std::string perks;
std::vector<Employee> employees;
public:
Manager(std::string perks, int salary, std::string name, Manager* manager) : Employee(salary, name, manager) {
this->perks = perks;
}
int getEmployeeSalary(Employee e) {
if (e.manager == this) {
return e.getSalary();
}
throw new std::invalid_argument("Employee is not managed by this manager");
}
int giveRaise(Employee e, int new_salary) {
if (e.manager == this) {
e.salary = new_salary; // Can't access private method
}
}
};
Unfortunately, I can't declare the giveRaise method as a friend because I haven't defined the manager class at that point. However, I'm not sure how to rearrange the code to get around this error. I don't want to make the salary field protected because I only want a specific manager to be able to access it.
Thanks for your help.
manager_bonus = 10 * sum(employee_bonuses)
. That's all you need to know. – Bathsheba Jul 1 '20 at 18:56Manager
should probably inherit fromEmployee
. I don't know if aPerson
abstract class is helpful or not. – Fred Larson Jul 1 '20 at 19:08private
data of another class are A) an accessible getter function and B)friend
ship. – user4581301 Jul 1 '20 at 19:24