Copyright (c) 2016,
All rights reserced.
文件名称: main.cpp
作者 :李鑫
All rights reserced.
文件名称: main.cpp
作者 :李鑫
完成日期:2016.4.6
问题描述:设计构造函数,使角色对象在创建时被初始化;设计析构函数,当角色对象被撤消时,提示一句“XXX(名称)退出江湖…”。
#include<iostream>
using namespace std;
class Role
{
public:
Role (string mz,int x);
~Role();
void show();
void eat(int d);
void attack();
void beAttack();
bool islife(){
return life;
}
private:
string name;
int blood;
bool life;
};
Role::Role(string mz,int x)
{
name = mz;
blood = x;
if(blood>0)
life = true;
else
life = false;
}
Role::~Role()
{
cout<<name<<"退出江湖..."<<endl;
}
void Role::show()
{
cout<<name<<" has "<<blood<<" blood, it is ";
if(islife())
cout<<"alived.";
else
cout<<"dead.";
cout<<endl;
}
void Role::eat(int d)
{
if(islife())
blood = blood + d;
}
void Role::attack()
{
if(islife())
blood++;
}
void Role::beAttack()
{
if(islife())
blood--;
if(blood==0)
life=false;
}
int main( )
{
Role mary("Mary", 4);
Role jack("Jack", 0);
mary.show();
jack.show();
return 0;
}