模拟一个游戏场景
有一个英雄:初始所有属性为1
atk,def,apd,hp
游戏当中有以下3种武器
长剑Sword: 装备该武器获得 +1atx,+1def
短剑Blade: 装备该武器获得 +1atk,+1spd
斧头Axe: 装备该武器获得 +1atk,+1hp
要求英雄写一个函数:
equipweapon 装备武器
实现效果:装备不同武器英雄获得不同的属性加成
/*
有一个英雄:初始所有属性为1
atk,def,spd,hp
游戏当中有以下3种武器
长剑Sword:装备该武器获得+1atk,+1def
短剑Blade:装备该武器获得+1atk,+1spd
斧头Axe:装备该武器获得+1atk,+1hp
*/
#include <iostream>
using namespace std;
class Hero;
class Weapon
{
public:
int atk;
Weapon():atk(1){}
virtual void update(Hero &h);
int get_atk1();
virtual ~Weapon(){}
};
class Sword:public Weapon
{
int def;
public:
Sword():def(1){}
void update(Hero &h);
};
class Blade:public Weapon
{
int spd;
public:
Blade():spd(1){}
void update(Hero &h);
};
class Aex:public Weapon
{
int hp;
public:
Aex():hp(1){}
void update(Hero &h);
};
class Hero
{
int atk;
int def;
int spd;
int hp;
public:
Hero():atk(1),def(1),spd(1),hp(1){}
void set_atk(int atk);
void set_def(int def);
void set_spd(int spd);
void set_hp(int hp);
int get_atk();
int get_def();
int get_spd();
int get_hp();
void updateWeapon(Weapon *w);
void show();
};
void Hero::set_atk(int atk)
{
this->atk=atk;
}
void Hero::set_def(int def)
{
this->def=def;
}
void Hero::set_spd(int spd)
{
this->spd=spd;
}
void Hero::set_hp(int hp)
{
this->hp=hp;
}
int Hero::get_atk()
{
return this->atk;
}
int Hero::get_def()
{
return this->def;
}
int Hero::get_spd()
{
return this->spd;
}
int Hero::get_hp()
{
return this->hp;
}
void Weapon::update(Hero &h)
{
int new_atk=h.get_atk()+this->atk;
h.set_atk(new_atk);
}
void Sword::update(Hero &h)
{
Weapon::update(h);
int new_def=h.get_def()+this->def;
h.set_def(new_def);
}
void Blade::update(Hero &h)
{
Weapon::update(h);
int new_spd=h.get_spd()+this->spd;
h.set_spd(new_spd);
}
void Aex::update(Hero &h)
{
Weapon::update(h);
int new_hp=h.get_hp()+this->hp;
h.set_hp(new_hp);
}
void Hero::updateWeapon(Weapon *w)
{
w->update(*this);
delete w;
}
void Hero::show()
{
cout<<"atk="<<atk<<endl;
cout<<"def="<<def<<endl;
cout<<"spd="<<spd<<endl;
cout<<"hp="<<hp<<endl;
}
int main()
{
Hero h1,h2,h3;
h1.updateWeapon(new Sword);
h2.updateWeapon(new Blade);
h3.updateWeapon(new Aex);
h1.show();
h2.show();
h3.show();
return 0;
}