*All rightreserved.
*文件名称:test.cpp
*作 者:张明宇
*完成日期:2016年3月27日
*/
问题及代码:
基于下面设计的游戏中角色类,补充完整需要的成员函数,使角色能一定的规则行动或改变状态。下面代码中包含的是最基本的要求,可以根据你的设计进行补充。
class Role
{
public;
......
private;
string name;
int blood;
bool life;
};
int main()
{
Role mary;
mary.setRole("Mary",4);
mary.show();
mary.attack();
mary.eat(2);
mary.beAttack();
mary.beAttack();
mary.show();
return 0;
}
#include <iostream>
using namespace std;
class role
{
public:
void setrole(string ch,int x);
void show();
int attack();
int eat(int n);
int beattack();
bool islived();
private:
string name;
int blood;
bool life;
};
void role::setrole(string ch,int x)
{
name=ch;
blood=x;
}
bool role::islived()
{
if(blood>0)
return 1;
else
return 0;
}
void role::show()
{
cout<<"your blood is:"<<blood<<endl;
}
int role::attack()
{
if(islived())
blood=blood+1;
return blood;
}
int role::eat(int n)
{
if(islived())
blood=blood+n;
return blood;
}
int role::beattack()
{
if(islived())
blood=blood-1;
return blood;
}
int main()
{
role mary;
mary.setrole("Mary",4);
mary.show();
mary.attack();
mary.eat(2);
mary.beattack();
mary.beattack();
mary.show();
return 0;
}