1。建立对象时所指定的类称为最远派生类。
2。虚基类的成员是由最远派生类的构造函数通过调用虚基类的构造函数进行初始化的。
3。在整个继承结构中,直接或间接继承虚基类的所有派生类,都必须在构造行数的成员
初始化表中给出对虚基类的构造函数的调用。如果未列出,则表示调用该虚基类的缺省构造函数。
4。在建立对象时,只有最远派生类的构造函数调用虚基类的构造函数,该派生类的其他基类对虚基类
构造函数的调用被忽略。
#include<iostream>
using namespace std;
class B0
{
int nV;
public:
B0(int n)
{
nV=n;
cout<<"Member of B0"<<endl;
}
void fun(){cout<<"fun of B0 be called"<<endl;}
};
class B1:virtual public B0
{
int nV1;
public:
B1(int a,int b1):B0(a)
{
nV1=b1;
cout<<"Member of B1 be called"<<endl;
}
};
class B2:virtual public B0
{
int nV2;
public:
B2(int a,int b2):B0(a)
{
nV2=b2;
cout<<"Member of B2 be called"<<endl;
}
};
class D1:public B1,public B2
{
int nVd;
public:
D1(int a,int b1,int b2,int b ):B0(a),B1(a,b1),B2(a,b2)
{
nVd=b;
cout<<"Meber of D1 be called "<<endl;
}
void fund()
{
cout<<"fund of D1"<<endl;
}
};
int main()
{
D1 d1(1,2,3,4);
d1.fun();
d1.fund();
return 0;
}