C++ 多态小练习

news/2025/2/26 17:18:28

编写一个如下场景:
有一个英雄Hero类,私有成员,攻击,防御,速度,生命值,以及所有的set get 方法
编写一个 武器 Weapon 类,拥有私有成员攻击力,以及set get 方法
编写一个 长剑 Sword 类,继承自武器类,拓展属性 生命值,以及set get 方法
编写一个 匕首Blade类,继承自武器类,拓展属性 速度,以及set get 方法
编写一个 斧头 Axe类,继承自武器类,拓展属性 防御力,以及set get 方法

武器Weapon类里面,要求有一个多态函数,叫做 equip 函数
英雄Hero类里面,要求有一个公开函数,equipWeapon(Weapon* w)
实现功能:英雄既可以装备长剑,也可以装备短剑,也可以装备斧头,但是要求装备不同的武器,英雄需要获得不同的属性加成

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;                 

class Hero{
private:
	int attack;
	int defense;
	int speed;
	int health;
public:
	Hero(int a,int d,int s,int h):attack(attack),defense(defense),speed(speed),health(health)
	{
		attack=a;
		defense=d;
		speed=s;
		health=h;
	}

	void set_attack(int val){attack+=val;}
	void set_defense(int val){defense+=val;}
	void set_speed(int val){speed+=val;}
	void set_health(int val){health+=val;}

	int get_attack(){return attack;}
	int get_defense(){return defense;}
	int get_speed(){return speed;}
	int get_health(){return health;}

	void equipWeapon(class Weapon * w);

	void show()
	{
		cout<<"attack= "<<attack<<endl;
		cout<<"defense= "<<defense<<endl;
		cout<<"speed= "<<speed<<endl;
		cout<<"health= "<<health<<endl;
	}
};


class Weapon{
private:
	int attack;
public:
	Weapon():attack(50){}
	void set_attack(int val){attack=val;}
	int get_attack(){return attack;}

	virtual int* equip()=0;
};


class Sword:public Weapon{
private:
	int health;
public:

	Sword():health(50){}
	void set_health(int val){health=val;}
	int get_health(){return health;}
	
	int* equip()
	{
		int att=get_attack();
		int d=0;
		int s=0;
		int h=health;
		int* arr=new int[4]{att,d,s,h};
		return arr;
	}
};


class Blade:public Weapon{
private:
	int speed;
public:
	Blade():speed(50){}
	void set_speed(int val){speed=val;}
	int get_speed(){return speed;}

	int* equip()
	{
		int att=get_attack();
		int d=0;
		int s=speed;
		int h=0;
		int* arr=new int[4]{att,d,s,h};
		return arr;

	}
};


class Axe:public Weapon{
private:
	int defense;
public:
	void set_defense(int val){defense=val;}
	int get_defense(){return defense;}

	int* equip()
	{
		int att=get_attack();
		int d=defense;
		int s=0;
		int h=0;
		int* arr=new int[4]{att,d,s,h};
		return arr;

	}
};


void Hero::equipWeapon(class Weapon* w)
{
	int* data=w->equip();
	set_attack(data[0]);
	set_defense(data[1]);
	set_speed(data[2]);
	set_health(data[3]);

	delete[] data;
}

int main(int argc,const char** argv){
	Hero lwl(100,50,50,200);

	Sword sword;
	Blade blade;
	Axe axe;
	
	Weapon* w[4]={&sword,&blade,&axe};
	
	lwl.equipWeapon(w[0]);

	lwl.show();
}

运行截图:


http://www.niftyadmin.cn/n/5868978.html

相关文章

【linux配置】 修改内核网络参数

命令解释 echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore 这个命令的具体含义是&#xff1a; echo 1&#xff1a;将值1写入文件。 /proc/sys/net/ipv4/conf/all/arp_ignore&#xff1a;将值1写入 /proc/sys/net/ipv4/conf/all/arp_ignore 文件&#xff0c;从而修改内核参…

【前端】【面试】【树】JavaScript 树形结构与列表结构的灵活转换:`listToTree` 与 `treeToList` 函数详解

JavaScript 树形结构与列表结构的灵活转换&#xff1a;listToTree 与 treeToList 函数详解 在前端开发的数据处理工作中&#xff0c;树形结构和列表结构是两种常见的数据形式。树形结构能够清晰展示数据间的层级关系&#xff0c;适合用于菜单、组织架构等场景&#xff1b;而列…

【HarmonyOS Next】鸿蒙应用公钥和证书MD5指纹的获取

【HarmonyOS Next】鸿蒙应用公钥和证书MD5指纹的获取 一、问题背景 政府的icp备案时&#xff0c;或者某些三方SDK以来的管理后台&#xff0c;都需要配置鸿蒙应用的公钥和证书MD5指纹 二、解决方案 专有名词解释&#xff1a; 华为AppGallery Connect简称 AGC平台&#xff0…

DeepSeek 开源周:DeepEP 项目详解,GPU 压榨计划启动!

引言 就在今天&#xff0c;2025年2月25日&#xff0c;DeepSeek 再次为人工智能社区带来了一场技术盛宴——DeepEP 项目的开源。这个旨在优化 GPU 性能的工具一经发布便迅速获得了广泛的关注和赞誉&#xff0c;短短两小时内就斩获了超过1000个 Star。本文将详细介绍 DeepEP 的功…

JNA基础使用,调用C++返回结构体

C端 test.h文件 #pragma oncestruct RespInfo {char* path;char* content;int statusCode; };extern "C" { DLL_EXPORT void readInfo(char* path, RespInfo* respInfo); }test.cpp文件 #include "test.h"void readInfo(char* path, RespInfo* respInfo…

android 新增native binder service 方式(三)

书接上回&#xff0c;继续第三种方式&#xff0c;是手动生成 service binder 的方法,项目结构 1&#xff0c;编译aidl aidl 文件保持不变&#xff0c;如何生成Bn和Bp 文件呢。 aidl -I ./libserviceaidl/aidl -h ./ -o ./ --langcpp libserviceaidl/aidl/com/test/IService.a…

【LeetCode 热题100】 240. 搜索二维矩阵 II的算法思路及python代码

240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m n m \times n mn 矩阵 m a t r i x matrix matrix 中的一个目标值 t a r g e t target target。该矩阵具有以下特性&#xff1a; 每行的元素从左到右升序排列。每列的元素从上到下升序排列。 示例 1&#xff1a; 输入…

4*A100 部署 deepseek-r1-671B

部署deepseek-r1-671B 使用 4*A100 部署 deepseek-r1-671b-1.58bit 大模型。 环境 ubuntu22.04LTScuda 12.2.0 要求 内存&#xff1a; 256GB及以上显存&#xff1a; 256GB及以上&#xff08;160G可以跑起来&#xff0c;但对于长上下文容易oom&#xff09;&#xff0c;这里…