2、
#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 Mage:public hero
{
private:
int ap_atk = 50;
public:
Mage(){}
Mage(string name,int hp,int attack):hero(name,hp,attack){}
void Atk()
{
blood -= (attack += ap_atk);
}
};
class Archer:public hero
{
private:
int ap_atk = 100;
public:
Archer(){}
Archer(string name,int hp,int attack):hero(name,hp,attack){}
void Atk()
{
blood -= (attack += ap_atk);
}
};
int main()
{
Mage m("Mage",1000,500);
Archer a("Archer",200,200);
int sum = 0;
while(blood > 0)
{
m.Atk();
//a.Atk();
sum++;
cout << blood << endl;
}
cout << "小怪在法师的第" << sum << "攻击下被杀死了"<<endl;
return 0;
}
运行效果如下