1、全局变量,int monster = 10000;定义英雄类hero,受保护的属性string name,int hp,int attcKk;公有的无参构造,有参构造,虚成员函数void AtK()[blood-=0,},法师类继承自英雄类,私有属性int ap_atk=50;重写虚成员函数void AtKk()(blood-=-(attck+ap_atk);};射手类继承自英雄类,私有属性int ac_atk = 100;重写虚成员函数void AtK()blood-=(attck+ac_atk)}实例化类对象,判断怪物何时被杀死。
#include <iostream>
using namespace std;
int monster = 10000;
class Hero
{
private:
string name;
public:
int hp;
int attck;
Hero(){}
Hero(string _name, int _hp, int _attck)
{
name = _name;
hp = _hp;
attck = _attck;
}
virtual void AtK()
{
hp -= 0;
}
};
class Mage:public Hero
{
private:
int ap_attck = 50;
public:
Mage(){}
Mage(string _name,int _hp,int _attck):Hero(_name, _hp, _attck){}
void AtK() override
{
hp -= (attck + ap_attck);
}
};
class Archer:public Hero
{
private:
int ac_attck = 100;
public:
Archer(){}
Archer(string _name,int _hp,int _attck):Hero(_name, _hp, _attck){}
void AtK() override
{
hp -= (attck + ac_attck);
}
};
int main()
{
Mage mage("Mage", 100, 20); // 实例化法师对象
Archer archer("Archer", 150, 30); // 实例化射手对象
while (monster > 0)
{
mage.AtK();
archer.AtK();
monster -= (mage.attck + archer.attck);
cout << "怪物血量: " << monster << endl;
}
cout << "怪物被杀死!" << endl;
return 0;
}
2、思维导图