题目描述

在一个平面打斗游戏中,任何的角色(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;
}

输入

第一行:名字 血量 攻击力 位置,其中位置处是两个整数,代表平面x、y坐标
第二行:增加的攻击力
第三行:要增加的血量
第四行:新位置,用两个整数代表
输入的各部分之间用空格隔开

输出

分别用两行显示游戏角色的初始状态和调整参数后的状态
如“Avanda has 500 attack and 1000 blood in (4,3)”表示Avanda有500攻击力1000血量,在(4,3)位置处

样例输入

copy

Avanda 500 1000 4 3
-300
200
2 5

Made by hxl.

样例输出

Avanda has 500 attack and 1000 blood in (4,3)
Avanda has 200 attack and 1200 blood in (2,5)
#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:
Human(string name1, int attack1, int rblood, int rx, int ry):Role(rblood, rx, ry), name(name1), attack(attack1) {}
void moveTo(int rx, int ry)
{
Role::moveTo(rx, ry);
}
void addAttack(int incAtt)
{
attack += incAtt;
}
void addBlood(int b)
{
Role::addBlood(b);
}
void show()
{
cout << name << " has " << attack << " attack and " << blood << " blood in (" << loc.getX() << "," << loc.getY() << ")" << endl;
}
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; }

  

最新文章

  1. WCF基础教程之异常处理:你的Try..Catch语句真的能捕获到异常吗?
  2. android 点亮屏幕与锁定屏幕
  3. Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
  4. 【HDU 3483】 A Very Simple Problem (二项式展开+矩阵加速)
  5. 用Autohotkey让powerpoint幻灯片一直播放
  6. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\921bbfc4\ca7cf42\App_Code.fu98jwep.dll”--“拒绝访问。 ”
  7. Linux怎样访问Windows共享文件和文件夹
  8. input中range相关操作
  9. SQL server 2008 安装提示:属性不匹配
  10. COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问。最新解决方案
  11. UIElementImageShot
  12. Oracle 19c使用dbca来搭建物理DG
  13. hello1以及hello2的部分代码分析
  14. 转载:编译安装Nginx(1.5.1)《深入理解Nginx》(陶辉)
  15. expect 自动完成交互式程序神器
  16. Python爬虫项目--爬取猫眼电影Top100榜
  17. 使用httpclient下载 页面、图片
  18. EditPlus 4.3.2560 中文版已经发布
  19. hydra nodejs 微服务框架简单试用
  20. Windows路由表配置:双网卡同时上内外网

热门文章

  1. C++容器嵌套实现动态二维数组(Vector2D)
  2. Vue.js-----轻量高效的MVVM框架(四、指令)
  3. JavaSE---关键字---return,break,continue
  4. 查看pip已经安装过的包
  5. Dotfuscator 的使用方法
  6. python_元组 学习
  7. [OpenStack] [Liberty] Neutron单网卡桥接模式访问外网
  8. If you are tired...
  9. redis要注意的一些知识
  10. JavaScript操作符(=?,)优先级