C++day5

思维导图

C语言练习

练习题 

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <unistd.h>
 5 #include <sstream>
 6 #include <vector>
 7 #include <memory>
 8 
 9 using namespace std;
10 
11 enum TYPE{
12     A,
13     B,
14     C,
15     D
16 };
17 //武器类
18 class Weapon{
19 private:
20     int atk=10;
21     enum TYPE type=A;
22 public:
23     Weapon(){
24         cout<<"武器"<<endl;
25     }
26     virtual void set(int* t){*t+=this->atk;}
27     virtual int get(){return type;}
28 };
29 //长剑类
30 class Sword:public Weapon{
31 private:
32     int hp=100;
33     enum TYPE type=B;
34 public:
35     Sword(){
36         cout<<"长剑类"<<endl;
37     }
38     void set(int* t){*t+=this->hp;}
39     int get(){return type;}
40 };
41 //短剑类
42 class Blade:public Weapon{
43 private:
44     int spd=10;
45     enum TYPE type=C;
46 public:
47     Blade(){
48         cout<<"短剑类"<<endl;
49     }
50     void set(int* t){*t+=this->spd;}
51     int get(){return type;}
52 };
53 //斧头类
54 class Axe:public Weapon{
55 private:
56     int def=10;
57     enum TYPE type=D;
58 public:
59     Axe(){
60         cout<<"斧头类"<<endl;
61     }
62     void set(int* t){*t+=this->def;}
63     int get(){return type;}
64 };
65 //英雄类
66 class Hero{
67 private:
68     int atk=0;
69     int hp=0;
70     int spd=0;
71     int def=0;
72 public:
73     void equipWeapon(Weapon* p){
74         if(p->get()==A)
75         {
76             int* t=&atk;
77             p->set(t);
78         }
79         if(p->get()==B)
80         {
81             int* t=&hp;
82             p->set(t);
83         }
84         if(p->get()==C)
85         {
86             int* t=&spd;
87             p->set(t);
88         }
89         if(p->get()==D)
90         {
91             int* t=&def;
92             p->set(t);
93         }
94         //int* t=&hp;
95         //p->set(t);
96     }
97     void show(){
98         cout<<"atk="<<atk<<endl;
99         cout<<"hp="<<hp<<endl;
00         cout<<"spd="<<spd<<endl;
01         cout<<"def="<<def<<endl;
02     }
03 };
04 int main(int argc,const char** argv){
05     Hero doro;
06     Sword s;
07     Blade b;                                                              
08     Axe a;
09     Weapon* w1=&s;
10     Weapon* w2=&b;
11     Weapon* w3=&a;
12 
13     doro.equipWeapon(w1);
14     doro.show();
15     doro.equipWeapon(w2);
16     doro.show();
17     return 0;
18 }
19 

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值