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

实验5

作者:互联网

#include <iostream>
#include <string>
using namespace std;


class MachinePets {
public:
    MachinePets(const string s) :nickname(s) {}
    ;
    string getNickname();
    virtual string talk() = 0;
private:
    string nickname;
};


string MachinePets::getNickname() {
    return nickname;
}


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


string PetCats::talk() {
    return "miao wu~";

}

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

};


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


void play(MachinePets* p) {
    cout << p->getNickname();
    cout << " says " << p->talk() << endl;

}

// 主函数
int main() {
    PetCats cat("miku");
    PetDogs dog("da huang");
    play(&cat); // 按照play()形参,传递参数
    play(&dog); // 按照play()形参,传递参数
    return 0;
}
View Code

 

 

 

标签:const,string,getNickname,实验,MachinePets,public,talk
来源: https://www.cnblogs.com/elise00/p/10970847.html