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

效果

相关推荐
肆忆_14 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星18 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc