首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
若有以下程序: #include <iostream> using namespace std; class Base { public: Base () { x=0; }
若有以下程序: #include <iostream> using namespace std; class Base { public: Base () { x=0; }
admin
2010-06-06
15
问题
若有以下程序: #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 obi; cout<<obj.x<<endl; return 0; } 该程序运行后的输出结果是
选项
A、20
B、30
C、10
D、0
答案
A
解析
本题考核虚基类的应用。本题中,虽然Derived1和Derived2都是由共同的基类x派生而来的,但由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derived1中修改,还是在类Derived2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derived obj;”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derived1的构造函数使得x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。
转载请注明原文地址:https://kaotiyun.com/show/BSjp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
若用数组名作为函数调用时的实参,则实际上传递给形参的是()。
以下程序:#include#includemain(){charstr[]="abcd\n\123\xab";printf("%d",strlen(str));}运行后的输出结果是
if语句的基本形式为:if(表达式)语句,其中“表达式"()。
定义如下变量和数组:inti,x[3][3]={1,2,3,4,5,6,7,8,9};则下面语句的输出结果是()。for(i=0;i
请编写函数fun,其功能是:判断t所指字符串中的字母是否由连续递增字母序列组成(字符串长度大于等于2)。例如字符串:uvwxyz满足要求;而字符串:uvxwyz不满足要求。注意:部分源程序存放在PROGl.C中,请勿改动主函数main和其他函数中的任何内
给定程序中,函数fun的功能是:利用指针数组对形参SS所指字符串数组中的字符串按由长到短的顺序排序,并输出排序结果。ss所指字符串数组中共有N个字符串,且串长小于M。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。注意:源程序存放
请阅读以下程序:#include<stdio.h>#include<string.h>voidfun(intb[])fstaticinti=0;d0{b[i]+=b[i+1];}while(++i<2);)main(){intk
以下函数实现按每行8个输出w所指数组中的数据:#includevoidfun(int:*w,intn){inti;for(i=0;i
使用VC++2010打开考生文件夹下blank1中的解决方案。此解决方案的项目中包含一个源程序文件blank1.c。此程序定义了学生结构体变量,存储了学生的学号、姓名和三门课的成绩。所有学生数据均以二进制方式输出到文件中。函数fun的功能是从形参filen
设有定义:doublea,b,c;若要求通过输入分别给a、b、c输入1、2、3,输入形式如下(注:此处□代表一个空格)□□1.0□□2.0□□3.0<回车>则能进行正确输入的语句是
随机试题
下列句中加横线的词语属于意动用法的是()
患者,女,24岁,人工流产术后1周,突然阴道出血增多,伴腹痛,无发热;查体:子宫稍大,质软,压痛,双侧附件区未扪及异常。该患者目前最应该进行的操作是
治疗抽动秽语综合征可选用的药物包括
A.处3年以下有期徒刑、拘役、管制或罚金B.处3年至7年有期徒刑,并处罚金C.处2年至7年有期徒刑,并处销售金额50%至2倍罚金D.处3年以上10年以下有期徒刑,并处罚金E.处3年以下有期徒刑、拘役管制或剥夺政治权利在自然灾害和突发事件
根据《污水综合排放标准》,属于第一类污染物的是()。
基坑在初期排水时,排水设备应选择()离心式水泵。
为了贯彻执行《中华人民共和国计量法》和《中华人民共和国强制检定的工作计量器具检定管理办法》,切实做好强制检定工作,某区人民政府批转区标准计量局提出了《关于强制检定的工作计量器具分期分批实施检定的报告的通知》。无论是强制检定,还是非强制检定,它们检定的依
当备选供货商有很大不同时,做出错误决策的概率很高,采购者一旦选错,会造成严重后果。
确定和调整最低工资标准应考虑的因素包括()
楚某1997年3月5日犯甲罪,追诉期限应为10年,2002年3月5日又犯乙罪,乙罪的追诉期限也是10年。这时甲罪的追诉期限应()。
最新回复
(
0
)