其他分享
首页 > 其他分享> > 实验5

实验5

作者:互联网

#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;
}

 

 

标签:return,string,实验,MachinePets,const,public,talk
来源: https://www.cnblogs.com/shy0825/p/10969858.html