7-28 格斗吧(20) (10分)

7-28 格斗吧(20) (10分)
小李闲着无聊设计了一个平面打斗游戏中,在这个游戏里面任何的角色(Role)都有血量(blood)和位置loc(此处loc是Location类的实例)属性。 有了Role类,可以派生出不同的角色,如人、神仙、怪兽等。首先,定义了Location类和Role类,人类(Human)中新增了姓名和攻击力数据成员,请为Human类设计成员函数,并实现Role类中的moveTo和addBlood两个成员函数。

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);
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;
}
输入格式:
第一行:名字 血量 攻击力 位置,其中位置处是两个整数,代表平面x、y坐标 第二行:增加的攻击力 第三行:要增加的血量 第四行:新位置,用两个整数代表 输入的各部分之间用空格隔开分别用两行显示游戏角色的初始状态和调整参数后的状态

输出格式:
分别用两行显示游戏角色的初始状态和调整参数后的状态。

输入样例:
在这里给出一组输入。例如:

Avanda 500 1000 4 3
-300
200
2 5
输出样例:
在这里给出相应的输出。例如:

Avanda has 500 attack and 1000 blood in (4,3)
Avanda has 200 attack and 1200 blood in (2,5)

#include<iostream>
#include<string>
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;}; 
	void disp()
	{
		cout<<"("<<x<<","<<y<<")";
	} 
};
class Role
{
public:
    Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {}
    void moveTo(int rx, int ry);  
    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;
}
class Human:public Role
{
	string name;int attack;
	public:
		Human(string name,int attack,int blood,int x,int y)
		:Role(blood,x,y),name(name),attack(attack)
		{
		}
		void display()
		{
			cout<<"Avanda has ";
			cout<<attack;
			cout<<" attack and ";
			cout<<blood;
			cout<<" blood in ";
			loc.disp();
			cout<<endl;
		}
		void addattack(int x)
    {
    	attack+=x;
	}
};
int main()
{
	string name;int blood;int attack;int x;int y;
	int add1,add2,nx,ny;
	cin>>name>>attack>>blood>>x>>y;
	cin>>add1;cin>>add2;
	cin>>nx>>ny;
	Human hu(name,attack,blood,x,y);
	hu.display();
	hu.addattack(add1);
	hu.addBlood(add2);
	hu.moveTo(nx,ny);
	hu.display();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值