C++走向远洋——25(项目二,游戏类)

*/
 * Copyright (c) 2016,烟台大学计算机与控制工程学院
 * All rights reserved.
 * 文件名:game.cpp
 * 作者:常轩
 * 微信公众号:Worldhello
 * 完成日期:2016年3月29日
 * 版本号:V1.0
 * 问题描述:游戏类的简单实现
 * 程序输入:无
 * 程序输出:见运行结果
 */
#include<iostream>
#include<string>
using namespace std;
class Role
   {
   public:
	   void setRole(string M,int B);
	   void show();
       void attack();
       void eat(int n);
	   void beAttack();
   private:
	   string name;
	   int blood;
	   bool life;
   };
void Role::setRole(string M,int B)
{
       name=M;
	   blood=B;
}

void Role::show()
{
    if(life)
		cout<<name<<" has "<<blood<<" , it is alived"<<endl;
    else
        cout<<name<<" has "<<blood<<" , it is dead"<<endl;
}

void Role::attack()
{
	blood++;
}
 
void Role::eat(int n)
{
	blood=blood+n;
}

void Role::beAttack()
{
	blood--;
}
int main()
   {
	   Role mary;
	   mary.setRole("Mary",4);
	   mary.show();
	   mary.attack();
	   mary.eat(2);
	   mary.beAttack();
      mary.beAttack();
      mary.show();
	   
	   return 0;
   }
 

运行结果:

心得:

        书中自有黄金屋

原文地址:https://www.cnblogs.com/chxuan/p/8232256.html