有如下程序: #include #include using namespace std; class Animal{ public: virtual string

admin2021-05-06  28

问题 有如下程序:
       #include
       #include
       using namespace std;
       class Animal{
       public:
           virtual string GetType() const { return "Animal"; }
           virtual string GetVoice() const { return "Voice"; }
       };
       class Dog:public Animal{
       public:
           string GetType() const { return "Dog"; }
           string GetVoice() const { return "Woof"; }
       };
       class Cat:public Animal{
       public:
           string GetType() const { return "Cat"; }
           string GetVoice() const { return "Miaow"; }
       };
       void Type(Animal a) { cout<       void Speak(Animal a) { cout<       int main() {
           Dog d; Type(d); cout<<" speak "; Speak(d); cout<<" - ";
           Cat c; Type(c); cout<<" speak "; Speak(c); cout<           return 0;
       }
运行时的输出结果是

选项 A、Dog speak Voice - Cat speak Voice
B、Dog speak Woof - Cat speak Miaow
C、Animal speak Voice - Animal speak Voice
D、Animal speak Woof - Animal speak Miaow

答案C

解析 本题考查虚函数的运用,本题中定义Dog d; Type(d)时,执行基类的Type函数,输出Animal,然后输出speak,然后执行基类的Speak函数输出Voice,最后输出-,同理cat输出类似,所以结果为C选项正确。
转载请注明原文地址:https://kaotiyun.com/show/xGfp777K
0

最新回复(0)