C++继承 多态

cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class Weapon;
class Hero{
private:
	string Name;
	int Attack;
	int Defense;
	int Speed;
	int Health;
public:
	Hero():Name(""),Attack(0),Defense(0),Speed(0),Health(0){}

	void setName(string name)
	{
		Name = name;
	}
	void setAttack(int attcak)
	{
		Attack = attcak;
	}


	void setDefense(int defense)
	{
		Defense = defense;
	}


	void setSpeed(int speed)
	{
		Speed = speed;
	}


	void setHealth(int health)
	{
		Health = health;
	}
	string getName(){return Name;}

	int getAttack(){return Attack;}

	int getDefense(){return Defense;}

	int getSpeed(){return Speed;}


	int getHealth(){return Health;}
	void equipWeapon(Weapon* w);
};

class Weapon
{
	private:
		int Attack;
	public:
		Weapon(int attcak = 0 ):Attack(attcak){}
	void 	setAttack(int attcak)
		{
			Attack = attcak;
		}

	int	getAttack(){return Attack;}
		virtual void equip(Hero* hero){}
};


class Sword:public Weapon
{
	private:
		int Health;
	public:
		Sword(int health = 0):Health(health){}
		void setHealth(int health)
		{
			Health = health;
		}
		int getHealth(){return Health;}
		void equip(Hero* hero)
		{
			hero->setAttack(hero->getAttack() + getAttack());
			hero->setHealth(hero->getHealth() + Health);
		}
};

class Blade:public Weapon
{
	private:
		int Speed;
	public:
		Blade(int speed = 0):Speed(speed){}
		void setSpeed(int speed)
		{
			Speed = speed;
		}
		int getSpeed(){return Speed;}

		void equip(Hero* hero)
		{
			hero->setAttack(hero->getAttack() + getAttack());
			hero->setSpeed(hero->getSpeed() + Speed);
		}
};


class Axe:public Weapon
{
	private:
		int Defense;
	public:
		Axe(int defense = 0):Defense(defense){}
		void setDefense(int defense)
		{
			Defense = defense;
		}
		int getDefense(){return Defense;}
		void equip(Hero* hero)
		{
			hero->setAttack(hero->getAttack() + getAttack());
			hero->setDefense(hero->getDefense() + Defense);
		}
};

void Hero::equipWeapon(Weapon* w) 
{
    w->equip(this);
}

int main(int argc,const char** argv){
	//创建英雄
	Hero hero;
	hero.setName("亚索");
	hero.setAttack(51);
	hero.setDefense(18);
	hero.setSpeed(350);
	hero.setHealth(512);

	//创建长剑,装备武器
	Sword sword;
	sword.setAttack(20);
	sword.setHealth(50);
	hero.equipWeapon(&sword);
	cout << "装备长剑后" << endl;
	cout << "名称:" << hero.getName() << endl;
	cout << "攻击:" << hero.getAttack() << endl;
	cout << "防御:" << hero.getDefense() << endl;
	cout << "生命:" << hero.getHealth() << endl;
	cout << "速度:" << hero.getSpeed() << endl;
	cout << "-------------------------------" << endl;

	//创建匕首,装备武器
	Blade blade;
	blade.setAttack(30);
	blade.setSpeed(60);
	hero.equipWeapon(&blade);
	cout << "装备匕首后" << endl;
	cout << "名称:" << hero.getName() << endl;
	cout << "攻击:" << hero.getAttack() << endl;
	cout << "防御:" << hero.getDefense() << endl;
	cout << "生命:" << hero.getHealth() << endl;
	cout << "速度:" << hero.getSpeed() << endl;
	cout << "-------------------------------" << endl;
	//创建黑切,装备武器
	Axe axe;
	axe.setAttack(80);
	axe.setDefense(60);
	cout << "装备黑切后" << endl;
	cout << "名称:" << hero.getName() << endl;
	cout << "攻击:" << hero.getAttack() << endl;
	cout << "防御:" << hero.getDefense() << endl;
	cout << "生命:" << hero.getHealth() << endl;
	cout << "速度:" << hero.getSpeed() << endl;
	cout << "-------------------------------" << endl;
}

效果

相关推荐
游离状态的猫19 分钟前
JavaScript性能优化实战:从瓶颈定位到极致提速
开发语言·javascript·性能优化
GeekABC9 分钟前
FastAPI系列06:FastAPI响应(Response)
开发语言·python·fastapi·web
小彭努力中10 分钟前
7.Three.js 中 CubeCamera详解与实战示例
开发语言·前端·javascript·vue.js·ecmascript
why1511 小时前
腾讯(QQ浏览器)后端开发
开发语言·后端·golang
charade3121 小时前
【C语言】内存分配的理解
c语言·开发语言·c++
LinDaiuuj1 小时前
判断符号??,?. ,! ,!! ,|| ,&&,?: 意思以及举例
开发语言·前端·javascript
小臭希2 小时前
Java——琐碎知识点一
java·开发语言
淋一遍下雨天3 小时前
Spark Streaming核心编程总结(四)
java·开发语言·数据库
小白学大数据3 小时前
如何避免爬虫因Cookie过期导致登录失效
开发语言·爬虫·python·scrapy
爱吃烤鸡翅的酸菜鱼4 小时前
【SpringMVC】概念引入与连接
java·开发语言·mysql