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;
}

效果

相关推荐
白帽攻防录7 小时前
SRC 挖洞:PostScript 打印机驱动双 RCE,打印服务器怎么变成内网跳板
开发语言·网络安全·php·内网安全
十年Java程序媛7 小时前
Java枚举Enum完整教程|替代静态常量,开发与面试要点
java·开发语言·spring boot·后端·面试
2603_965148118 小时前
电商选品调研:通过关键词搜索API获取市场热销商品,结合商品详情API分析竞品卖点与定价
开发语言·数据库·人工智能·python·信息可视化
所以遗憾是什么呢?8 小时前
【题解】Educational Codeforces Round 195 (Rated for Div. 2) ABCDE
c++·动态规划·acm·icpc·ccpc·xcpc·codeforecs
xixiaoyunya8 小时前
Python 异步编程入门:用 asyncio 轻松实现并发任务
开发语言·python·php
学逆向的9 小时前
C 循环结构的执行效率分析
c语言·开发语言·数据结构·网络安全
小坏讲微服务9 小时前
刚发布一个月的JDK 27,为什么我建议你立刻升级?
java·开发语言·spring boot·spring·架构·求职招聘·springboot4
Lhan.zzZ10 小时前
Qt WebEngineView 在 Linux 上无法加载网页:从“看不见 HTML“到“禁用沙箱“
linux·c++·qt
打工仔折腾 AI10 小时前
用 Dashlet 把极空间 NAS 服务入口整理成统一导航首页的实践
java·开发语言·人工智能·后端·python·rpc·ai agent 实战
MATLAB代码顾问11 小时前
Simulink PI 参数扫描与批量仿真完整 MATLAB 代码
开发语言·matlab