C++走向远洋——26(项目二,2,构造函数与析构函数)

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


int main()
   {
	   Role mary("Mary",4);
	   Role jack("Jack",0);
	   
       mary.show();
	   jack.show();
	   return 0;
   }
 

运行结果:

心得:

        书中自有颜如玉

posted @ 2016-03-31 18:05  壹言  阅读(130)  评论(0编辑  收藏  举报