/* Copyright (c) 2016
* All rights reserved
* 文件名称:3.cpp
* 作者:刘丽
* 完成日期:2016年 3 月 17日
* 版本号: v1.0
*
* 问题描述:编写一个游戏角色的类
*/
#include<iostream>
using namespace std;
class Role
{
public:
void setRole( string a,int n);
void show();
void beattack();
void eat(int n);
private:
int blood;
string name;
bool life;
};
void Role::setRole(string a,int n)
{
name=a;
blood=n;
}
void Role::show()
{
if(life)
cout<<name<<" has "<<blood<<",he is alive."<<endl;
else
cout<<"he is died."<<endl;
}
void Role::beattack()
{
blood--;
if(blood==0)
life=false;
}
void Role::eat(int n)
{
blood++;
}
int main()
{
Role mary;
mary.setRole("mary",4);
mary.show();
mary.eat(2);
mary.beattack();
mary.beattack();
mary.show();
return 0;
}<img src="https://img-blog.youkuaiyun.com/20160329203134816?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />