作业--day40

全局变量,int monster = 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 

int monster = 10000;
using namespace std;

//英雄类
class Hero{
protected:
    string name;
    int hp;
    int attck;
public:
    Hero():name("hero"), hp(1000), attck(50){}
    Hero(string name, int hp, int attck):name(name), hp(hp), attck(attck){}
    virtual void atk(){
        monster -= 0;
    }
};

//法师类
class Mage:public Hero{
    int ap_atk = 50;
public:
    Mage(int ap_atk, string name, int hp, int attck):ap_atk(ap_atk), Hero(name,hp,attck){}
    void atk(){
        monster -= (Hero::attck + ap_atk);
        cout << "monster = " << monster << endl;
    }
    void show(){
        cout << "name=" << Hero::name << ", attck=" << Hero::attck <<endl;
    }
};

//射手类
class Adc:public Hero{
    int ac_atk = 100;
public:
    Adc(int ac_atk, string name, int hp, int attck):ac_atk(ac_atk), Hero(name,hp,attck){}
    void atk(){
        monster -= (Hero::attck + ac_atk);
        cout << "monster = " << monster << endl;
    }
    void show(){
        cout << "name=" << Hero::name << ", attck=" << Hero::attck <<endl;
    }
};

int main()
{
    Adc ac(60, "lucy", 1500, 100);
    ac.show();
    Mage ap(80, "luna", 1300, 80);
    ap.show();

    Hero *p = NULL;
    int count = 0;
    while(monster > 0){
        p = &ac;
        p->atk();
        p = &ap;
        p->atk();
        count++;
    }
    cout << "count = " << count << endl;
    return 0;
}

思维导图

作业--day40_第1张图片

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