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;
}
相关推荐
richxu2025100128 分钟前
C语言<<超全.超重要>>知识点总结
c语言·开发语言
2501_9160088930 分钟前
用多工具组合把 iOS 混淆做成可复用的工程能力(iOS混淆|IPA加固|无源码混淆|Ipa Guard|Swift Shield)
android·开发语言·ios·小程序·uni-app·iphone·swift
胎粉仔30 分钟前
Swift 初阶 —— inout 参数 & 数据独占问题
开发语言·ios·swift·1024程序员节
WBluuue37 分钟前
数据结构与算法:摩尔投票算法
c++·算法·leetcode
Lizhihao_38 分钟前
Python如何写Selenium全攻略
开发语言·python
柯一梦1 小时前
深入解析C++ String类的实现奥秘
c++
「QT(C++)开发工程师」1 小时前
嵌入式Lua脚本编程核心概念
开发语言·lua
_extraordinary_1 小时前
Java Spring事务,事务的传播机制
java·开发语言·spring
蜗牛沐雨2 小时前
详解c++中的文件流
c++·1024程序员节
无聊的小坏坏2 小时前
从零开始:C++ TCP 服务器实战教程
服务器·c++·tcp/ip