day7作业

编写一个如下场景: 有一个英雄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) 实现功能:英雄既可以装备长剑,也可以装备短剑,也可以装备斧头,但是要求装备不同的武器,英雄需要获得不同的属性加成

cpp 复制代码
#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;
}
相关推荐
zyhomepage9 分钟前
科技的成就(六十八)
开发语言·人工智能·科技·算法·内容运营
slandarer10 分钟前
MATLAB | R2025a 更新了哪些有趣的东西?
开发语言·matlab
瑞雪兆丰年兮12 分钟前
数学实验(Matlab编程基础)
开发语言·算法·matlab·数学实验
✿ ༺ ོIT技术༻15 分钟前
笔试强训:Day5
c++·算法
努力的小帅30 分钟前
C++_STL_map与set
开发语言·数据结构·c++·学习·leetcode·刷题
yezipi耶不耶43 分钟前
Rust入门之高级Trait
开发语言·后端·rust
双叶8361 小时前
(C语言)超市管理系统 (正式版)(指针)(数据结构)(清屏操作)(文件读写)
c语言·开发语言·数据结构·c++·windows
为美好的生活献上中指1 小时前
java每日精进 5.14【参数校验】
java·开发语言·spring boot·tomcat
末央&1 小时前
【数据结构】手撕AVL树(万字详解)
数据结构·c++
序属秋秋秋1 小时前
《数据结构初阶》【二叉树 精选9道OJ练习】
c语言·数据结构·c++·算法·leetcode