首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #include #include using namespace std; class Instrument{ public: Instrument
有如下程序: #include #include using namespace std; class Instrument{ public: Instrument
admin
2021-05-06
52
问题
有如下程序:
#include
#include
using namespace std;
class Instrument{
public:
Instrument(string t="乐器",string n="无名"):type(t),name(n) { }
virtual string GetType() const { return "乐器"; }
string GetName() const { return "无名"; }
protected:
string type,name;
};
class Piano:public Instrument{
public:
Piano(string n,string t="钢琴"):Instrument(t,n) { }
string GetType() const { return "钢琴"; }
string GetName() const { return name; }
};
int main(){
Instrument *pi=new Piano("星空");
cout<
GetType()<<’-’<
GetName();
delete pi;
return 0;
}
运行时的输出结果是
选项
A、乐器-星空
B、乐器-无名
C、钢琴-星空
D、钢琴-无名
答案
D
解析
本题考查虚函数的运用,本题中基类Instrument,派生类Piano,其中虚函数为GetType,当定义Instrument *pi=new Piano("星空")时,调用派生类的GetType函数,得到type为钢琴,执行GetName时只能执行基类的这个函数,输出无名,所以输出钢琴-无名。选项D正确。
转载请注明原文地址:https://kaotiyun.com/show/HFfp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列描述中,错误的是()。
在数据库设计中,将E-R图转换成关系数据模型的过程属于()。
使用VC6打开考生文件夹下的源程序文件modi3.cpp,通过把类Distance定义为类Point的友元类,来实现计算两点之间距离的功能。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。(1)把类CDistance定义为类CPoi
使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成以下函数:intfactorial(intn):求出n的阶乘,必须使用递归调用。如果n小于1则返回0。注意:不能修改函数的其他部分。1#include<
下列循环语句中有语法错误的是()。
下列选项中,正确的C++标识符是()。
某个工厂有若干个仓库,每个仓库存放有不同的零件,相同零件可能放在不同的仓库中。则实体仓库和零件间的联系是
下面属于应用软件的是()。
在一个容量为15的循环队列中,若头指针front=6,尾指针rear=9,则循环队列中的元素个数为
以下程序中调用cin函数给变量a输入数值的方法是错误的,其错误原因是()。#includewildmajn(){in!*p,*q,a,h;p=&a;coutp;
随机试题
风热感冒取大椎、曲池、合谷、外关等穴浅刺疾出以清热解表的针灸治疗原则是
“肾为气之根”主要指
引起急性梗阻性化脓性胆管炎最常见的因素是
在可摘局部义齿支架弯制中,属于无卡环体,环抱稳定作用较差的卡环是A.锻丝下返卡环B.锻丝上返卡环C.长臂卡环D.固定卡环E.连续卡环
下列药物中,善于疏解半表半里之邪又有和解退热之功的是()。
A.鸡血藤B.丹参C.益母草D.桃仁E.莪术治疗血瘀而兼便秘,宜用
教育心理学研究认为,焦虑水平与学习效率之间的关系是______。
《华阳国志》
以经理办公室的名义,按一定的格式和要求,写一份于2012年7月28日公布的通知,包括以下内容:1.时间:8月7日星期四下午2:002.会议地点:公司办公大楼会议厅3.会议内容:讨论能源规划(energyprogram)
Studiesoffriendshipseemtoimplicatemorecomplexfactors.Forexample,onefunctionfriend-shipseemstofulfillis【C1】____
最新回复
(
0
)