类继承直接基类与间接基类规则

1、C++中继承可以是多层次的
(1)类继承直接基类与间接基类规则如下:

  • 在声明派生类时,只需要列出它的直接基类
  • 派生类沿着类的层次自动向上继承它的间接基类

(2) 派生类的成员包括:

  • 派生类自己定义的成员

  • 直接基类中的所有成员

  • 所有间接基类的全部成员

2、举栗子

class MoreDerived:public Derived {
public:
	MoreDerived():Derived(4) {
		cout << "More Derived constructed" << endl;
	}
	~MoreDerived() {
		cout << "More Derived destructed" << endl;
	}
};
int main()
{
	MoreDerived Obj;
	return 0;
}
/*输出结果:
Base 4 constructed
Derived constructed
More Derived constructed
More Derived destructed
Derived destructed
Base 4 destructed
*/
posted @ 2019-12-05 11:27  江南又一春  阅读(623)  评论(0编辑  收藏  举报