day7作业

news/2025/2/28 3:16:10/

编写一个如下场景: 有一个英雄Hero类,私有成员,攻击(Atx),防御(Defense),速度(Speed),生命值(Blood),以及所有的set get 方法 编写一个 武器 Weapon 类,拥有私有成员攻击力(Atx),以及set get 方法 编写一个 长剑 Sword 类,继承自武器类,拓展属性 生命值(Blood),以及set get 方法 编写一个 匕首Blade类,继承自武器类,拓展属性 速度(Speed),以及set get 方法 编写一个 斧头 Axe类,继承自武器类,拓展属性 防御力(Defense),以及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 Atx;int Defense;int Speed;int Blood;
public:Hero(int Atx=0,int Defense=0,int Speed=0,int Blood=0):Atx(Atx),Defense(Defense,Speed(Speed),Blood(Blood){}void setAtx(int atx) { Atx = atx; }int getAtx() const { return Atx; }void setDefense(int defense) { Defense = defense; }int getDefense() const { return Defense; }void setSpeed(int speed) { Speed = speed; }int getSpeed() const { return Speed; }void setBlood(int blood) { Blood = blood; }int getBlood() const { return Blood; }
};class Weapon{
private:int Atx;
public:		Weapon(int Atx=0):Atx(Atx){}virtual ~Weapon() {}virtual void equip(Hero* h) = 0;virtual void unequip(Hero* h) = 0;void setAtx(int atx) { Atx = atx; }int getAtx() const { return Atx; }};
class Sword:public Weapon{private:int Blood;public:Sword(int Blood=50):Defense(Defense){}void equip(Hero* h) {Weapon::equip(h);h->setBlood(h->getBlood() + bloodBonus);}void setBlood(int blood) {Blood = blood; }int getBlood() const { return blood; }
};
}class Blade:public Weapon{
private:int Speed;public:Sword(int Speed=50):Defense(Defense){}void equip(Hero* h) {Weapon::equip(h);h->setSpeed(h->getSpeed() + Speed;}void setSpeed(int speed) { Speed = speed; }int getSpeed() const { return speed; }
};}
class Axe:public Weapon{
private:int Defense;public:Axe(int Defense=50):Defense(Defense){}void equip(Hero* h) {Weapon::equip(h);h->setDefense(h->getDefense() + Defenss);}void setDefenseBonus(int defense) { Denfense = defense; }int getDefense() const { return Defense; }
};
}int main(int argc,const char** argv){Hero hero;Sword sword;Blade blade;Axe axe;
}


http://www.ppmy.cn/news/1575402.html

相关文章

工程师 - VSCode的AI编码插件介绍: MarsCode

豆包 MarsCode MarsCode AI: Coding Assistant Code and Innovate Faster with AI 豆包 MarsCode - 编程助手 安装完成并使能后&#xff0c;会在下方状态栏上显示MarsCode AI。 安装完并重启VSCode后&#xff0c;要使用这个插件&#xff0c;需要注册一下账号。然后授权VSCod…

ipe网络安全

&#x1f345; 点击文末小卡片 &#xff0c;免费获取网络安全全套资料&#xff0c;资料在手&#xff0c;涨薪更快 什么是IDS&#xff1f; IDS是英文"Intrusion Detection Systems"的缩写&#xff0c;中文意思是"入侵检测系统"。 大家还记得「网络安全」安…

Vite vs Webpack

1. Vite 比 Webpack 快在哪里&#xff1f; 开发模式的差异 Webpack&#xff1a;在开发环境中&#xff0c;Webpack 是先打包再启动开发服务器。这意味着所有的模块都需要在开发前进行打包&#xff0c;这会增加启动时间和构建时间。 Vite&#xff1a;Vite 则是直接启动开发服务器…

iOS Framework代码中加载图片资源

在Framework中&#xff0c;加载UIImage(named:"icon.png")时&#xff0c;老是失败&#xff0c; 换成 UIImage(contentsOfFile: filePath) 也是不对&#xff1a; if let filePath Bundle.main.path(forResource: "icon", ofType: "png"){ icon …

精准识别IP应用场景

基于全球领先的IP应用场景识别服务IPv4/IPv6全量数据库&#xff0c;为企业提供高精度、低延迟的场景化解析能力&#xff0c;助您构建更安全、智能的网络生态。 ​精准识别&#xff0c;毫秒响应 全量数据覆盖&#xff0c;依托全球最大的IP地址库&#xff0c;支持IPv4/IPv6双协…

Vue 3 + Vite 项目中配置代理解决开发环境中跨域请求问题

在 Vue 3 Vite 项目中&#xff0c;配置代理是解决开发环境中跨域请求问题的常见方法。通过在 Vite 的配置文件中设置代理&#xff0c;可以将前端请求转发到后端服务器&#xff0c;从而避免浏览器的同源策略限制。 1. 创建 Vue 3 Vite 项目 首先&#xff0c;确保你已经安装了…

MySQL数据库SQL语句分析用户活跃情况

数据库建表语句 用户表 (users) CREATE TABLE users (user_id INT AUTO_INCREMENT PRIMARY KEY,register_date DATE NOT NULL,user_name VARCHAR(255) NOT NULL );这个表用于存储用户的基本信息&#xff0c; user_id 是用户的唯一标识&#xff0c; register_date 记录用户注册…

reCAPTCHA v3 实现笔记

一、概述 reCAPTCHA v3 是一种用于区分用户和机器人行为的安全验证服务。它通过在后台评估用户行为并返回一个分数来判断用户是否为真实用户。本文将介绍如何在前端和后端实现 reCAPTCHA v3 的集成。 二、注册和密钥生成 注册 reCAPTCHA v3 访问 Google reCAPTCHA 管理页面&a…