c++day5

2024-01-03 10:38:12

?

#include <iostream>

using namespace std;

int blood=10000;
class hero
{
protected:
    string name;
    int hp;
    int attck;
public:
    //无参构造
    hero():attck(500){}
    //有参构造
    hero(string name,int hp,int attck):name(name),hp(hp),attck(attck){}

    //虚成员函数
    virtual void Atk()
    {
        blood-=0;
    }
};
class fas:public hero
{
private:
    int ap_atk=50;
public:
    void Atk()
    {
        blood-=(attck+ap_atk);
        cout<<blood<<endl;
    }
};
class shes:public hero
{
private:
    int ac_atk=100;
public:
    void Atk()
    {
        blood-=(attck+ac_atk);
        cout<<blood<<endl;
    }
};

int main()
{
    fas f1;
    hero *p=&f1;
    p->Atk();
    shes s1;
    hero *q=&s1;
    q->Atk();
    int count=0;
   while(blood>0)
   {
      p->Atk();
      q->Atk();
      count++;

   }
   cout<<count<<endl;

    return 0;
}

?

c++day5 - 幕布

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