华侨大学面向对象编程期末考试题求解析

阅读以下程序并给出执行结果#include<iostream>usingnamespacestd;classBase{protected:intb;public:Base... 阅读以下程序并给出执行结果
#include <iostream>
using namespace std;
class Base
{
protected:
int b;
public:
Base(int bval=0)
{ cout<<"Constructing Base..."<<bval<<endl;
b=bval;
}
};
class Derive1: virtual public Base
{
protected:
int d1;
public:
Derive1(int bval, int dval):Base(bval)
{ cout<<"Constructing Derive1..."<<dval<<endl;
d1=dval;
}
};
class Derive2:virtual public Base
{
protected:
int d2;
public:
Derive2(int bval,int dval):Base(bval)
{ cout<<"Constructing Derive2..."<<dval<<endl;
d2=dval;
}
};
class Derive:public Derive1, public Derive2
{ protected:
int d;
public:
Derive(int bval, int dval1,int dval2,int dval);
};
Derive::Derive(int bval, int dval1,int dval2,int dval)
:Derive1(bval, dval1),Derive2(bval, dval2),Base(bval)
{
cout<<"Constructing Derive..."<<dval<<endl;
d=dval;
}
void main()
{
Derive Objectd(1,2,3,4);
}
答案:Constructing Base... 1
Constructing Derive1... 2
Constructing Derive2... 3
Constructing Derive... 4
请求高手或者有心得的人给下详细解析谢谢大家
展开
 我来答
FrozenSeason
2010-09-03 · TA获得超过347个赞
知道小有建树答主
回答量:109
采纳率:0%
帮助的人:154万
展开全部
还真有点难度 想了一会儿
构造的时候的顺序是 父类到子类
所以先是Constructing Base
然后由于Derive1,Derive2是虚继承
所以构造Derive1,Derive2时不会构造Base
最后才是Derive的构造函数
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式