单选题
有如下程序:
#include<iostream>
#include<string>
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:
stnng type,name;
};
class Piano:public Instrument{
public:
Piano(string n,string t="钢琴"):lnstrument(t,n){}
string CetType() const{return"钢琴";}
string GetName() const{return name;}
};
int main(){
Instrument * pi=new Piano("星空");
cout<<pi->GetType()<<"-"<<pi->GetName();
delete pi;
return 0;
}
运行时的输出结果是______。
【正确答案】
A
【答案解析】[解析] 本题考查虚函数的运用,本题中基类Instrument,派生类Piano,其中虚函数为GetType,当定义Instrument*pi=new Piano("星空")时,调用派生类的GetType函数,得到type为钢琴,name为星空,所以输出钢琴-星空。选项A正确。