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;
}
相关推荐
胡八一10 分钟前
30 分钟上手 exp4j:在 Java 中安全、灵活地计算数学表达式
java·开发语言·安全
郝学胜-神的一滴44 分钟前
Linux 进程控制块(PCB)解析:深入理解进程管理机制
linux·服务器·开发语言
后端小张1 小时前
【鸿蒙开发手册】重生之我要学习鸿蒙HarmonyOS开发
开发语言·学习·华为·架构·harmonyos·鸿蒙·鸿蒙系统
胖咕噜的稞达鸭1 小时前
AVL树手撕,超详细图文详解
c语言·开发语言·数据结构·c++·算法·visual studio
CSCN新手听安1 小时前
【linux】多线程(六)生产者消费者模型,queue模拟阻塞队列的生产消费模型
linux·运维·服务器·c++
-SGlow-1 小时前
Linux相关概念和易错知识点(48)(epoll的底层原理、epoll的工作模式、反应堆模式)
linux·服务器·c语言·网络·c++
007php0071 小时前
百度面试题解析:synchronized、volatile、JMM内存模型、JVM运行时区域及堆和方法区(三)
java·开发语言·jvm·缓存·面试·golang·php
csdn_aspnet1 小时前
C++ 圆台体积和表面积计算程序(Program for Volume and Surface area of Frustum of Cone)
c++
芒果量化1 小时前
Optuna - 自动调参利器&python实例
开发语言·python·算法·机器学习
foundbug9992 小时前
基于CSMA-CA协议的V2X通信MATLAB仿真
开发语言·网络·matlab