预测以下程序的输出
#include
using namespace std;
class Base
{
public:
virtual void show() { cout<<" In Base \n"; }
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived \n"; }
};
int main(void)
{
Base *bp = new Derived;
bp->show();
Base &br = *bp;
br.show();
return 0;
}
(一种)
In Base
In Base
(B)
In Base
In Derived
(C)
In Derived
In Derived
(D)
In Derived
In Base
答案: (C)
说明:由于show()在基类中是虚拟的,因此将根据要引用或指向的对象的类型而不是指针或引用的类型来调用它。
这个问题的测验
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。