思维导图
练习
全局变量,int monster = 10000;定义英雄类hero,受保护的属性string name,int hp,int attck;公有的无参构造,有参构造,虚成员函数 void Atk(){blood-=0;},法师类继承自英雄类,私有属性 int ap_atk=50;重写虚成员函数void Atk(){blood-=(attck+ap_atk);};射手类继承自英雄类,私有属性 int ac_atk = 100;重写虚成员函数void Atk(){blood-=(attck+ac_atk);}实例化类对象,判断怪物何时被杀死。
#include <iostream>
using namespace std;
static int blood = 10000;
class hero
{
protected:
string name;
int hp;
int attack;
public:
hero(){}
hero(string name,int hp,int attack):name(name),hp(hp),attack(attack){}
virtual void Atk()
{
blood -= 0;
}
};
class master:public hero
{
int ap_atk = 50;
public:
master(){}
master(string name,int hp,int attack):hero(name,hp,attack){}
void Atk()
{
blood -= attack + ap_atk;
cout << "Master Attack:-" << attack + ap_atk;
}
};
class shooter:public hero
{
int ac_atk = 100;
public:
shooter(){}
shooter(string name,int hp,int attack):hero(name,hp,attack){}
void Atk()
{
blood -= attack + ac_atk;
cout << "Shooter Attack:-" << attack + ac_atk;
}
};
void monster(hero &p)
{
p.Atk();
}
int main()
{
master h1("zzz",2000,2100);
shooter h2("xxx",3000,1125);
cout << "Monster HP:" << blood << endl;
while(blood > 0)
{
monster(h1);
if(blood <= 0)
break;
cout << " Monster HP:" << blood << endl;
monster(h2);
if(blood <= 0)
break;
cout << " Monster HP:" << blood << endl;
}
cout << " Monster Killed" << endl;
return 0;
}