问题及代码:
/*
*copyright(c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:test.cpp
*作者:崔青青
*完成日期:2016年3月26日
*问题描述:基于下面设计的游戏中角色类,补充完整需要的成员函数,使角色能有一定的规则行动或改变状态。
*输入:
*程序输出:
*/
#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;
}
运行结果:
知识点总结:
对类的建造和掌握。
学习心得:
对函数的定义是很重要的