C++ DAY5 作业

发布时间:2024年01月02日

?1.全局变量,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 Atik()(blood-=(attck+ac_atk);}实例化类对象,判断怪物何时被杀死。(能写多少写多少)

#include <iostream>

using namespace std;

int Monster_blood = 10000;

//英雄类
class Hero
{
protected:
    string name;
    int hp;
    int attck;
public:
    //构造函数
    Hero(){
        cout << "Hero无参构造" << endl;
    }
    Hero(string name,int hp,int attck):name(name),hp(hp),attck(attck){
        cout << "Hero有参构造" << endl;
    }
    //虚函数
    virtual void Atk()
    { Monster_blood = 0;}

};

//法师类
class Master: public Hero
{
    int ap_atk = 50;
public:
    //构造函数
    Master(string name,int hp,int attck):Hero(name,hp,attck){
        cout << "Master有参构造" << endl;
    }
    void Atk()
    {
        Monster_blood -= (attck + ap_atk);
    }
};

//射手类
class Shooter:public Hero
{
    int ac_atk = 100;
public:
    //构造函数
    Shooter(string name,int hp,int attck):Hero(name,hp,attck){
        cout << "Shooter有参构造" << endl;
    }
    void Atk()
    {
        Monster_blood -=(attck + ac_atk);
    }
};

struct Attck_num
{
    int M_num;
    int S_num;
};
//打怪
Attck_num Beat_Monster(Master M1,Shooter S1,struct Attck_num num)
{
    while(Monster_blood >= 0)
    {
        M1.Atk();
        if(Monster_blood >= 0)
        {
            num.M_num++;
        }else {break;}

        S1.Atk();
        if(Monster_blood >= 0)
        {
            num.S_num++;
        }else {break;}
    }
    return num;
}

int main()
{
    Master M1("Raz",1000,60);
    Shooter S1("VN",1000,70);
    //定义打怪次数结构体
    struct Attck_num num;
    num.M_num = 0;
    num.S_num = 0;
    num = Beat_Monster(M1,S1,num);

    cout << "Master攻击次数:" << num.M_num << endl;
    cout << "Shooter攻击次数" << num.S_num << endl;
    cout << "Monster die" << endl;
    return 0;
}

2.思维导图

文章来源:https://blog.csdn.net/qq_44121517/article/details/135348468
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。