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;
}
相关推荐
leiming621 小时前
c++ map容器
开发语言·c++·算法
杨校21 小时前
杨校老师课堂备赛C++信奥之模拟算法习题专项训练
开发语言·c++·算法
世洋Blog21 小时前
AStar算法基础学习总结
算法·面试·c#·astar·寻路
haing201921 小时前
七轴协作机器人运动学正解计算方法
算法·机器学习·机器人
谈笑也风生1 天前
把二叉搜索树转换为累加树(一)
算法
youngee111 天前
hot100-64跳跃游戏
算法·游戏
hd51cc1 天前
MFC 文档/视图 二
c++·mfc
wzfj123451 天前
认识lambda
c++
老王熬夜敲代码1 天前
C++万能类:any
开发语言·c++·笔记
liu****1 天前
机器学习-线性回归
人工智能·python·算法·机器学习·回归·线性回归