有如下程序:
#include<iostream>
using namespace std;
class Animal{
public:
virtual char*getType( )const{ return "Animal";}
virtual char*getVoice( )const{ return "Voice";)
};
class Dog: public Animal{
public:
char*getType( )const{return "Dog";)
char*getVoice( )const{return "Woof";}
};
void type(Animal& a){ cout<<a.getType( );}
void sDeak(Animal a){ cout<<a.getVoice( );}
int main( ){
Dog d;type(d);cout<<"speak";speak(d);cout<<endl;
return 0;
}
运行时的输出结果是______。
参考答案:DogspeakVoice
解析:
在类Base中加了virtual关键字的函数就是虚拟函数,于是在Base的派生类Derived中就可以通过重写虚拟函数来实现对基类虚拟函数的覆盖,这是面向对象中的多态性的体现。