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;

}
相关推荐
small_wh1te_coder19 分钟前
c语言超详细知识点总结 1500行手写源码 持续更新中ing 从25年5月到6月5日
c++·c
SteveDraw2 小时前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
十五年专注C++开发2 小时前
设计模式之单例模式(二): 心得体会
开发语言·c++·单例模式·设计模式
?!7142 小时前
算法打卡第18天
c++·算法
zh_xuan3 小时前
c++ std::pair
开发语言·c++
CodeWithMe3 小时前
【C/C++】EBO空基类优化介绍
开发语言·c++
k要开心3 小时前
从C到C++语法过度1
开发语言·c++
whoarethenext4 小时前
使用 C/C++的OpenCV 实时播放火柴人爱心舞蹈动画
c语言·c++·opencv
能工智人小辰4 小时前
Codeforces Round 509 (Div. 2) C. Coffee Break
c语言·c++·算法
梦星辰.4 小时前
VSCode CUDA C++进行Linux远程开发
linux·c++·vscode