C++多态

洗手液战神: 02-25 19:13:52 作业: 编写一个如下场景: 有一个英雄Hero类,私有成员,攻击,防御,速度,生命值,以及所有的set get 方法 编写一个 武器 Weapon 类,拥有私有成员攻击力,以及set get 方法 编写一个 长剑 Sword 类,继承自武器类,拓展属性 生命值,以及set get 方法 编写一个 匕首Blade类,继承自武器类,拓展属性 速度,以及set get 方法 编写一个 斧头 Axe类,继承自武器类,拓展属性 防御力,以及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 Weapon{//武器
	private:
		int power;
	public:
		Weapon(int attack):power(attack){}
		virtual ~Weapon(){}

		void setPower(int attack){power=attack;}
		int getPower()const{return power;}
		virtual void equip(class Hero* hero)=0;
};

class Hero{//英雄
	private:
		int attack;
		int defense;
		int speed;
		int health;
	public:
		Hero(int atk,int def,int spd,int hp)
			:attack(atk),defense(def),speed(spd),health(hp)	{}

		void setAttack(int atk){attack=atk;}
		void setDefense(int def){defense=def;}
		void setSpeed(int spd){speed=spd;}
		void setHealth(int hp){health=hp;}

		int getAttack(){return attack;}
		int getDefense(){return defense;}
		int getSpeed(){return speed;}
		int getHealth(){return health;}

		void equipWeapon(Weapon* w)
		{
			w->equip(this);
		}
};


class Sword:public Weapon{//长剑
	private:
		int extra_hp;
	public:
		Sword(int attack,int health):Weapon(attack),extra_hp(health){}

		void setExtra_hp(int health){extra_hp=health;}
		int getExtra_hp(){return extra_hp;}
		void equip(Hero* hero){
			hero->setAttack(hero->getAttack()+getPower());
			hero->setHealth(hero->getHealth()+extra_hp);
		}
};


class Blade:public Weapon{//匕首
	private:
		int extra_spd;
	public:
		Blade(int attack,int speed):Weapon(attack),extra_spd(speed){}

		void setExtra_spd(int speed){extra_spd=speed;}
		int getExtra_spd(){return extra_spd;}
		void equip(Hero* hero){
			hero->setAttack(hero->getAttack()+getPower());
			hero->setSpeed(hero->getSpeed()+extra_spd);
		}
};

class Axe:public Weapon{//巨斧
	private:
		int extra_def;
	public:
		Axe(int attack,int defense):Weapon(attack),extra_def(defense){}

		void setExtra_def(int defense){extra_def=defense;}
		int getExtra_def(){return extra_def;}
		void equip(Hero* hero){
			hero->setAttack(hero->getAttack()+getPower());
			hero->setDefense(hero->getDefense()+extra_def);
		}
};
int main(int argc,const char** argv){

	Hero hero(100,100,100,50);
	Sword sword(5, 20);
	Blade blade(3, 15);
	Axe axe(8, 10);

	hero.equipWeapon(&sword);
	std::cout << "装备长剑 - 攻击力: " << hero.getAttack()
		<< ", 生命值: " << hero.getHealth() << std::endl;

	hero.equipWeapon(&blade);
	std::cout << "装备匕首 - 攻击力: " << hero.getAttack()
		<< ", 速度: " << hero.getSpeed() << std::endl;

	hero.equipWeapon(&axe);
	std::cout << "装备巨斧 - 攻击力: " << hero.getAttack()
		<< ", 防御力: " << hero.getDefense() << std::endl;

}
相关推荐
河阿里16 分钟前
深入理解LRU缓存机制:从原理到应用(C++实现
开发语言·c++·缓存
唠玖馆36 分钟前
c++ 栈和队列
c++
邪修king40 分钟前
UE5 C++ 游戏性能优化:大一也能学会的实战级优化指南
c++·游戏·ue5
诙_41 分钟前
深入理解C++--STL
开发语言·c++
ximu_polaris1 小时前
C++高频面试题汇总
c++·面试
TANGLONG2221 小时前
【C++】STL基础必备:深入解析vector容器的实现(含源码)
c语言·开发语言·数据结构·c++·笔记·算法·stl
paeamecium1 小时前
【PAT甲级真题】- Spell It Right(20)
c++·pat考试·pat
慢慢向上的蜗牛1 小时前
Atlas300I推理卡驱动适配Linux 6.12+内核
linux·c++·人工智能·华为·驱动·底层开发·ascend
码农小韩1 小时前
QT学习记录(三)——C++学习基础(三)
开发语言·c++·qt·学习·算法·嵌入式软件
承渊政道1 小时前
【动态规划算法】(似包非包以及卡特兰数问题深入解析)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法