#include "pch.h"
#include <iostream>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::string;//引用程序所需的函数标准声明,便于在型程序出错时找错,养成良好习惯。
class machine_pets {//电子宠物类
public:
machine_pets(){}
machine_pets(const string str) {
nickname = str;
}
virtual void talk() {
}
void getnickname(string s) {
nickname = s;
}
private:
string nickname;
};
class pet_cats:public machine_pets {//公有继承自电子宠物类的电子猫类
public:
pet_cats(const string name0){
name = name0;
}
void talk() {
cout << name << " says : " << words << endl;
}
private:
string name;
string words = "miao wu``";//此处可以通过参数自行设置宠物的声音
};
class pet_dogs :public machine_pets {//公有继承自电子宠物类的电子狗类
public:
pet_dogs(const string name0) {
name = name0;
}
void talk() {
cout << name << " says : " << words << endl;
}
private:
string name;
string words = "wang wang``";
};
void play(machine_pets *pets) {//公共接口函数,用于发出叫声,
pets->talk();
}
int main() {
pet_cats cat("hello kitty");
pet_dogs dog("superdog");
play(&cat);
play(&dog);
system("pause");
return 0;
}
原文:https://www.cnblogs.com/aitxy899/p/10946251.html