实验五

#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(&dog); 
   return 0;
}

  

 

posted @ 2019-06-01 22:58  小眉毛  阅读(105)  评论(2编辑  收藏  举报