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

效果

相关推荐
vortex53 分钟前
探索 Shell:选择适合你的命令行利器 bash, zsh, fish, dash, sh...
linux·开发语言·bash·shell·dash
zzc9215 分钟前
MATLAB仿真生成无线通信网络拓扑推理数据集
开发语言·网络·数据库·人工智能·python·深度学习·matlab
HUN金克斯14 分钟前
C++/C函数
c语言·开发语言·c++
慢半拍iii15 分钟前
数据结构——F/图
c语言·开发语言·数据结构·c++
钢铁男儿17 分钟前
C# 表达式和运算符(表达式和字面量)
开发语言·c#
编程有点难20 分钟前
Python训练打卡Day43
开发语言·python·深度学习
m0_6371469326 分钟前
零基础入门 C 语言基础知识(含面试题):结构体、联合体、枚举、链表、环形队列、指针全解析!
c语言·开发语言·链表
LjQ204034 分钟前
网络爬虫一课一得
开发语言·数据库·python·网络爬虫
你是狒狒吗42 分钟前
TM中,return new TransactionManagerImpl(raf, fc);为什么返回是new了一个新的实例
java·开发语言·数据库