#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; }
运行结果:
时间: 2024-10-09 21:21:28