- #include <iostream>
- #include <string>
- using namespace std;
- class Role
- {
- public:
- void setRole(string nam,int b);
- void show();
- void attack();
- void eat(int n);
- void beAttack();
- ~Role()
- {
- cout<<name<<"退出江湖。。。"<<endl;
- }
- private:
- string name;
- int blood;
- bool life;
- };
- void Role::setRole(string nam,int b)
- {
- name=nam;
- blood=b;
- if(blood>0)
- life=true;
- else
- life=false;
- }
- void Role::show()
- {
- cout<<name <<" has "<<blood <<" blood, it is ";
- if(blood>0)
- cout<<"alived."<<endl;
- else
- cout<<"dead."<<endl;
- }
- void Role::attack()
- {
- blood++;
- }
- void Role::beAttack()
- {
- blood--;
- if(blood==0)
- life=false;
- }
- void Role::eat(int n)
- {
- blood+=n;
- }
- int main()
- {
- Role mary;
- mary.setRole("Mary",4);
- Role jack;
- jack.setRole("Jack",0);
- mary.show();
- jack.show();
- return 0;
- }
游戏中的角色类2
最新推荐文章于 2022-04-29 15:44:58 发布