第五周项目2(1)

来源:互联网 发布:淘宝电脑主机便宜原因 编辑:程序博客网 时间:2024/05/23 01:06
#include<iostream>  #include<cmath>  using namespace std;  class role  {  public:      void setrole(string a,int n);      void show();      void attack();      void eat(int n);      void beattack();      bool alived();  private:      string name;      int blood;      bool life;  };  void role::setrole(string a,int n)  {      if(alived())      {           name=a;           blood=n;      }      else          cout<<a<<"it is dead!!"<<endl;  }  bool role::alived()  {      if(blood>0)          life=true;      else          life=false;      return life;  }  void role::show()  {       if(alived())       {           cout<<name<<" has "<<blood<<" blood , "<<"it is alived!!!"<<endl;       }       else          cout<<name<<" has "<<blood<<" blood , "<<"it is dead!!!"<<endl;  }  void role::attack()  {        if(alived())          blood+=1;  }  void role::eat(int n)  {      if(alived())          blood+=n;  }  void role::beattack()  {       if(alived())          blood-=1;  }  int main()  {      role mary;      mary.setrole("Marry",4);      mary.show();      mary.attack();      mary.eat(2);      mary.beattack();      mary.beattack();      mary.show();      return 0;      return 0;  } 

0 0