单选题
有如下程序:
#include<iostream>
using namespace std;
class Base{
public:
virtual void Show(){cout<<"B";}
};
class Derived:public Base{
public:
void Show(){cout<<"D";}
};
int main(){
Base *p1=new Derived;
Derived *p2=new Derived;
p1->Show();
p2->Show();
delete p1;
delete p2;
return 0;
}
运行这个程序的输出结果是______。
【正确答案】
D
【答案解析】[解析] 在基类Base中定义了虚函数Show()输出字符‘B’,而在派生类Derived中对虚函数Show()进行了重新定义,输出字符‘D’。在主函数中对基类和派生类中的虚函数Show()进行了多态调用,此时调用的是派生类中重新定义的Show(),输出字符‘D’。