游戏中的角色类1

来源:互联网 发布:linux 启动ntp客户端 编辑:程序博客网 时间:2024/04/28 23:38

问题及代码:

/*

*文件名称:游戏中的角色类1

*作者:隋文韬

*完成日期:2016.4.5

*问题描述:设计游戏中的角色类,补充完整需要的成员函数,使角色能一定的规则行动或改变状态

*输入描述:无

*程序输出:角色名称 初始血量 当前血量

*/

#include<iostream>
#include<string>
using namespace std;
class Rode
{
public:
    void setRode(string nam,int blo);
    void show();
    void attack();
    void eat(int medicine);
    void beAttack();
private:
    string name;
    int blood;
    bool life;
};
void Rode::setRode(string nam,int blo)
    {
        name=nam;
        blood=blo;
        if(blood>0)
        {
            life=true;
        }
        else life=false;
    }
    void Rode:: show()
    {
        cout<<name<<" has "<<blood<<" blood"<<endl;
    }
    void Rode::attack()
    {
        blood++;
    }
    void Rode::eat(int medicine)
    {
        blood=blood+medicine;
    }
    void Rode::beAttack()
    {
        blood--;
        if(blood>0)
            life=true;
        else
            life=false;
    }

int main()
{
    Rode mary;
    mary.setRode("Mary",4);
    mary.show();
    mary.attack();
    mary.eat(2);
    mary.beAttack();
    mary.beAttack();
    mary.show();
    return 0;
}

0 0
原创粉丝点击