#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; }
运行结果:
原文地址:http://blog.csdn.net/ljd939952281/article/details/46314007