首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #include #include using namespace std; class Instrument{ public: Instrument
有如下程序: #include #include using namespace std; class Instrument{ public: Instrument
admin
2021-09-05
35
问题
有如下程序:
#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://www.kaotiyun.com/show/db5p777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下程序:#includeusingnamespacestd;classA{public:A(inti):rl(i){}voidprint(){cout
有如下定义:inta[5]={1,3,5,7,9},*p=a;下列表达式中不能得到数值5的是()。
下列关于C++函数的说明中,正确的是()。
在下列原型所示的C++函数中,按“传值”方式传递参数的是()。
下列有关C++流的叙述中,错误的是()。
下列关于派生类构造函数和析构函数的说法中,错误的是()。
下列关于运算符重载的叙述中,错误的是()。
有两个关系R和S如下:则由关系R得到关系S的操作是()。
软件测试的目的是( )。
随机试题
门腔分流术存在的主要问题是【】
(Either)thebrother(or)thesisterwilltell(their)fatherabout(whathashappened).
医院药学信息服务的出版发行工作包括编写发行本医院的
以下体表标志正确的是
凡从事生产、经营的纳税人应自领取营业执照或发生纳税义务之日起30日内按照国家有关规定设置账簿。()
教师为履行职责所必需的权利为()。
甲、乙两汽车从相距695公里的两地出发,相向而行,乙汽车比甲汽车迟2个小时出发,甲汽车每小时行驶55公里,若乙汽车出发后5小时与甲汽车相遇,则乙汽车每小时行驶().
下列叙述中,错误的是
TaskOne-Job•Forquestions13-17,matchtheextractswiththepeople,listedA-H.•Foreachextract,choosethejobeachsp
【B1】【B9】
最新回复
(
0
)