#include<iostream> #include<string> using namespace std; class MachinePets{ public: MachinePets(){} MachinePets(const string s){ nikname=s; } string getNikname()const{return nikname;} virtual string talk(){return 0;} private: string nikname; }; class PetCats:public MachinePets{ public: PetCats(const string s):MachinePets(s){} string talk(){return "wu~"; } private: string nikname; }; class PetDogs:public MachinePets{ public: PetDogs(const string s):MachinePets(s){} string talk(){return "wang~"; } private: string nikname; }; void play(MachinePets * p){ cout<<p->getNikname()<<" "<<"says"<<" "<<p->talk()<<endl; } int main(){ PetCats cat("miku"); PetDogs dog("da huang"); play(&cat); play(&dog); return 0; }
原文:https://www.cnblogs.com/qiuxiuh/p/10970859.html