全局变量,int monster = 10000;定义英雄类hero,受保护的属性string name, int hp,int attck;公有的无参构造,有参构造,虚成员函数 void Atk(){blood-=0;},法师类继承自英雄类,私有属性int ap_ _atk=50; 重写虚成员函数void Atk(){blood-=(attck-ap_ atk);}; 射手类继承自英雄类,私有属性intac_ _atk= 100;实例化类对象,判断怪物何时被杀死。
#include <iostream>
using namespace std;
int blood=10000;
class hero
{
protected:
string name;
int hp;
int attack;
public:
hero()
{
cout<<"英雄诞生!"<<endl;
}
hero(string name,int hp,int attack):name(name),hp(hp),attack(attack)
{
cout<<"英雄穿衣服诞生!"<<endl;
}
virtual void Atk()
{
blood-=0;
}
};
class master:public hero
{
private:
int ap_atk=50;
public:
master(string name,int hp,int attack):hero(name,hp,attack)
{
cout<<"法师诞生!"<<endl;
}
void Atk()
{
blood-=(attack-ap_atk);
cout<<"法师攻击以后剩余血量"<<blood<<endl;
}
};
class shooter:public hero
{
private:
int ac_atk=100;
public:
shooter(string name,int hp,int attack):hero(name,hp,attack)
{
cout<<"射手诞生!"<<endl;
}
void Atk()
{
blood-=(attack-ac_atk);
cout<<"射手攻击以后剩余血量"<<blood<<endl;
}
};
int main()
{
shooter p1("射手",100,800);
master p2("法师",80,1000);
while(blood>0)
{
p1.Atk();
p2.Atk();
}
return 0 ;
}