实验五:类的继承、派生和多态2

/*问题描述*/

设计并实现一个机器宠物类MachinePets。 每个机器宠物有如下信息:昵称(nickname) 每个机器宠物有如下成员函数: 带参数的构造函数MachinePets(const string s) ,为机器宠物初始化昵称。 纯虚函数string talk()为机器宠物派生类提供宠物叫声的统一接口。
设计并实现电子宠物猫类PetCats,该类公有继承自MachinePets。每个电子宠物猫类有如下成员函数: 带参数的构造函数PetCats(const string s),为机器宠物猫初始化昵称。 string talk(),返回电子宠物猫叫声。 设计并实现电子宠物狗类PetDogs, 该类公有继承自MachinePets。每个电子宠物狗类有如下成员函数:
带参数的构造函数PetCats(const string s),为机器宠物狗初始化昵称。 string talk(),返回电子宠物狗叫声。

/*代码如下*/

 1 #include "pch.h"
 2 #include <iostream>
 3 #include<string>
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 7 using std::string;//引用程序所需的函数标准声明,便于在型程序出错时找错,养成良好习惯。
 8 
 9 class machine_pets {//电子宠物类
10 public:
11     machine_pets(){}
12     machine_pets(const string str) {
13         nickname = str;
14     }
15     virtual void talk() {
16     }
17     void getnickname(string s) {
18         nickname = s;
19     }
20 private:
21     string nickname;
22 };
23 
24 class pet_cats:public machine_pets {//公有继承自电子宠物类的电子猫类
25 public:
26     pet_cats(const string name0){
27         name = name0;
28     }
29     void talk() {
30         cout << name << " says : " << words << endl;
31     }
32 private:
33     string name;
34     string words = "miao wu``";//此处可以通过参数自行设置宠物的声音
35 };
36 
37 class pet_dogs :public machine_pets {//公有继承自电子宠物类的电子狗类
38 public:
39     pet_dogs(const string name0) {
40         name = name0;
41     }
42     void talk() {
43         cout << name << " says : " << words << endl;
44     }
45 private:
46     string name;
47     string words = "wang wang``";
48 };
49 
50 void play(machine_pets *pets) {//公共接口函数,用于发出叫声,
51     pets->talk();
52 }
53 
54 int main() {
55     pet_cats cat("hello kitty");
56     pet_dogs dog("superdog");
57 
58     play(&cat);
59     play(&dog);
60 
61     system("pause");
62     return 0;
63 }

/*运行截图*/

/*拓展部分*/

我们可以尝试通过音频调用的函数,以文件名作为参数等方法,来调用音频文件,使得宠物可以发出猫猫狗狗的叫声

这里给出一段参考性的代码

 1 #include <windows.h>
 2 #include<stdio.h>
 3 #include <mmsystem.h> 
 4 #pragma comment(lib, "winmm.lib")
 5  
 6 int main()
 7 {
 8        int mp3_alltime=0;
 9        mciSendString("open \"C:\\Users\\Administrator\\Desktop\\M2M Pretty Boy.wav\" alias mp3_music",NULL,0,NULL); //需要更改
10        //播放音乐
11        mciSendString("play mp3_music",NULL,0,NULL);
12        //获取播放时间
13        char sPosition[500];
14        mciSendString("status mp3_music length",sPosition,225,0);
15        mp3_alltime=atol(sPosition);
16        Sleep(mp3_alltime);
17        return 0;
18 }
how to talk

/*选做部分*/

展示一下运行的结果⑧

/*运行截图*/

/*简单的说明*/

补足程序后大概就是这么玩的。

1、创建角色,选择职业(这里暂时只实现了swordsman的代码,所以暂时只能选swordsman)

2、默认你的对手是什么;

3、展示你和你对手的状态,包括血量和魔法值(本题用了容器写了一个背包,里面放置你拥有的治疗药水和魔法药水)

4、战斗,你可以选择普攻,特殊攻击(有概率被闪避)或者使用药水来恢复自己,对手也一样,这是一个回合。进攻成功可以获得经验然后升级。

5、直至一方死亡,若你死亡,就game over,如果对手死亡,你可以获得他背包中剩下的物品,然后迎接下一位对手。

6、重复以上步骤。

/*自己的一些想法*/

1、再写一个金币加进去,每次击败对手可以获得金币,并且设置药水在商店中的价格,每次开局前可以购买药水。

2、加入金币的原因,战斗过程中如果对手用完了药水,那么面对第二个对手失败的可能性很大,有了金币可以增强持续战斗能力。

3、可以自由选择对手职业,不总是默认的。

4、......

/*再说一点⑧*/

弓箭手和法师还没有实现,我要先写作业啦,没办法,我太菜了,得笨鸟先飞。

我把作业写完再回来试着实现我的想法和另外两个职业。

posted @ 2019-05-28 23:26  爱因斯坦PLUS  阅读(279)  评论(2编辑  收藏  举报