有如下程序: #include #include using namespace std; class Instrument{ public: Instrument

admin2020-06-29  22

问题 有如下程序:
       #include
       #include
       using namespace std;
       class Instrument{
       public:
           Instrument(string t="乐器",string n="无名"):type(t),name(n) { }
           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、钢琴-无名

答案B

解析 本题考查派生类的应用,本题中基类Instrument,派生类Piano,当定义Instrument*pi=newPiano("星空")时,pi为指向基类的指针,那么执行pi->GetType()时调用基类的GetType函数,得到type为乐器,name为无名,所以输出乐器-无名。选项B正确。
转载请注明原文地址:https://jikaoti.com/ti/Ael0FFFM
0

最新回复(0)