在下面的过度简化示例中,class Derived
继承了class Base
的私有成员/方法。我想要做的是能够在Derived类的继承结构中添加一个额外的成员(double salary
)。
struct Person {
int age;
int height;
};
class Base {
protected:
Person worker;
public:
double hours (Person *employee);
};
class Derived: public Base {
private:
//Class has an inherited member named "Person worker"
//How do I add new member "double salary" to struct "Person worker"
double salary (Person *manager); //A method that will rely on "double salary"
};
也许这样的事情(如下面Vlad所建议的那样)可行,但是有更好的方法吗?
struct Person {
int age;
int height;
};
struct Person2: Person {
double salary;
}
class Base {
private:
Person worker;
public:
double hours (Person *employee);
};
class Derived: public Base {
private:
Person2 fulltime;
double salary (Person2 *manager); //A method that will rely on "double salary"
};
如果我错了,请更正我,但Person worker
仍然由派生类继承,除了它无法直接访问(但是hours
方法,也是继承的,可以访问它,因为它是基类的一部分)。
答案 0 :(得分:0)
如果我理解正确你需要在hierarhy中再引入一个课程。例如
struct Employee : Person
{
double salary;
};