多次继承下,最高层的子类的构造函数不需要写父类以上的构造函数

和虚拟继承不一样,虚拟继承需要些虚基类的构造函数。

虚拟继承构造函数写法:http://www.cnblogs.com/vhyc/p/5582450.html

class A{
protected:
	int a;
public:
	A(int x) :a(x){ cout << "a" << endl; };
};

class B :public A
{
protected:
	int b;
public:
	B(int x, int y) :A(x), b(y){ cout << "b" << endl; }
};

class C :public B
{
private:
	int c;
public:
	C(int x, int y, int z) :B(x, y), c(z){ cout << "c" << endl; }//和虚拟继承不一样,不能写A(x),否则编译就不过关
	void display()
	{
		cout << a << endl << b << endl << c << endl;
	}
};

int main()
{
	C a(1, 2, 3);
	a.display();
}

  

posted on 2016-06-14 17:35  Kooing  阅读(159)  评论(0编辑  收藏  举报

导航