11.25c++继承、多态

练习:

cpp 复制代码
编写一个 武器类
class Weapon{
    int atk;
}

编写3个武器派生类:短剑,斧头,长剑
class knife{
    int spd;
}

class axe{
    int hp;
}

class sword{
    int def;
}

编写一个英雄类
class Hero{
    int atk;
    int def;
    int spd;
    int hp;
public:
    所有的get set 方法
    void equipWeapon(Weapon*)
    根据传入的武器不同,英雄获得不同的属性加成
}

代码实现:

cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <myhead.h>
using namespace std;  

class Weapon;
class Hero{
private:
	int atk;
	int def;
	int spd;
	int hp;
public:
	Hero(int atk=0,int def=0,int spd=0,int hp=0):spd(spd),hp(hp),def(def),atk(atk){}
	void setAtk(int atk){
		this->atk=atk;	
	}
	void setSpd(int spd){
		this->spd=spd;
	}
	void setHp(int hp){
		this->hp=hp;
	}
	void setDef(int def){
		this->def=def;
	}
	int getAtk(){
		return this->atk;
	}
	int getSpd(){
		return this->spd;
	}
	int getHp(){
		return this->hp;
	}
	int getDef(){
		return this->def;
	}
	void equipweapon(Weapon* w);
	void showProptery(){
		cout << "h p=" << hp << endl;
		cout << "spd=" << spd << endl;
		cout << "def=" << def << endl;
		cout << "atk=" << atk << endl;
		cout << "-------------" << endl;
	}
};
  
class Weapon{
private:
	int atk;
public:
	Weapon(int atk=0):atk(atk){}

	void setAtk(int atk){
		this->atk=atk;
	}
	int getAtk(){
		return this->atk;
	}
	virtual void addProptery(Hero& hero){}
};
class Knife:public Weapon{
private:
	int spd;
public:
	Knife(int atk=1,int spd=1):Weapon(atk),spd(spd){}
	void setSpd(int spd){
		this->spd=spd;
	}
	int getSpd(){
		return this->spd;
	}
	virtual void addProptery(Hero& hero){
		int atk=hero.getAtk()+this->getAtk();
		int spd=hero.getSpd()+this->spd;
		hero.setAtk(atk);
		hero.setSpd(spd);
	}
	
};
class Axe:public Weapon{
private:
	int hp;
public:
	Axe(int atk=1,int hp=1):Weapon(atk),hp(hp){}
	void setHp(int hp){
		this->hp=hp;
	}
	int getHp(){
		return this->hp;
	}
	virtual void addProptery(Hero& hero){
		int atk=hero.getAtk()+this->getAtk();
		int hp=hero.getHp()+this->hp;
		hero.setAtk(atk);
		hero.setHp(hp);
	}
};
class Sword:public Weapon{
private:
	int def;
public:
	Sword(int atk=1,int def=1):Weapon(atk),def(def){}
	void setDef(int def){
		this->def=def;
	}
	int getDef(){
		return this->def;
	}
	virtual void addProptery(Hero& hero){
		int atk=hero.getAtk()+this->getAtk();
		int def=hero.getDef()+this->def;
		hero.setAtk(atk);
		hero.setDef(def);
	}
};
void Hero::equipweapon(Weapon* w){
	w->addProptery(*this);
}

int main(int argc,const char **argv){
	Hero h1,h2,h3;
	Knife k;
	Sword s;
	Axe a;
	h1.equipweapon(&k);
	h2.equipweapon(&s);
	h3.equipweapon(&a);
	h1.showProptery();
	h2.showProptery();
	h3.showProptery();
	return 0;
}
相关推荐
CodeWithMe5 分钟前
【C/C++】EBO空基类优化介绍
开发语言·c++
404.Not Found15 分钟前
Day46 Python打卡训练营
开发语言·python
love530love16 分钟前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
凌辰揽月18 分钟前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
海奥华222 分钟前
go中的接口返回设计思想
开发语言·后端·golang
lifallen24 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
运维开发王义杰24 分钟前
Python: 告别 ModuleNotFoundError, 解决 pipx 环境下 sshuttle 缺少 pydivert 依赖的终极指南
开发语言·python
k要开心25 分钟前
从C到C++语法过度1
开发语言·c++
小吕学编程28 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
whoarethenext36 分钟前
使用 C/C++的OpenCV 实时播放火柴人爱心舞蹈动画
c语言·c++·opencv