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;
}
相关推荐
破茧蝴蝶5 小时前
Python分布式爬虫完整教程
开发语言·python
狗凯之家源码网5 小时前
全新 PHP 轻量客服聊天室源码深度评测
开发语言·php·聊天室系统
远离UE46 小时前
UE5 Substrate Thin Film 源码拆析
开发语言·javascript·ue5
keyipatience6 小时前
JavaScript学习(1)书写位置,变量、数据类型、运算符
开发语言·前端·javascript·css·学习
一拳一个呆瓜6 小时前
【STL】文件系统导航
c++·stl
All for pursuit.7 小时前
【动态规划-21】312.戳气球
数据结构·c++·算法·leetcode·动态规划
栈帧之外7 小时前
【C++】STL之set
开发语言·c++
catchadmin8 小时前
免费可商用 PHP 管理后台 CatchAdmin V5.4.1 发布,新增 MCP 支持
开发语言·php
zhanghaha13148 小时前
C++构造函数的一些其他应用(逐词逐句保姆讲解)
c++
jimy19 小时前
c++的 Type Traits (二、更详细版本)
开发语言·c++