当前位置: 首页 > news >正文

C++(匿名函数+继承+多态)

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

// 基类 Weapon
class Weapon {
protected:
    int atk;
public:
    Weapon(int atk = 0) : atk(atk) {}

    virtual void setatk(const int& _atk) {
        atk = _atk;
    }

    virtual int getatk() const {  // 使用 const 保证不会修改成员
        return atk;
    }

    virtual ~Weapon() = default;  // 确保正确的析构
};

// 派生类 Sword (长剑)
class Sword : public Weapon {
private:
    int hp;
    int all[2];
public:
    Sword(int atk, int hp = 0) : Weapon(atk), hp(hp) {}

    void getatk(const int& _atk, const int& _hp) {
        atk = _atk;  // 改为直接赋值
        hp = _hp;
    }

    int* getatk(){  // 重写 getatk 并返回属性
        all[0] = atk;
        all[1] = hp;
        return all;
    }
};

// 派生类 Blade (短剑)
class Blade : public Weapon {
private:
    int spd;
    int all[2];
public:
    Blade(int atk, int spd = 0) : Weapon(atk), spd(spd) {}

    void getatk(const int& _atk, const int& _spd) {
        atk = _atk;  // 改为直接赋值
        spd = _spd;
    }

    int* getatk(){  // 重写 getatk 并返回属性
        all[0] = atk;
        all[1] = spd;
        return all;
    }
};

// 派生类 Axe (斧头)
class Axe : public Weapon {
private:
    int def;
    int all[2];
public:
    Axe(int atk, int def = 0) : Weapon(atk), def(def) {}

    void getatk(const int& _atk, const int& _def) {
        atk = _atk;  // 改为直接赋值
        def = _def;
    }

    int* getatk(){  // 重写 getatk 并返回属性
        all[0] = atk;
        all[1] = def;
        return all;
    }
};

// Hero 类,能够装备不同的武器,并获得不同的加成
class Hero {
private:
    int atk;
    int def;
    int spd;
    int hp;
    Weapon* weapon;  // 使用基类指针

public:
    Hero(int atk = 0, int def = 0, int spd = 0, int hp = 0)
        : atk(atk), def(def), spd(spd), hp(hp), weapon(nullptr) {}

    // 装备武器
    void equipWeapon(Weapon* p) {
        weapon = p;
        // 使用多态来获取武器的 atk
        if (weapon) {
            this->atk += weapon->getatk();  // 增加英雄的 atk
        }
    }

    void display() {
        cout << "Hero stats -> atk: " << atk << ", def: " << def
             << ", spd: " << spd << ", hp: " << hp << endl;
    }
};

int main() {
    // 创建不同的武器
    Sword sword(100, 100);
    Blade blade(70, 30);
    Axe axe(130, 40);

    // 创建 Hero 对象
    Hero hero(50, 30, 20, 100);

    // 装备武器
    hero.equipWeapon(&sword);  // 装备长剑
    hero.display();

    hero.equipWeapon(&blade);  // 装备短剑
    hero.display();

    hero.equipWeapon(&axe);  // 装备斧头
    hero.display();

    return 0;
}

 

 

http://www.dtcms.com/a/107810.html

相关文章:

  • MySQL GROUP BY 和 HAVING 子句中 ‘Unknown column‘ 错误的深入解析
  • 详细介绍一下C++的按位运算
  • Tinder上线《The Game Game》
  • mapreduce工作原理
  • 论文阅读10——解开碳排放与碳足迹之间的关系:文献回顾和可持续交通框架
  • TCP四次挥手
  • 《K230 从熟悉到...》颜色识别
  • 归并排序延伸-非递归版本
  • 基于yolo11的BGA图像目标检测
  • Ubuntu18.04 编译 Android7.1代码报错
  • 使用numpy读取数据集
  • Ubuntu Wayland启动腾讯会议并实现原生屏幕共享
  • JS—页面渲染:1分钟掌握页面渲染过程
  • C语言实现排序
  • spring-ai-alibaba第六章阿里dashscope集成mcp百度翻译tools
  • Java 大视界 -- Java 大数据在智慧文旅虚拟场景构建与沉浸式体验增强中的技术支撑(168)
  • STM32F103_LL库+寄存器学习笔记14 - CAN发送完成中断
  • 【小兔鲜】day02 Pinia、项目起步、Layout
  • 2023年CIE SCI1区TOP:序列融合麻雀搜索算法ISSA,深度解析+性能实测
  • N元语言模型的时间和空间复杂度计算
  • 【网络协议】三次握手与四次挥手
  • 【区块链 + 可信存证】创世云区块链存证平台 | FISCO BCOS 应用案例
  • Redis 数据结构的底层实现—字符串、哈希表、列表、集合
  • 【银河麒麟系统常识】命令:reboot(立即重启操作系统)
  • SQL server 2022和SSMS的使用案例1
  • linux,物理机、虚拟机,同时内外网实现方案;物理机与虚拟机互通网络;
  • 机器学习 分类算法
  • 苍穹外卖day12
  • 网络安全的重要性与防护措施
  • 一、STM32简介