第五周项目2-游戏中的角色类(1)

*All rightreserved. 

 *文件名称:test.cpp 

 *作   者:张明宇

 *完成日期:2016年3月27日 

 */

问题及代码:

基于下面设计的游戏中角色类,补充完整需要的成员函数,使角色能一定的规则行动或改变状态。下面代码中包含的是最基本的要求,可以根据你的设计进行补充。

class Role
{
	public;
	   ......
	private;
	   string name;
	   int blood;
	   bool life;
};
int main()
{
	Role mary;
	mary.setRole("Mary",4);
	mary.show();
	mary.attack();
	mary.eat(2);
    mary.beAttack();
	mary.beAttack();
	mary.show();
	return 0;
}
#include <iostream>   
using namespace std;  
class role  
{  
public:  
    void setrole(string ch,int x);  
    void show();  
    int attack();  
    int eat(int n);  
    int beattack();  
    bool islived();  
private:  
    string name;  
    int blood;  
    bool life;  
};  
void role::setrole(string ch,int x)  
{  
    name=ch;  
    blood=x;  
}  
bool role::islived()  
{  
    if(blood>0)  
        return 1;  
    else  
        return 0;  
}  
void role::show()  
{  
    cout<<"your blood is:"<<blood<<endl;  
}  
int role::attack()  
{  
    if(islived())  
    blood=blood+1;  
    return blood;  
}  
int role::eat(int n)  
{  
     if(islived())  
    blood=blood+n;  
    return blood;  
}  
int role::beattack()  
{  
     if(islived())  
    blood=blood-1;  
    return blood;  
}  
int main()  
{  
    role mary;  
    mary.setrole("Mary",4);  
    mary.show();  
    mary.attack();  
    mary.eat(2);  
    mary.beattack();  
    mary.beattack();  
    mary.show();  
    return 0;  
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值