1.
2.
#include <iostream>
using namespace std;
int blood = 10000;
class Hero
{
protected:
string name;
int hp;
int attck;
public:
Hero(string name,int hp,int attck)
{
this->name = name;
this->hp = hp;
this->attck = attck;
cout << "有参构造" << endl;
}
virtual void Atk()
{
blood-=0;
}
};
class Master:public Hero
{
int ap_atk = 50;
public:
Master(string name,int hp,int attck):Hero (name,hp,attck)
{
cout << "有参构造" << endl;
}
void Atk()
{
blood-=(attck - ap_atk);
cout<<"英雄1攻击后,怪物剩余血量:"<< blood << endl;
}
};
class Shooter:public Hero
{
int ac_atk = 100;
public:
Shooter(string name,int hp,int attck):Hero (name,hp,attck)
{
this->ac_atk = ac_atk;
cout << "有参构造" << endl;
}
void Atk()
{
blood-=(attck - ac_atk);
cout << "英雄2攻击后,怪物剩余血量:" << blood << endl;
}
};
int main()
{
Hero *p1 = new Master ("干将莫邪",100,600);
Hero *p2 = new Shooter ("百里守约",100,800);
while(blood > 0)
{
p1->Atk();
p2->Atk();
cout<<"怪物剩余血量:"<<blood<<endl;
}
return 0;
}
运行结果: