虚基类及其派生类的构造函数

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;
		
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值