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;
}
相关推荐
沐怡旸12 分钟前
【底层机制】右值引用是什么?为什么要引入右值引用?
c++·面试
编码浪子16 分钟前
趣味学RUST基础篇(构建一个命令行程序2重构)
开发语言·重构·rust
echoarts1 小时前
MATLAB R2025a安装配置及使用教程(超详细保姆级教程)
开发语言·其他·matlab
scx201310041 小时前
P13929 [蓝桥杯 2022 省 Java B] 山 题解
c++·算法·蓝桥杯·洛谷
阿方.9181 小时前
《数据结构全解析:栈(数组实现)》
java·开发语言·数据结构
CYRUS_STUDIO1 小时前
LLVM 不止能编译!自定义 Pass + 定制 clang 实现函数名加密
c语言·c++·llvm
CYRUS_STUDIO1 小时前
OLLVM 移植 LLVM 18 实战,轻松实现 C&C++ 代码混淆
c语言·c++·llvm
落羽的落羽1 小时前
【C++】简单介绍lambda表达式
c++·学习
Dovis(誓平步青云)2 小时前
《探索C++11:现代语法的内存管理优化“性能指针”(下篇)》
开发语言·jvm·c++
charlie1145141912 小时前
前端三件套简单学习:HTML篇1
开发语言·前端·学习·html