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;
}
相关推荐
丁值心16 分钟前
5.29打卡
开发语言·人工智能·python·机器学习·支持向量机
我有一个魔盒22 分钟前
python uv包管理器使用
开发语言·python·uv
小鹿鹿啊31 分钟前
C语言编程--20.合并K个升序列表
c语言·开发语言
归途醉染35 分钟前
C# Costura.Fody 排除多个指定dll
开发语言·c#
wujj_whut1 小时前
【PC网上邻居--1】基于Samba协议的局域网文件共享系统设计与实现
c++
想睡hhh1 小时前
Practice 2025.5.29 —— 二叉树进阶面试题(1)
c++·算法
vvilkim1 小时前
C# 数组与字符串:全面解析与应用实践
开发语言·算法·c#
QQ_hoverer1 小时前
Java设计模式之工厂模式与策略模式简单案例学习
java·开发语言·学习·设计模式·策略模式
Ashlee_code1 小时前
TRS收益互换平台开发实践:从需求分析到系统实现
java·数据结构·c++·python·架构·php·需求分析