问题及代码:
/*
*文件名称:游戏中的角色类
*作者:刘涛
完成日期:2016.3.27
*
*问题描述:设计游戏中的角色类,补充完整需要的成员函数,使角色能一定的规则行动或改变状态
*输入描述:无
程序输出:姓名 血量
*/
#include<iostream>
#include<string>
using namespace std;
class Rode
{
public:
void setRode(string nam,int blo);
void show();
void attack();
void eat(int medicine);
void beAttack();
private:
string name;
int blood;
bool life;
};
void Rode::setRode(string nam,int blo)
{
name=nam;
blood=blo;
if(blood>0)
{
life=true;
}
else life=false;
}
void Rode:: show()
{
cout<<name<<" has "<<blood<<" blood"<<endl;
}
void Rode::attack()
{
blood++;
}
void Rode::eat(int medicine)
{
blood=blood+medicine;
}
void Rode::beAttack()
{
blood--;
if(blood>0)
life=true;
else life=false;
}
int main()
{
Rode mary;
mary.setRode("Mary",4);
mary.show();
mary.attack();
mary.eat(2);
mary.beAttack();
mary.beAttack();
mary.show();
return 0;
}
运行及结果: