第5周项目2-游戏中的角色类(1)

来源:互联网 发布:乳源县网络问政 编辑:程序博客网 时间:2024/06/07 04:19

Copyright (c) 2016,
All rights reserced.
文件名称:main.cpp
作者:李鑫

完成日期:2016.4.6

问题描述:基于下面设计的游戏中角色类,补充完整需要的成员函数,使角色能一定的规则行动或改变状态。下面代码中包含的是最基本的要求,可以根据你的设计进行扩充。

#include<iostream>using namespace std;class Role{public:    void setRole(string mz,int x);    void show();    void eat(int d);    void attack();    void beAttack();    bool islife(){       return life;    }private:    string name;    int blood;    bool life;};void Role::setRole(string mz,int x){    name = mz;    blood = x;    if(blood>0)         life = true;       else         life = false;}void Role::show(){    cout<<name<<" has "<<blood<<" blood, it is ";    if(islife())        cout<<"alived.";    else        cout<<"dead.";    cout<<endl;}void Role::eat(int d){    if(islife())        blood = blood + d;}void Role::attack(){    if(islife())    blood++;}void Role::beAttack(){    if(islife())        blood--;    if(blood==0)        life=false;}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