C++day5

2024-01-02 22:41:41

?

#include <iostream>
using namespace std;
int 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 ~Hero(){cout<<"hero的析构函数"<<endl;}
    virtual void Atk()
    {
        blood-=0;
    }
};
class Master:public Hero
{
    int ap_atk=50;
public:
    Master(){}
    Master(string name,int hp,int attck):Hero(name,hp,attck){}
    void Atk() override
    {
        blood-=(attck+ap_atk);
    }
};
class Shooter:public Hero
{
    int ac_atk=100;
public:
    Shooter(){}
    Shooter(string name,int hp,int attck):Hero(name,hp,attck){}
    void Atk()override
    {
        blood-=(attck+ac_atk);
    }
    ~Shooter(){cout<<"shoot的析构函数"<<endl;}
};
int main()
{
   Master m1("剑姬",100,2400);
   Shooter s1("刀妹",100,2500);
   int count=0;
   while(blood>0)
   {
       m1.Atk();
       if(blood>0)
       {
           s1.Atk();
       }
       count++;
   }
   Hero *p=new Shooter("万豪",2000,100);
           p->Atk();
   delete p;
    return 0;
}

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