1、阅读下面的程序,并写出运行结果
(3)纯虚函数
#include <iostream>
using namespace std;
class Base
{
public:
virtual void Who() =0;
};
class FirstDerived:public Base
{
public:
void Who() { cout<<"F"; }
};
class SecondDerived:public Base
{
public:
void Who() { cout<<"S"; }
};
int main()
{
FirstDerived first_obj;
SecondDerived second_obj;
Base &bref=first_obj;
bref.Who();
bref=second_obj;
bref.Who();
Base *bp;
bp=&first_obj;
bp->Who();
bp=&second_obj;
bp->Who();
return 0;
}
预计运行结果:FSFS
实际运行结果:
错误分析:Base &bref=first_obj;这句话意思是bref为first_obj的引用,而bref=second_obj;是用second_obj为first_obj赋值然而并没有数据成员所以并没有什么乱用。所以输出FFFS。