一个派生类可以继承多个基类
语法:
class 派生类:继承方式 基类1,继承方式 基类2
{
}
构造函数:先基类,再派生类 。
析构函数:先派生类,再基类。
多个基类的调用跟基类继承的顺序有关
#include <iostream>
using namespace std;
class Test
{
public:
Test()
{
cout << "Test()"<<endl;
}
~Test()
{
cout << "~Test()"<<endl;
}
};
class Base1
{
public:
Base1()
{
cout << "Base1()" << endl;
}
~Base1()
{
cout << "~Base1()" << endl;
}
Test t;
};
class Base2
{
public:
Base2()
{
cout << "Base2()" << endl;
}
~Base2()
{
cout << "~Base2()" << endl;
}
};
class Derived:public Base1,public Base2
{
public:
Derived():Base1(),Base2()
{
cout << "Derived()"<<endl;
}
~Derived()
{
cout << "~Derived()"<<endl;
}
};
int main()
{
Derived d;
/*
Base1()
Base2()
Derived()
~Derived()
~Base2()
~Base1()
*/
return 0;
}