C++的多态与继承

cpp 复制代码
#include <iostream>
using namespace std;
class NPC
{
protected:
    int atk;
    int def;
    int spd;
    int hp;
    string name;
public:
    NPC(int atk,int def,int spd,int hp,const string& name)
    :atk(atk), def(def), spd(spd), hp(hp), name{name}
    {}
    virtual void show()=0;
    virtual int getAtk(){return atk;};
    virtual int getDef(){return def;};
    virtual int getSpd(){return spd;};
    virtual int getHp(){return hp;};
    virtual void setAtk(int atk){this->atk=atk;};
    virtual void setDef(int def){this->def=def;};
    virtual void setSpd(int spd){this->spd=spd;};
    virtual void setHp(int hp){this->hp=hp;};
    
    virtual ~NPC(){};
};

class weapon
{
private:
    /* data */
public:
    virtual ~weapon(){};
    virtual bool onEquip(NPC&)=0;
    virtual bool onUnequip(NPC&)=0;
};

class Blade:public weapon
{
public:
    bool onEquip(NPC& npc){
        npc.setAtk(npc.getAtk()+1);
        npc.setSpd(npc.getSpd()+1);
        return true;
    }
    bool onUnequip(NPC& npc){
        npc.setAtk(npc.getAtk()-1);
        npc.setSpd(npc.getSpd()-1);
        return true;
    }
};
class Sword:public weapon
{
public:
    bool onEquip(NPC& npc){
        npc.setAtk(npc.getAtk()+1);
        npc.setHp(npc.getHp()+1);
        return true;
    }
    bool onUnequip(NPC& npc){
        npc.setAtk(npc.getAtk()-1);
        npc.setHp(npc.getHp()-1);
        return true;
    }
};
class Axe:public weapon
{
public:
    bool onEquip(NPC& npc){
        npc.setAtk(npc.getAtk()+1);
        npc.setDef(npc.getDef()+1);
        return true;
    }
    bool onUnequip(NPC& npc){
        npc.setAtk(npc.getAtk()-1);
        npc.setDef(npc.getDef()-1);
        return true;
    }
};
class Hero:public NPC
{
private:
    /* data */
public:
    Hero(int atk,int def,int spd,int hp,const string& name)
    :NPC(atk, def, spd, hp, name)
    {}
    void show() override {
        cout << "Hero: " << name << ", Atk: " << atk << ", Def: " << def 
             << ", Spd: " << spd << ", Hp: " << hp << endl;
    }
    bool equipWeapon(weapon& w) {
        return w.onEquip(*this);
    }
    bool unequipWeapon(weapon& w) {
        return w.onUnequip(*this);
    }
    ~Hero(){};
};


int main(int argc, char const *argv[])
{
    auto hero = Hero(10, 5, 3, 100, "一点都不想去大坝的深蓝");
    hero.show();
    Blade blade;
    Sword sword;
    Axe axe;
    hero.equipWeapon(blade);
    hero.show();
    hero.unequipWeapon(blade);
    hero.show();

    hero.equipWeapon(sword);
    hero.show();
    hero.unequipWeapon(sword);
    hero.show();

    hero.equipWeapon(axe);
    hero.show();
    hero.unequipWeapon(axe);
    hero.show();

    return 0;
}
相关推荐
iCxhust6 小时前
c# U盘映像生成工具
开发语言·单片机·c#
yangzhi_emo7 小时前
ES6笔记2
开发语言·前端·javascript
emplace_back8 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk8 小时前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习
萧曵 丶8 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
xiaolang_8616_wjl8 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
夜月yeyue8 小时前
设计模式分析
linux·c++·stm32·单片机·嵌入式硬件
收破烂的小熊猫~8 小时前
《Java修仙传:从凡胎到码帝》第四章:设计模式破万法
java·开发语言·设计模式
nananaij9 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm