首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #include #include using namespace std; class Instrument{ public: Instrument
有如下程序: #include #include using namespace std; class Instrument{ public: Instrument
admin
2021-09-05
37
问题
有如下程序:
#include
#include
using namespace std;
class Instrument{
public:
Instrument(string t="乐器",string n="无名"):type(t),name(n) { }
virtual string GetType() const { return "乐器"; }
virtual 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、乐器-无名
答案
A
解析
本题考查虚函数的运用,本题中基类Instrument,派生类Piano,其中虚函数为GetType,当定义Instrument *pi=new Piano("星空")时,调用派生类的GetType函数,得到type为钢琴,name为星空,所以输出钢琴-星空。选项A正确。
转载请注明原文地址:https://kaotiyun.com/show/idfp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。(1)在类TestClass中定义name为字符串类型,age为整型,请在注释∥********1********之后添加语句。(2
有如下程序:#includeusingnamespacestd;inti=1;classFun{public:staticinti;intvalue(){returni-1;}intvalue()const{ret
有如下类定义:classPoint{private:staticinthow_many;};_________how_many=0;要初始化Point类的静态成员how_many,下画线处应
下列关于函数参数的叙述中,错误的是
下列情况中,不会调用拷贝构造函数的是()。
下列运算符中,可以重载为类的友元函数的运算符是
类的析构函数的作用是()。
分析下面程序,该程序的运行结果是()。#inc1ude<iostream.h>c1assTestClass{public:staticintm,TesK:lass(){
下列有关继承和派生的叙述中,正确的是()。
随机试题
GB15979—20024一次性使用卫生用品卫生标准》中规定,生产环境中空气采样应该在下列何种状态下进行
能引起胃肠型中毒的细菌有()
下列物质进入血管后都有可能形成栓塞,除外()。
《证券法》规定,只允许专门从事证券经纪业务的是()证券公司。
“备案号”栏应填写()。“起运国(地区》”栏应填写()。
下列关于医疗期及停工留薪期待遇的说法中正确的有()。
“奉献”作为职业道德规范,其对从业人员的基本要求是()。
从儿童听觉的发展看,新生儿听觉的特点是()。
某个单位和气团结,但是业绩平平,你怎么看?
在计算机网络中负责信息处理的部分称为()。
最新回复
(
0
)