C++day5

文章展示了C++中如何使用类和继承创建英雄角色(Master和Shooter),通过虚拟函数实现不同角色的不同攻击效果,模拟怪物与英雄的战斗过程。
摘要由CSDN通过智能技术生成

1.

#include <iostream>

using namespace std;

int monster=10000;
class Hero
{
    string name;
    int hp;
    int attack;
public:
    Hero():name("张三"),hp(100),attack(10){}
    Hero(string name,int hp,int attack):name(name),hp(hp),attack(attack){}
    virtual int Atk()
    {
        monster-=attack;
        hp-=10;
        if(hp<=0)
        {
            return 0;
        }
        cout << "-------------" << name << "hp=" << hp << "-------------" << endl;
        return hp;
    }
};

class Master:public Hero
{
    int ap_ack;
public:
    Master(string name,int hp,int attack,int ap_ack):Hero(name,hp,attack),ap_ack(ap_ack){}
    int Atk()
    {
        int res=Hero::Atk();
        if(res<=0)
        {
            return -1;
        }
        monster-=ap_ack;
        return 0;
    }
};

class Shooter:public Hero
{
    int ad_ack;
public:
    Shooter(string name,int hp,int attack,int ad_ack):Hero(name,hp,attack),ad_ack(ad_ack){}
    int Atk()
    {
        int res=Hero::Atk();
        if(res<=0)
        {
            return -1;
        }
        monster-=ad_ack;
        return 0;
    }
};


int main()
{
    int time=1;
    Master m1("甄姬",150,50,300);
    Shooter s1("鲁班",120,50,400);
    while(monster>0)
    {

        Hero* p1=&m1;
        if(p1->Atk()==0)
        {
            cout << "-------------monster=" << monster << "-------------" << endl;
        }
        Hero* p2=&s1;
        if(p2->Atk()==0)
        {
            cout << "-------------monster=" << monster << "-------------" << endl;
        }
        cout << " " << endl;
        time++;
    }
    cout << "-----------怪兽在第" << time << "次被消灭-----------" << endl;
    return 0;
}

2.

#include <iostream>

using namespace std;

int monster=10000;
class Hero
{
    string name;
    int hp;
    int attack;
public:
    Hero():name("张三"),hp(100),attack(10){}
    Hero(string name,int hp,int attack):name(name),hp(hp),attack(attack){}
    virtual int Atk()
    {
        monster-=attack;
        hp-=10;
        if(hp<=0)
        {
            return 0;
        }
        cout << "-------------" << name << "hp=" << hp << "-------------" << endl;
        return hp;
    }
};

class Master:public Hero
{
    int ap_ack;
public:
    Master(string name,int hp,int attack,int ap_ack):Hero(name,hp,attack),ap_ack(ap_ack){}
    int Atk()
    {
        int res=Hero::Atk();
        if(res<=0)
        {
            return -1;
        }
        monster-=ap_ack;
        return 0;
    }
};

class Shooter:public Hero
{
    int ad_ack;
public:
    Shooter(string name,int hp,int attack,int ad_ack):Hero(name,hp,attack),ad_ack(ad_ack){}
    int Atk()
    {
        int res=Hero::Atk();
        if(res<=0)
        {
            return -1;
        }
        monster-=ad_ack;
        return 0;
    }
};


int main()
{
    int time=1;
    Master m1("甄姬",150,50,300);
    Shooter s1("鲁班",120,50,400);
    while(monster>0)
    {

        Hero* p1=&m1;
        if(p1->Atk()==0)
        {
            cout << "-------------monster=" << monster << "-------------" << endl;
        }
        Hero* p2=&s1;
        if(p2->Atk()==0)
        {
            cout << "-------------monster=" << monster << "-------------" << endl;
        }
        cout << " " << endl;
        time++;
    }
    cout << "-----------怪兽在第" << time << "次被消灭-----------" << endl;
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值