您的位置:首页 > 其它

第5周项目2 游戏中角色类(扩充函数)

2016-04-02 13:15 393 查看
/*
*copyright(c) 2016,烟台大学计算机学院
*All rights reserved
*文件名称:test.cpp
*作者:李硕
*版本:v6.0
*
*问题描述:宣告主权
*输入描述:
*程序输出:
*/

#include <iostream>
using namespace std;
class Role
{
public:
void eat(int x);
void attack();
void beAttack();
bool isAlived();
void setRole(string n, int b);
void show(); //显示
private:
string name;
int blood;
bool life;
};
void Role::eat(int x)
{
if(isAlived())
blood=x+1;
}
void Role::attack()
{
if(isAlived())
blood++;
}
void Role::beAttack()
{
if(isAlived())
blood--;
if(blood==0)
life=false;
}
bool Role::isAlived()
{
return life;
}
void Role::setRole(string n, int b)
{
name=n;
blood=b;
if(blood>0)
life=true;
else
life=false;
cout<<"初始值是:"<<endl;
}
void Role::show()
{

cout<<name<<" 's "<<blood<<" blood is ";
if(isAlived())
cout<<"islive.";
else
{
cout<<"dead."<<endl;
cout<<"GG"<<endl;
}

cout<<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;
}
<img src="http://img.blog.csdn.net/20160402131634753?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: