问题 填空题

有如下程序:
#include<iostream>
using namespaee std;c lass Animal
public:
virtual char*getType( )constreturn"Animal";
virtual char*getVoice( )constreturn"Voice";

class Dog:public Animal
public:
char*getType( )constreturn"Dog";
char*getVoice( )eonstreturn"Woof";

void type(Animal&A) cout<<a.getType( );
void speak(Animal A) eout<<a.getVoice( );
int main( )
Dog d;type(D) ;cout<<"speak";speak(D) ;cout< return 0;

程序的输出结果是______。

答案

参考答案:Dog SpeakVoice

解析: 派生类继承基类,并重新定义了基类的虚函数。void type(Animal&A) 是对象引用作为函数参数,传递的是地址,是“地址调用”,故a.getType( )调用的是派生类重新定义后的get—Type( )成员函数。void speak(Animal A) 是对象作为函数参数,是“传值调用”,在进行函数调用时,将派生类对象赋值给基类对象。但是,对象a只能调用派生类继承自基类的成员。故在a.getVoice( )中调用的是基类的虚函数getVoice( ),打印“Voive”。

选择题
填空题