1.整理思维导图
2.全鋦变量, int blood= 10000;定义英雄类hero,受保护的属性string name, int hp,int attck;公有的无参构造,有参构造,
虚成员函数void Atk(){blood-=0;},法师类继承自英雄类,私有属性intap_ _atk=50; 重写虚成员函数void Atk(){lood-=(attck+ap_ atk);}; 射手类继承自英雄泪,私有属性intac_ _atk= 100;实例化类对象,判断怪物何时被杀死。
#include <iostream>
using namespace std;
static int blood = 10000;
class Hero
{
protected:
string name; //英雄名
int hp; //英雄血量
int attack; //攻击值
public:
Hero(){cout << "Hero无参构造" << endl;}
Hero(string name,int hp,int attack):name(name),hp(hp),attack(attack)
{
this->name = name;
this->hp = hp;
this->attack = attack;
}
//在实例化出类对象的时候,类对象和该虚函数本身没有实际的意义
virtual void Atk() = 0;//纯虚函数
// {
// blood-=0;
// }
};
class Mage:public Hero //法师
{
int ap_atk = 50;
public:
Mage(){}
Mage(int ap_atk,string name,int hp,int attack):Hero(name,hp,attack),ap_atk(ap_atk){}
void Atk()
{
blood -= (attack+ap_atk);
}
};
class Archer:public Hero //射手
{
int ac_atk = 100;
public:
Archer(){}
Archer(int ac_atk,string name,int hp,int attack):Hero(name,hp,attack),ac_atk(ac_atk){}
void Atk()
{
blood -=(attack+ac_atk);
}
};
int main()
{
Mage m(50,"妲己",2000,600);
Archer s(100,"虞姬",1500,800);
Hero *h1 = &m;
Hero *h2 = &s;
while(blood > 0)
{
h1->Atk();
h2->Atk();
cout << "怪物血量" << blood << endl;
}
return 0;
}