游戏中的角色类

#include <iostream>
#include"string.h"
using namespace std;

class role
{
public:

    void setrole(string NAME,int BLOOD);
    void show();
    void attack();
    void eat(int n);
    void beattack();

private:
    string name;
    int blood;
    bool life;

};
 void role::setrole(string NAME,int BLOOD)
{
    name=NAME;
    blood=BLOOD;
}

void role::attack()
{
   blood++;
}
void role::eat(int n)
{
    blood=blood+n;
}
void role::beattack()
{
    blood--;
}

void role::show()
{


   cout<<name<<" has "<<blood<<" blood,it is ";
   life=blood;
   if(life)
    cout<<"alive"<<endl;
   else
    cout<<"die"<<endl;
}


int main()
{
    role mary;
    mary.setrole("Mary",4);
    mary.show();
    mary.attack();
    mary.eat(2);
    mary.beattack();
    mary.beattack();
    mary.show();
    return 0;
}
 
 
 
 

运行结果:





你可能感兴趣的:(游戏中的角色类)