首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
若有以下程序: #include<iostream> using namespace std; class Base { public: Base() { x=0; }
若有以下程序: #include<iostream> using namespace std; class Base { public: Base() { x=0; }
admin
2009-02-15
25
问题
若有以下程序:
#include<iostream>
using namespace std;
class Base
{
public:
Base()
{
x=0;
}
int x;
};
class Derived1:virtual public Base
{
public:
Derived1()
{
x=10;
}
};
class Derived2:virtual public Base
{
public:
Derived2()
{
x=20;
}
};
class Derived:public Derived1, protected Derived2
{ };
int main()
{
Derived obj;
cout<<obj.x<<end1;
return 0;
}
该程序运行后的输出结果是【 】。
选项
答案
20
解析
本题考核虚基类。本题中,虽然Derived1和Derived2都是由共同的基类x派生而来的,但由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derived1修改,还是在类Derived2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derived obj”时,就会先调用虚基类 Base的构造函数,使得x=0,然后执行类Derived1的构造函数使得x=10,再执行类 Derived2的构造函数,使得x=20。最后输出x的值为20。
转载请注明原文地址:https://kaotiyun.com/show/pzjp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下面的函数调用语句中,func函数的实参个数是()。func(f2(v1,v2),(v3,v4,v5),(v6,max(v7,v8)))
以下选项中关于程序模块化的叙述错误的是()。
C语言规定,在一个c程序中,main()函数的位置()。
设有说Nint(*ptr)[M];其中的标识符ptr是()。
以下标识符不是关键字的是()。
一棵二叉树共有25个结点,其中5个是叶子结点,则度为1的结点数为
下面不属于结构化程序设计原则的是
下列选项中不属于结构化程序设计原则的是
层次型、网状型和关系型数据库划分原则是
随机试题
使用乙炔气瓶时,环境温度不应超过()。
A.短效巴比妥类B.苯巴比妥C.水杨酸类D.甲醇E.锂血液灌流可清除
淋巴瘤最重要的临床特征是
在护理健康教育中,下列哪一种不属于知识灌输方法()。
[2010年,第99题]将二进制小数0.1010101111转换成相应的八进制数,其正确结果是()。
下列关于多功能桥式起重机安全措施的说法,错误的是()。
青海玉树地震期间。干警王伟同志不顾亲人的遇难而去救别人。你是怎么看待的?
[2016年]已知函数f(x,y)=,则().
(2014下项管)在编制项目沟通计划的过程中,对项目干系人分析的目的不包括______。
上网查询资料时,目前最好用的搜索引擎是
最新回复
(
0
)