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

实验五

作者:互联网

#include<string>
#include<iostream>
using namespace std;
class MachinePets {
public:
    MachinePets(const string s);
    virtual string talk()=0;
    string getNickname() {
        return nickname;
    }
private:
    string nickname;
};

class PetCats:public MachinePets{
 public:
    PetCats(const string s);
    string talk();

};

class PetDogs :public MachinePets {
public:
    PetDogs(const string s);
    string talk();
};

MachinePets::MachinePets(const string s) :nickname(s) {
}

PetCats::PetCats(const string s):MachinePets(s) {
}
string PetCats::talk()
{
    string a = "miao wu~";
    return a;
}
PetDogs::PetDogs(const string s) :MachinePets(s) {
}

string PetDogs::talk()
{
    string a = "wang wang~";
    return a;
}

void play(MachinePets *p)
{
    cout << p->getNickname() << "says" << p->talk() << endl;
}
int main() {
    PetCats cat("miku");
    PetDogs dog("da huang");
    play(&cat);
    play(&dog);
    system("pause");
    return 0;
}
View Code

 

标签:PetDogs,const,string,实验,MachinePets,public,talk
来源: https://www.cnblogs.com/hongzai1206/p/10972847.html