多继承

#include <iostream>
using namespace std;

class OBJ1
{
public:
	OBJ1()
	{
		cout<<"construct OBJ1"<<endl;
	}
};

class OBJ2
{
public:
	OBJ2()
	{
		cout<<"construct OBJ2"<<endl;
	}
};

class Base1
{
public:
	Base1()
	{
		cout<<"construct Base1"<<endl;
	}
};

class Base2
{
public:
	Base2()
	{
		cout<<"construct Base2"<<endl;
	}
};


class Base3
{
public:
	Base3()
	{
		cout<<"construct Base3"<<endl;
	}
};

class Base4
{
public:
	Base4()
	{
		cout<<"construct Base4"<<endl;
	}
};

/*
多继承的构造顺序:
1,按照父类被继承的顺序先构造虚拟基类,再构造非虚拟基类
2,成员对象按照声明时的顺序被定义,对于组合的其他类的对象调用构造函数
3,类自己的构造函数
*/
class Derived : public Base1, virtual public Base2,
				public Base3, virtual public Base4
{
public:
	Derived() : Base4(),Base3(),Base2(),Base1(),obj2(),obj1()
	{
		cout<<"Derived ok."<<endl;
	}
protected:
	OBJ1 obj1;
	OBJ2 obj2;
};


void main()
{
	Derived d;
	cout<<"this is ok."<<endl;
}
posted @ 2010-10-14 13:47  Cranny  阅读(193)  评论(0编辑  收藏  举报