OJ-多态性
2015-06-24 22:39
344 查看
在一个平面打斗游戏中,任何的角色(Role)都有血量(blood)和位置loc(此处loc是Location类的实例)属性。有了Role类,可以派生出不同的角色,如人、神仙、怪兽等。如下程序中,定义了Location类和Role类,人类(Human)中新增了姓名和攻击力数据成员,请为Human类设计成员函数,并实现Role类中的moveTo和addBlood两个成员函数。
请在begin和end中间写下需要的代码。你只能编辑并提交begin和end之间的代码。
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)
!!两点:
@ Mayuko
请在begin和end中间写下需要的代码。你只能编辑并提交begin和end之间的代码。
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)
#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 na,int att,int bl,int x,int y); void addAttack(int a); void addBlood(int b); void moveTo(int mx,int my); void show(); private: string name; // 姓名 int attack; // 攻击力 }; Human::Human(string na,int att,int bl,int x,int y):name(na),attack(att),Role(bl,x,y){} void Human::addAttack(int a) { attack+=a; } void Human::addBlood(int b) { blood+=b; } void Human::moveTo(int mx,int my) { Role::moveTo(mx,my); } void Human::show() { cout<<name<<" has "<<attack<<" attack and "<<blood<<" blood in ("<<loc.getX()<<","<<loc.getY()<<")"<<endl; } //************* 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; }
!!两点:
cout<<name<<" has "<<attack<<" attack and "<<blood<<" blood in ("<<loc.getX()<<","<<loc.getY()<<")"<<endl;//Human没有继承自Location
void Human::moveTo(int mx,int my) { Role::moveTo(mx,my);//Human继承自Role }
@ Mayuko
相关文章推荐
- Labview与其他应用程序的接口设计
- poj 1163 The Triangle
- [设计模式学习笔记]FACADE外观模式
- 设计模式之十八:桥接模式(Bridge)
- PHP自动登录的实现和Cookie的安全性(UCHome的实现方法)
- gdb和gcc升级:解决编译和调试时出现gdb no symbol in current context
- 同一端口如何区分不同的Socket
- 设计模式之策略模式——极客学院学习笔记
- WebBrowser与IE的关系,如何设置WebBrowser工作在IE9模式下?
- Sqlite 帮助类 SQLiteHelper
- Happy Number
- WPF中的快捷键(累积中)
- 信息论的知识
- jsp自定义标签
- C/C++——程序的内存分配
- OJ-虚函数
- comparetor和comparable的区别
- java学习之旅13--运算符_01_算术_逻辑_位运算符_扩展运算符
- rabiitmq
- HDU-2871 Memory Control(线段树)