方法1:
class Employee
{
public:
virtual int calculateSalary() = 0;
};
class PermanentEmployee : public Employee {
const int salaryPerMonth;
public:
PermanentEmployee(int sal) : salaryPerMonth(sal){}
int calculateSalary() {
return salaryPerMonth;
}
};
class ContractEmployee : public Employee {
const int wagesPerHr;
int totalHour;
public:
ContractEmployee(int sal) : wagesPerHr(sal), totalHour(0){}
void setWorkingDuration(int time) {
totalHour = totalHour + time;
}
int calculateSalary() {
return wagesPerHr * totalHour;
}
};
class Manager {
list<Employee *> ls;
public:
void assignWorkingHour() {
list<Employee …
Run Code Online (Sandbox Code Playgroud) 情况1:
class ObjectCount {
private:
ObjectCount(){}
};
class Employee : private ObjectCount {};
Run Code Online (Sandbox Code Playgroud)
案例2:
class ObjectCount {
public:
ObjectCount(){}
};
class Employee : private ObjectCount {};
Run Code Online (Sandbox Code Playgroud)
情况 1: ObjectCount 构造函数是私有的,继承是私有的。它给编译器错误
在 case2: ObjectCount 构造函数是 public 并且继承是 private 。这段代码没问题。
任何人都可以解释它是如何发生的吗?
c++ ×2