C++ day5

思维导图

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

using namespace std;

int monster=10000;
class Hero
{
protected:
    string name;
    int hp;
    int attack;
public:
    Hero():attack(100){}
    Hero(string n,int h,int a):name(n),hp(h),attack(a){}
    void virtual Atk()
    {
        monster-=0;
    }
};

class Fashi:public Hero
{
    int ap_atk=50;
public:

    void Atk()
    {
        monster-=(attack+ap_atk);
    }
};

class Sheshou:public Hero
{
    int ac_atk=100;
public:
    void Atk()
    {
        monster-=(attack+ac_atk);
    }
};

int main()
{
    Fashi f;
    Sheshou s;
    int count=0;
    while(monster>0)
    {
        f.Atk();
        s.Atk();
        count++;
    }
    cout << "count=" << count << endl;
    cout << "monster=" << monster << endl;
    return 0;
}

你可能感兴趣的:(c++,开发语言)