C++ 多态作业练习

作业1、

编写一个英雄类

class Hero{

int atk;

int def;

int spd;

int hp;

public:

所有的get set 方法

void equipWeapon(Weapon*)

根据传入的武器不同,英雄获得不同的属性加成

}

cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
using namespace std;

class Hero
{
private:
	int property; //属性
public:	
	Hero(int property = 0):property(property){}
	void addproperty(int buff)
	{
		property += buff;
	}

	int getproperty()
	{
		return property;
	}

	virtual void Igetproperty(){}
};

class atk: public Hero
{
public:
	atk(int property = 0):Hero(property){}
	virtual void Igetproperty()
	{
		addproperty(30);
	}
};

class def: public Hero
{
public:
	def(int property = 0):Hero(property){}
	virtual void Igetproperty()
	{
		addproperty(10);
	}

};

class spd: public Hero
{
public:
	spd(int property = 0):Hero(property){}
	virtual void Igetproperty()
	{ 
		addproperty(20);
	}

};

class hp: public Hero
{
public:	
	hp(int property = 0):Hero(property){}
	virtual void Igetproperty()
	{
   		addproperty(900);
	}

};

void propertyshow(Hero** addr)
{
	for(int i=0;addr[i]!= NULL;i++)
	{
		addr[i]->Igetproperty();
		cout << addr[i]->getproperty() << endl;
	}
}


int main(int argc,const char** argv)
{
	atk a;
	def b;
	spd c;
	hp d;
	Hero* addr[5] = {&a,&b,&c,&d};
	propertyshow(addr);

	return 0;
}
相关推荐
NiNi_suanfa21 小时前
【Qt】Qt 批量修改同类对象
开发语言·c++·qt
信奥胡老师1 天前
苹果电脑(mac系统)安装vscode与配置c++环境,并可以使用万能头文件全流程
c++·ide·vscode·macos·编辑器
妖灵翎幺1 天前
C++ 中的 :: 操作符详解(一切情况)
开发语言·c++·ide
风筝在晴天搁浅1 天前
代码随想录 718.最长重复子数组
算法
kyle~1 天前
算法---回溯算法
算法
star _chen1 天前
C++实现完美洗牌算法
开发语言·c++·算法
hzxxxxxxx1 天前
1234567
算法
繁星星繁1 天前
【C++】脚手架学习笔记 gflags与 gtest
c++·笔记·学习
Sylvia-girl1 天前
数据结构之复杂度
数据结构·算法
CQ_YM1 天前
数据结构之队列
c语言·数据结构·算法·