*Copyright(c) 2016.烟台大学计算机与控制工程学院
*ALL rights reserved.
*文件名称:main.cpp
*作者:孙亚茹
*完成日期:2016年6月7日
*问题描述:补充完整需要的成员函数。
*//
#include <iostream>
using namespace std;
class Role
{
public:
void setRole(string nam,int b);
void show();
void attack();
void eat(int n);
void beAttack();
bool isLife();
private:
string name;
int blood;
bool life;
};
bool Role::isLife()
{
if(blood>0)
return true;
else
return false;
}
void Role::setRole(string nam,int b)
{
name=nam;
blood=b;
}
void Role::attack()
{
if(isLife())
{
blood++;
}
}
void Role::eat(int n)
{
if(isLife())
{
blood+=n;
}
}
void Role::beAttack()
{
if(isLife())
{
blood--;
}
}
void Role::show()
{
if(isLife())
cout<<name<<"还有"<<blood<<"滴血"<<endl;
else
cout<<name<<"已经死了"<<endl;
}
int main()
{
Role mary;
mary.setRole("Mary",4);
mary.show();
int n;
while(1)
{
cout<<" 1 attack 2 beAttack 3 eat "<<endl;
cout<<"请您选择相应的动作:"<<endl;
cin>>n;
if(n==1)
{
mary.attack();
mary.show();
}
else if(n==2)
{
mary.beAttack();
mary.show();
}
else if(n==3)
{
mary.eat(2);
mary.show();
}
else
break;
}
return 0;
}
总结:
学会了bool型函数的用法。
做过后面的比较难的项目,再回过头来做当时不会做的就感觉没那么难了。
高考加油!!!