项目2——游戏中的角色类

来源:互联网 发布:网络视频用户规模 编辑:程序博客网 时间:2024/06/13 21:34

问题及代码:

/*  *copyright (c) 2014,烟台大学计算机学院  *All rights reserved.  *文件名称:test.cpp  *作    者:李一波  *完成日期:2016年3月31号  *版 本 号:v1.0  *  *问题描述:基于下面设计的游戏中的角色类,补充完整需要的成员函数,使角色能一定的规则行动或改变状态  *输入描述:  *程序输出: */#include<iostream>#include<cmath>using namespace std;class Role{public:    void setRole(string na,int n);    void show();    void attack();    void eat(int n);    void beAttack();    int isLived();private:    string name;    int blood;    bool life;};void Role::setRole(string na,int n){    name=na;    blood=n;}void Role::show(){    if(isLived())    {        cout<<name<<" has "<<blood<<",he is lived"<<endl;    }    else        cout<<"he is died"<<endl;}void Role::attack(){    blood++;}void Role::eat(int n){    blood=blood+n;}void Role::beAttack(){    blood--;}int Role::isLived(){    if(blood>0)        return 1;    else        return 0;}int main( ){    Role mary;    mary.setRole("Mary", 4);    mary.show();    mary.attack();    mary.eat(2);    mary.beAttack();    mary.beAttack();    mary.show();    return 0;}

运行结果:


0 0
原创粉丝点击