首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
若有以下程序: #include <iostream> using namespace std; class Base { public: Base() { x=
若有以下程序: #include <iostream> using namespace std; class Base { public: Base() { x=
admin
2009-02-15
18
问题
若有以下程序: #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、20
B、30
C、10
D、0
答案
1
解析
本题考核虚基类的应用。本题中,虽然Derivedl和Derivec[2都是由共同的 基类x派生而来的,但由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derivedl中修改,还是在类Derivect2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derived obj;”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derivedl的构造函数使得 x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。
转载请注明原文地址:https://kaotiyun.com/show/O7kp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有以下程序:#includeintfun(){staticintx=1:x+=1;returnx;}main(){inti,s=1;
有以下程序:#include<stdio.h>main(){intx=1,y=0;if(!x)y++;elseif(x==0)if(x)y+=2;elsey+=3;printf("%d\n",y);}程序运行后的输出结果是()
设有定义:intx=2;,以下表达式中,值不为6的是()。
数据库系统的三级模式不包括()。
数据库设计中反映用户对数据要求的模式是()。
表示关系式x≤y≤Z的C语言表达式的是()。
uniondt{inta;charb;doublec;}data;以下叙述中错误的是()。
以下有关宏替换的叙述不正确的是()。
算法空间复杂度的度量方法是
以下选项中不属于C语言程序运算符的是
随机试题
在处方书写中,缩写词“Inj.”代表的含义是
墨菲征阳性常见于以下哪种疾病()
流行性乙型脑炎的炎症性质是
正常婴儿开始能独坐的年龄是
双曲拱桥进行技术状况评定时,主拱圈的构件按拱肋、拱波、横向联结系划分。()
下列边坡支护结构类型中适用于建筑土质边坡高度H≤15m的是()。
个别激励是指_______。
MajesticOceanCruiseLinesOffervalid:June-August2006Enjoya15-dayjourneyaboardtheDiamondCruiserasyouexplo
举一例子说明小学数学概念的形成过程。
已知函数试计算下列各题:S0=∫02f(x)e-xdx
最新回复
(
0
)