c++day5(2024/1/2)

2024-01-03 07:59:16
#include <iostream>

using namespace std;
#include <iostream>

using namespace std;

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

class Master:public Hero{
    int ap_atk=50;
public:
    Master(string name,int hp,int attack):Hero(name,hp,attack){}
    void Atk(){
       int blood=monster;
       int  count=0;
       while (blood>0) {
           count++;
           blood-=(ap_atk+Hero::attack);
       }
       cout<< "法师需要攻击"<< count << "次后怪物死亡" << endl;
    }
    void show()
    {
        cout << "Master:name=" << Hero::name << endl;
        cout << "Master:hp=" << hp << endl;
        cout << "Master:attack=" << ap_atk+Hero::attack << endl;

    }
};

class Shooter:public Hero{
    int ac_atk=50;
public:
    Shooter(string name,int hp,int attack):Hero(name,hp,attack){}
    void Atk(){
       int blood=monster;
       int  count=0;
       while (blood>0) {
           count++;
           blood-=(ac_atk+Hero::attack);
       }
       cout<< "射手需要攻击"<< count << "次后怪物死亡" << endl;
    }
    void show()
    {
        cout << "Shooter:name=" << Hero::name << endl;
        cout << "Shooter:hp=" << hp << endl;
        cout << "Shooterr:attack=" << ac_atk+Hero::attack << endl;

    }
};

int main(){
    Shooter a("vn",560,150);
    Master b("虚空之眼",400,300);
    Hero *hero=&a;
    Hero *hero2=&a;

    a.show();
    hero->Atk();
    cout<<"--------------------------"<<endl;
    b.show();
    hero2->Atk();
    return 0;
}

在这里插入图片描述
在这里插入图片描述

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