有如下程序: #include<iostream> #include<string> using namespace std; class Instrument{ public: InStrument(s

admin2020-06-29  11

问题 有如下程序:
    #include<iostream>
    #include<string>
    using namespace std;
    class Instrument{
    public:  
    InStrument(string t=”乐器”,string n=”无名”):type(t),Dame(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{retum name;}
    };
    int main(){
    Imtnnnent*pi=new Piano(”星空”);
    cout<<pi一>GetType()<<’一’<<pi一>GetName();
    delete pi;
    return 0;
    }
    运行时的输出结果是(    )。

选项 A、钢琴一星空
B、钢琴一无名
C、乐器一星空
D、乐器一无名

答案A

解析 本题考查虚函数的运用,本题中基类Instrument,派生类Piano,其中虚函数为GetFype,当定义Instrument*pi=new Piano(”星空”)时,调用派生类的GetType函数,得到type为钢琴,name为星空,所以输出钢琴一星空。选项A正确。
转载请注明原文地址:https://kaotiyun.com/show/pi8p777K
0

最新回复(0)