您的位置:首页 > 其它

第五周项目2 游戏中角色类的设计(1)

2016-03-28 22:43 330 查看
/*
*Copyright(c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:test2.cpp
*作    者:刘金石
*完成日期:2016年3月27日
*版本  号:v1.0
*问题描述:设计游戏中的角色类。
*输入描述:无。
*输出描述:输出角色血量和状态。
*/
<pre name="code" class="html">#include<iostream>
using namespace std;
class Role
{
public:
void setRole(string name_1,int blo);
void show();
void beAttack();
void eat(int bloo);
void attack();
private:
string name;
int blood;
bool life();
};
void Role::setRole(string name_1,int blo)
{
name=name_1;
blood=blo;
}
void Role::attack()
{
cout<<name<<"attack others!"<<endl;
}
bool Role::life()
{
if(blood>0)
return true;
else return false;
}
void Role::show()
{
cout<<name<<"'s"<<" "<<"blood"<<" "<<"is"<<" "<<blood<<" ";
if(life())
cout<<"it"<<" "<<"is"<<" "<<"alived."<<endl;
else cout<<"it"<<" "<<"is"<<" "<<"dead."<<endl;
}
void Role::eat(int blo)
{
blood=blood+blo;
cout<<name<<" "<<"eat"<<" "<<"something,"<<"blood"<<" "<<"is"<<" "<<blood<<endl;
}
void Role::beAttack()
{
blood-=2;
cout<<name<<" "<<"be"<<" "<<"attack,"<<"blood"<<" "<<"is"<<" "<<blood<<endl;
}
int main()
{
Role mary;
mary.setRole("Mary",4);
mary.show();
mary.attack();
mary.eat(2);
mary.beAttack();
mary.beAttack();
mary.beAttack();
mary.show();
return 0;
}



运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: