C++ 多层继承
多层继承
一个类也可以从另一个已经派生自另一个类的类派生。
在下面的示例中,MyGrandChild
从类 MyChild
派生(MyChild
又从 MyClass
派生)。
示例
// 基类(父类)
class MyClass {
public
void myFunction() {
cout << "Some content in parent class." ;
}
};
// 派生类(子类)
class MyChild: public MyClass {
};
// 派生类(孙子类)
class MyGrandChild: public MyChild {
};
int main() {
MyGrandChild myObj;
myObj.myFunction();
return 0;
}
自己动手试一试 »