class Parent{
public:
Parent(string a, int i, int j, int k): name(a), num1(i), num2(j), num3(k){}
~Parent(){}
...
//other member functions
...
protected:
string a;
int num1, num2, num3;
};
class Child : public Parent{
public:
Child(string a, int i, int j): Parent(???){} //how should I initialise the base class?
~Child(){}
....
//other member funcitions
....
}
在上面的类中,父级获得的数据成员多于子级,但许多成员函数将从父级继承。我不知道是否有办法调用父构造函数,它具有比子构造函数更多的参数。
答案 0 :(得分:3)
Child
可以提供其他参数所需的任何值,例如:
class Child : public Parent {
public:
Child(string a, int i, int j): Parent(a, i, j, 12345){}
...
};