2、
文章来源地址https://www.toymoban.com/news/detail-516962.html
#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;
}
运行效果如下
文章来源:https://www.toymoban.com/news/detail-516962.html
到了这里,关于C++day5的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!