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

c++day5

作业:
编写一个如下场景:
有一个英雄Hero类,私有成员,攻击,防御,速度,生命值,以及所有的set get 方法
编写一个 武器 Weapon 类,拥有私有成员攻击力,以及set get 方法
编写一个 长剑 Sword 类,继承自武器类,拓展属性 生命值,以及set get 方法
编写一个 匕首Blade类,继承自武器类,拓展属性 速度,以及set get 方法
编写一个 斧头 Axe类,继承自武器类,拓展属性 防御力,以及set get 方法

武器Weapon类里面,要求有一个多态函数,叫做 equip 函数
英雄Hero类里面,要求有一个公开函数,equipWeapon(Weapon* w)
实现功能:英雄既可以装备长剑,也可以装备短剑,也可以装备斧头,但是要求装备不同的武器,英雄需要获得不同的属性加成

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

using namespace std;

// 英雄
class Hero {
private:
    int Attack;
    int Defense;
    int Speed;
    int Health;
public:
    Hero(int Attack = 0, int Defense = 0, int Speed = 0, int Health = 0)
        : Attack(Attack), Defense(Defense), Speed(Speed), Health(Health) {}

    // 攻击力
    void setAttack(int attack) { Attack = attack; }
    int getAttack() { return Attack; }

    // 防御力
    void setDefense(int defense) { Defense = defense; }
    int getDefense() { return Defense; }

    // 敏捷力
    void setSpeed(int speed) { Speed = speed; }
    int getSpeed() { return Speed; }

    // 生命值
    void setHealth(int health) { Health = health; }
    int getHealth() { return Health; }

    // 英雄面板
    void show() {
        cout << "英雄: 陈育林" << endl;
        cout << "攻击力: " << Attack << endl;
        cout << "防御力: " << Defense << endl;
        cout << "敏捷力: " << Speed << endl;
        cout << "生命值: " << Health << endl;
    }
};

// 武器
class Weapon {
private:
    int Attack;
public:
    Weapon(int Attack = 0) : Attack(Attack) {}
    void setAttack(int attack) { Attack = attack; }
    int getAttack() { return Attack; }
    virtual void equip(Hero* hero) {
        hero->setAttack(hero->getAttack() + Attack);
    }
    virtual void unequip(Hero* hero) {
        hero->setAttack(hero->getAttack() - Attack);
    }
};

// 长剑
class Sword : public Weapon {
private:
    int Health;
public:
    Sword(int Attack = 0, int Health = 0) : Weapon(Attack), Health(Health) {}
    void setHealth(int health) { Health = health; }
    int getHealth() { return Health; }
    void equip(Hero* hero) override {
        Weapon::equip(hero);
        hero->setHealth(hero->getHealth() + Health);
    }
    void unequip(Hero* hero) override {
        Weapon::unequip(hero);
        hero->setHealth(hero->getHealth() - Health);
    }
};

// 匕首
class Blade : public Weapon {
private:
    int Speed;
public:
    Blade(int Attack = 0, int Speed = 0) : Weapon(Attack), Speed(Speed) {}
    void setSpeed(int speed) { Speed = speed; }
    int getSpeed() { return Speed; }
    void equip(Hero* hero) override {
        Weapon::equip(hero);
        hero->setSpeed(hero->getSpeed() + Speed);
    }
    void unequip(Hero* hero) override {
        Weapon::unequip(hero);
        hero->setSpeed(hero->getSpeed() - Speed);
    }
};

// 斧头
class Axe : public Weapon {
private:
    int Defense;
public:
    Axe(int Attack = 0, int Defense = 0) : Weapon(Attack), Defense(Defense) {}
    void setDefense(int defense) { Defense = defense; }
    int getDefense() { return Defense; }
    void equip(Hero* hero) override {
        Weapon::equip(hero);
        hero->setDefense(hero->getDefense() + Defense);
    }
    void unequip(Hero* hero) override {
        Weapon::unequip(hero);
        hero->setDefense(hero->getDefense() - Defense);
    }
};

// 英雄装备武器的函数
void equipWeapon(Hero& hero, Weapon* weapon) {
    weapon->equip(&hero);
}

int main(int argc, const char** argv) {
    Hero hero;
    Sword sword(20, 100); // 长剑:攻击力+20,生命值+100
    Blade blade(15, 50);  // 匕首:攻击力+15,速度+50
    Axe axe(30, 20);      // 斧头:攻击力+30,防御力+20

    cout << "初始属性:" << endl;
    hero.show();

    equipWeapon(hero, &sword);
    cout << "\n装备长剑后的属性:" << endl;
    hero.show();

    equipWeapon(hero, &blade);
    cout << "\n装备匕首后的属性:" << endl;
    hero.show();

    equipWeapon(hero, &axe);
    cout << "\n装备斧头后的属性:" << endl;
    hero.show();

    return 0;
}

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

相关文章:

  • Spring Boot + Vue 接入腾讯云人脸识别API(SDK版本3.1.830)
  • 【Elasticsearch】使用Postman操作es的_bulk批量操作
  • Redis面试知识点总结
  • 【原创工具】文件清单生成器 By怜渠客
  • 【算法设计与分析】(一)介绍算法与复杂度分析
  • WEEX交易所安全教學:如何應對剪切板被劫持駭客攻擊?
  • 复合字面量
  • 第十八:路由传参 query
  • Jupyter Notebook中使用GPU进行计算
  • ubuntu离线安装Ollama并部署Llama3.1 70B INT4
  • 刷题总结 栈和队列:单调栈
  • 玩机日记 14 飞牛fnOS部署qBittorrent、AList、Jellyfin,实现下载、存取、刮削、观看一体的家庭影音中心
  • 基于 CFD 预测的机器学习第 2 部分:在 Benchmark 应用程序上使用 Stochos 预测流场
  • VMware建立linux虚拟机
  • github 部署前端静态网页(react vite)
  • 【PLL】相位检测器:PFD
  • Java多线程安全
  • QT零基础学习之路(四)--信号和槽机制
  • 第五章:队列管理模块
  • DAY40|动态规划Part08|LeetCode: 121. 买卖股票的最佳时机 、 122.买卖股票的最佳时机II 、 123.买卖股票的最佳时机III
  • 文本编辑器使用指南:Linux中的文本编辑器大冒险
  • 算法题(76):跳跃游戏II
  • 【JavaWeb13】了解ES6的核心特性,对于提高JavaScript编程效率有哪些潜在影响?
  • 静止的钉子
  • transformer架构嵌入层位置编码之动态NTK-aware位置编码
  • 第四章 哈希表
  • 每天一个Flutter开发小项目 (4) : 构建收藏地点应用 - 深入Flutter状态管理
  • 递归、搜索与回溯算法 —— 名词解析
  • Elasticsearch面试宝典【刷题系列】
  • 【深度学习神经网络学习笔记(三)】向量化编程