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;

}
相关推荐
苏宸啊4 小时前
IPC管道
linux·c++
BestOrNothing_20154 小时前
ROS2 话题通信实战:消息对象、Publisher 发布器与 Subscriber 订阅器保姆级教程
c++·ros2·subscriber·publisher·话题通信
艾iYYY5 小时前
string 类的模拟实现
android·服务器·c语言·c++·算法
为何创造硅基生物6 小时前
C++ virtual void StartNetwork() = 0; // 纯虚:子类必须实现,否则不能 new。
c++
知无不研6 小时前
对套接字的深入理解
linux·服务器·网络·c++·socket·网络套接字
hai3152475438 小时前
FlashAttention C语言(C++)实现(展示版)
c语言·开发语言·c++·人工智能·算法
wuminyu8 小时前
Java锁机制之Java对象重量级锁源码剖析
java·linux·c语言·jvm·c++
郝学胜_神的一滴9 小时前
Qt 高级开发 026:QTabWidget御道,从筑基到化境
c++·qt
apocelipes9 小时前
GNU GCC 多版本函数扩展
c语言·c++·linux编程
代码中介商10 小时前
C++完美转发与引用折叠全解析
开发语言·c++