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;
}
相关推荐
下北沢美食家几秒前
JavaScript面试题2
开发语言·javascript·ecmascript
6Hzlia1 分钟前
【Hot 100 刷题计划】 LeetCode 39. 组合总和 | C++ 回溯算法与 startIndex 剪枝
c++·算法·leetcode
宵时待雨17 分钟前
优选算法专题1:双指针
数据结构·c++·笔记·算法·leetcode
程序员学习随笔19 分钟前
深入剖析 std::optional:实现原理、性能优化与安全编程实践
c++·安全·空值
数据知道21 分钟前
claw-code 源码分析:大型移植的测试哲学——如何用 unittest 门禁守住「诚实未完成」的口碑?
开发语言·python·ai·claude code·claw code
tankeven27 分钟前
HJ172 小红的矩阵染色
c++·算法
小堃学编程30 分钟前
【项目实战】基于protobuf的发布订阅式消息队列(2)—— 线程池
java·开发语言
每日任务(希望进OD版)36 分钟前
线性DP、区间DP
开发语言·数据结构·c++·算法·动态规划
怨言.37 分钟前
Java内部类详解:从基础概念到实战应用(附案例)
java·开发语言
AC赳赳老秦37 分钟前
OpenClaw image-processing技能实操:批量抠图、图片尺寸调整,适配办公需求
开发语言·前端·人工智能·python·深度学习·机器学习·openclaw