代码: #include iostreamusing 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(){ FirstDeri
代码:
#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;
}
运行结果:
FFFS
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
总结: