首页 > 其他 > 详细

实验五

时间:2019-05-29 22:11:30      阅读:133      评论:0      收藏:0      [点我收藏+]

#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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!