当前位置: 首页 > article >正文

2.25作业

作业:
编写一个如下场景:
有一个英雄Hero类,私有成员,攻击,防御,速度,生命值,以及所有的setget方法
编写一个 武器 Weapon 类,拥有私有成员攻击力,以及set get 方法
编写一个 长剑 Sword 类,继承自武器类,拓展属性 生命值,以及set get 方法
编写一个 匕首Blade类,继承自武器类,拓展属性 速度,以及set get 方法
编写一个 斧头 Axe类,继承自武器类,拓展属性 防御力,以及set get 方法
武器Weapon类里面,要求有一个多态函数,叫做 equip 函数
英雄Hero类里面,要求有一个公开函数,equipWeapon(Weapon*w)
实现功能:英雄既可以装备长剑,也可以装备短剑,也可以装备斧头,但是要求装备不同的武器,英雄需要获得不同的属性加成

#include <iostream>
#include <cstring>

using namespace std;

class Weapon
{
	private:
		int weapon_attack;
	public:
		Weapon(int weapon_attack = 0)
			:weapon_attack(weapon_attack)
		{}
		
		void setWeapon_attack(int weapon_attack){this->weapon_attack = weapon_attack;}

		int getWeapon_attack(){return weapon_attack;}

		virtual int* equip(){return NULL;}
};

class Hero
{
	private:
		string user;
		int attack;
		int defense;
		int speed;
		int health;
	public:
		Hero(string user = NULL, int attack = 100,int defense = 100,int speed = 100,int health = 100)
			:user(user),attack(attack),defense(defense),speed(speed),health(health)
		{}

		void setUser(string user){this->user = user;}
		void setAttack(int attack){this->attack = attack;}
		void setDefense(int defense){this->defense = defense;}
		void setSpeed(int speed){this->speed = speed;}
		void setHealth(int health){this->health = health;}

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

		void equipWeapon(Weapon* w)
		{
			cout<<"玩家信息"<<endl;
			cout<<"------------------------------------------------"<<endl;
			cout<<"姓名\t"<<"攻击力\t"<<"防御力\t"<<"速度\t"<<"生命值"<<endl;
			cout<<user<<"\t"<<attack<<"\t"<<defense<<"\t"<<speed<<"\t"<<health<<endl;
			cout<<"-------------------------------------------------"<<endl;
			int *attributes = w->equip();
			cout<<endl;
			cout<<endl;
			cout<<"综合属性"<<endl;
			cout<<"------------------------------------------------"<<endl;
			cout<<"姓名\t"<<"攻击力\t"<<"防御力\t"<<"速度\t"<<"生命值"<<endl;
			cout<<user<<"\t"<<attack+attributes[0]<<"\t"<<defense+attributes[1]<<"\t"<<speed+attributes[2]<<"\t"<<health+attributes[3]<<endl;
			cout<<"-------------------------------------------------"<<endl;


		}
};

class Sword:public Weapon
{

	public:
		void setWeapon_attack(int weapon_attack){Weapon::setWeapon_attack(weapon_attack);}

		int getWeapon_attack(){return Weapon::getWeapon_attack();}
		
		 int* equip()
		{
			cout<<"武器名:"<<"长剑"<<endl;
			cout<<"攻击力: "<<"+100"<<endl;
			cout<<"拓展属性: "<<"生命值"<<"+70"<<endl;
			setWeapon_attack(100);
			int* attributes = new int[4];
			attributes[0] = getWeapon_attack();
			attributes[1] = 0;
			attributes[2] = 0;
			attributes[3] = 70;
			return attributes;
		}
};

class Blade:public Weapon
{
	public:
		void setWeapon_attack(int weapon_attack){Weapon::setWeapon_attack(weapon_attack);}

		int getWeapon_attack(){return Weapon::getWeapon_attack();}

		int* equip()
		{
			cout<<"武器名: "<<"匕首"<<endl;
			cout<<"攻击力: "<<"+50"<<endl;
			cout<<"拓展属性: "<<"速度"<<"+100"<<endl;
			setWeapon_attack(50);
			int* attributes = new int[4];
			attributes[0] = getWeapon_attack();
			attributes[1] = 0;
			attributes[2] = 100;
			attributes[3] = 0;
			return attributes;
		}
};

class Axe:public Weapon
{
	public:
		void setWeapon_attack(int weapon_attack){Weapon::setWeapon_attack(weapon_attack);}

		int getWeapon_attack(){return Weapon::getWeapon_attack();}

		int* equip()
		{
			cout<<"武器名: "<<"斧头"<<endl;
			cout<<"攻击力: "<<"+75"<<endl;
			cout<<"拓展属性: "<<"防御力"<<"+100"<<endl;
			cout<<"------------------------------------------------"<<endl;
			cout<<"姓名\t"<<"攻击力\t"<<"防御力\t"<<"速度\t"<<"生命值"<<endl;
			cout<<user<<"\t"<<attack<<"\t"<<defense<<"\t"<<speed<<"\t"<<health<<endl;
			cout<<"-------------------------------------------------"<<endl;
			setWeapon_attack(75);
			int* attributes = new int[4];
			attributes[0] = getWeapon_attack();
			attributes[1] = 100;
			attributes[2] = 0;
			attributes[3] = 0;
			return attributes;
		}
};


int main()
{
	Hero m("张三");
	Weapon* ptr = new Sword;
	m.equipWeapon(ptr);
	return 0;
}

效果


http://www.kler.cn/a/561422.html

相关文章:

  • 【Linux探索学习】第三十一弹——线程互斥与同步(下):深入理解确保线程安全的机制
  • IDEA撤销commit
  • OpenHarmony全球化子系统
  • C 语言 “神秘武器”:联合体与枚举大揭秘!
  • 深度学习-131-RAG技术之基于Anything LLM搭建本地私人知识库的应用策略问题总结(二)
  • HOMIE:具有同构外骨骼座舱的人形机器人操控
  • 冯诺依曼体系结构 ──── linux第8课
  • vue-signature-pad插件实现移动端签字功能(css,js)+将签名照片旋转90度之后的base64码传给后端
  • npm i 失败权限问题
  • 详解:用Python OpenCV库来处理图像并测量物体的长度
  • 计算机考研之数据结构:斐波那契数列专题(1)
  • Linux设备驱动开发-SPI驱动开发详解(包含设备树处理详细过程)
  • 第4章 Unicode 文本和字节序列
  • 神经网络 - 神经元
  • 【压力测试】要不要做全链路压测?
  • Jasper AI技术浅析(四):自然语言处理(NLP)与生成技术
  • 4部署kibana:5601
  • 【Python】2.获取pypi的api token 并把自己写好的库上传到pypi(保姆级图文)
  • 在 Windows 下的 Docker 中安装 R语言
  • Linux下安装Nginx服务及systemctl方式管理nginx详情