抽象工厂模型类的多态

work.cpp

#include 

using namespace std;
static int blood = 10000;
class Hero{
protected:
     string name;
     int hp;
     int base_attack;
public:
     Hero(){}
     Hero(string name,int hp,int base_attack):name(name),hp(hp),base_attack(base_attack){}
     virtual void jungle(){
         blood -= base_attack;
         cout<<"受到"<<name<<"攻击血量-"<<base_attack<<"剩余血量:"<<blood<<endl;
     }
     virtual ~Hero(){}
};
class Shooter:public Hero{
    int add_attack;
public:
    Shooter(){}
    Shooter(string name,int add_attack,int hp,int base_attack):add_attack(add_attack),Hero (name,hp,base_attack){}
    void jungle(){
        blood -= (add_attack+base_attack);
        cout<<"受到"<<name<<"攻击血量-"<<(add_attack+base_attack)<<"剩余血量:"<<blood<<endl;
    }
};
class Magicer:public Hero{
    int magic_attack;
public:
    Magicer(){}
    Magicer(string name,int magic_attack,int hp,int base_attack):magic_attack(magic_attack),Hero (name,hp,base_attack){}
    void jungle(){
        blood -= (magic_attack+base_attack);
        cout<<"受到"<<name<<"攻击血量-"<<(magic_attack+base_attack)<<"剩余血量:"<<blood<<endl;
    }
    ~Magicer(){}
};
void attack(Hero &h){
    h.jungle();
}
int main()
{
    Shooter houyi("后羿",100,600,80);
    Magicer xiaoqiao("小乔",50,600,80);
    int i=0;
    while(blood >= 0 ){
        i++;
        attack(houyi);
        if(blood < 0){
            break;
        }
        attack(xiaoqiao);

    }
    cout<<i<<endl;
    return 0;
}

抽象工厂模型类的多态_第1张图片

你可能感兴趣的:(c++,开发语言,状态模式)