01-02

01-02_第1张图片

作业:

  1. 全局变量,int blood = 10000;定义英雄类hero,受保护的属性string name,int hp,int attck;公有的无参构造,有参构造,虚成员函数 void Atk(){blood-=0;},法师类继承自英雄类,私有属性 int ap_atk=50;重写虚成员函数void Atk(){blood-=(attck+ap_atk);};射手类继承自英雄类,私有属性 int ac_atk = 100;重写虚成员函数void Atk(){blood-=(attck+ac_atk);}实例化类对象,判断怪物何时被杀死。(能写多少写多少)
#include 

using namespace std;
int blood=10000;
class hero
{
protected:
    string name;
    int hp;
    int attack;
public:
    hero():name("马哥"),hp(500),attack(50){}
    hero(string name,int hp,int attack):name(name),hp(hp),attack(attack){}
    virtual void Atk()
    {
        blood-=0;
    }
};
class AP:public hero
{
    int ap_atk=50;
public:
    AP():hero(){}
    AP(string name,int hp,int attack):hero(name,hp,attack){}
    void Atk()
    {
        blood-=(attack+ap_atk);
    }
};
class AD:public hero
{
    int ad_atk=100;
public:
    AD():hero(){}
    AD(string name,int hp,int attack):hero(name,hp,attack){}
    void Atk()
    {
        blood-=(attack+ad_atk);
    }
};
int main()
{
    AD a;
    AP b("hs",550,50);
    while (1)
    {
        a.Atk();
        cout<<"AD a了一下,blood="<

你可能感兴趣的:(c++)