实验五
2019-06-01 17:42 Chirly 阅读(120) 评论(1) 编辑 收藏 举报
#include <iostream> #include <string> using namespace std; class MachinePets{ public: MachinePets(const string s):nickname(s){}; string getNickname() {return nickname;}; virtual string talk()=0; private: string nickname; }; class PetCats:public MachinePets{ public: PetCats(const string s):MachinePets(s){}; string talk() {return "miao wu~";} }; class PetDogs:public MachinePets{ public: PetDogs(const string s):MachinePets(s){} string talk() {return "wang wang~";}; }; void play(MachinePets *p){ cout<<p->getNickname()<<"says"<<p->talk()<<endl; } int main() { PetCats cat("miku"); PetDogs dog("da huang"); play(&cat); // 按照play()形参,传递参数 play(&dog); // 按照play()形参,传递参数 return 0; }
运行结果: