您的位置:首页 > 编程语言 > C语言/C++

游戏角色中的类

2016-03-29 20:07 531 查看
/*  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.csdn.net/20160329203134816?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ class 初学