Description
在一個平面打鬥遊戲中,任何的角色(Role)都有血量(blood)和位置loc(此處loc是Location類的實例)屬性。有了Role類,可以派生出不同的角色,如人、神仙、怪獸等。如下程序中,定義了Location類和Role類,人類(Human)中新增了姓名和攻擊力數據成員,請爲Human類設計成員函數,並實現Role類中的moveTo和addBlood兩個成員函數。
請在begin和end中間寫下需要的代碼。你只能編輯並提交begin和end之間的代碼。
#include <iostream>
using namespace std;
class Location
{
private:
int x, y;
public:
Location(int a, int b):x(a),y(b) {}
int getX(){return x;}
int getY(){return y;}
void setXY(int a,int b) {x=a;y=b;}; //設置位置座標
};
class Role
{
public:
Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {}
void moveTo(int rx, int ry); //移動到(rx, ty)處,要改變loc的值
void addBlood(int b); //增加血量,參數爲負時,代表減少
protected:
int blood;
Location loc;
};
void Role::moveTo(int rx, int ry)
{
loc.setXY(rx,ry);
}
void Role::addBlood(int b)
{
blood+=b;
}
//************* begin *****************
class Human: public Role
{
public:
private:
string name; // 姓名
int attack; // 攻擊力
};
//************* end *****************
int main()
{
string name;
int att, blood, x, y;
cin>>name>>att>>blood>>x>>y;
Human hum(name,att,blood,x,y); //人name的攻擊力att,血量blood,在(x,y)處
hum.show();
int incAtt, incBlood, newx, newy ;
cin>>incAtt;
cin>>incBlood;
cin>>newx>>newy;
hum.addAttack(incAtt); //攻擊力增加incAtt
hum.addBlood(incBlood); //血量增加incBlood
hum.moveTo(newx,newy); //人移到了(news,newy)處
hum.show();
return 0;
}
Input
第一行:名字 血量 攻擊力 位置,其中位置處是兩個整數,代表平面x、y座標
第二行:增加的攻擊力
第三行:要增加的血量
第四行:新位置,用兩個整數代表
輸入的各部分之間用空格隔開
Output
分別用兩行顯示遊戲角色的初始狀態和調整參數後的狀態
如“Avanda has 500 attack and 1000 blood in (4,3)”表示Avanda有500攻擊力1000血量,在(4,3)位置處
Sample Input
Avanda 500 1000 4 3
-300
200
2 5
Sample Output
Avanda has 500 attack and 1000 blood in (4,3)
Avanda has 200 attack and 1200 blood in (2,5)
HINT
class Human: public Role
{
public:
Human(string str,int a,int b,int xx,int yy):Role(b,xx,yy),name(str),attack(a){}
void show(){
cout<<name<<" has "<<attack<<" attack and "<<blood;
cout<<" blood in ("<<loc.getX()<<",";
cout<<loc.getY()<<")"<<endl;
}
void addAttack( int incAtt){attack+=incAtt;}
private:
string name; // 姓名
int attack; // 攻擊力
};