首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
若有以下程序: #include <iostream> using namespace std; class Base { public: Base() { x=0;
若有以下程序: #include <iostream> using namespace std; class Base { public: Base() { x=0;
admin
2010-03-29
46
问题
若有以下程序: #include <iostream> using namespace std; class Base { public: Base() { x=0; } int x; }; class Derivedl : virtual public Base { public: Derivedl() { x=10; } }; class Derived2 : virtual public Base { public: Derived2() { x=20; } }; class Derived : public Derivedl,protected Derived2 { }; int main () { Derived obj; cout<<obj.x<<end1; return 0; } 该程序运行后的输出结果是( )。
选项
A、10
B、20
C、30
D、0
答案
2
解析
本题中,虽然Derived1和Derived2都是由共同的基类x派生而来的,但由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derived1修改,还是在类Derived2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derived obj”时,就会先调用虚基类Base的构造函数,使得x=O,然后执行类Derived1的构造函数使得x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。
转载请注明原文地址:https://jikaoti.com/ti/LmW0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有以下程序#include<stdio.h>main(){intb=10,a=-11;a%=b%=4;printf(’’%d%d\n’’,a,b);)则程序的输出结果是
有以下程序structS{inta,b;}data[2]={10,100,20,200};main(){structSP=data[1];printf("%d\n",++(p.a));}程序运行后的输出结果是
下面不属于结构化程序设计原则的是
软件设计中模块划分应遵循的准则是()。
以下选项中不能作为C语言合法常量的是
假定已建立以下链表结构,且指针p和q已指向如图所示的节点。则下列选项中可将q所指节点从链表中删除并释放该节点的语句组是()。
某二叉树共有12个结点,其中叶子结点只有1个。则该二叉树的深度为(根结点在第1层)
在快速排序法中,每经过一次数据交换(或移动)后()。
软件按功能可以分为:应用软件、系统软件和支撑软件(或工具软件)。下面属于应用软件的是()。
给定程序MODI1.C中,函数fun的功能是判断整数n是否是“完数”。当一个数的因子之和恰好等于这个数本身时,就称这个数为“完数”。例如:6的因子包括1、2、3,而6=1+2+3,所以6是完数。如果是完数,函数返回值为1,否则函数返回值为0。数组a中存放的
随机试题
男,70岁。发现大量蛋白尿2周入院。入院后查本周蛋白尿阳性。为明确诊断,检查意义最大的是
患牛,5岁,雨天滑倒后不愿起立,强行站立后患前肢肘关节外展,触诊疼痛,运动时未听到骨擦音,桡骨头向外突出,进一步确诊的适宜方法是
头痛而胀,甚则头胀如裂,恶风发热,面红耳赤,口渴喜饮,大便不畅,溲赤,舌尖红,苔薄黄,脉浮数。证属( )。
男性,25岁,因十二指肠溃疡急性穿孔行胃大部切除术,术后顺利恢复进食。第8天,在进半流食鸡蛋时,突然出现频繁呕吐,下列治疗中,错误的是
在项目经济评价中,对过去已经发生、在当前决策中可不予考虑的费用称为()。
下列关于评估客户投资风险承受度的表述中,错误的是()。
陶渊明、徐儒子、曾巩、黄庭坚分别被称为()。
影子银行
(2008下软设)在UML的各种视图中,______(1)显示外部参与者观察到的系统功能;______(2)从系统的静态结构和动态行为角度显示系统内部如何实现系统的功能;______(3)显示的是源代码以及实际执行代码的组织结构。(2)
A.--pianoB.--violinC.--celloD.--guitarE.--xylophoneF.--harpG.--oboeH.--clarinetI.--drumJ.--gongK.--cymbals
最新回复
(
0
)