您的位置:首页 > 其它

第六周项目2:给游戏角色添加武器

2016-04-06 18:53 141 查看
问题及代码:

/*
*copyright(c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:test.cpp
*作者:崔青青
*完成日期:2016年4月6日
*问题描述:在上一周的基础上,对角色设计武器。
*输入:
*程序输出:
*/
#include <iostream>
using namespace std;
class weapon
{
public:
weapon(string nam,int f);
int showweapon();
void show();
private:
int force;
string wname;
};
weapon::weapon(string nam,int f)
{
wname=nam;
force=f;
}
int weapon::showweapon()
{
return force;
}
void weapon::show()
{
cout<<"使用武器:"<<wname<<"  "<<"威力:"<<force<<endl;
}
class role
{
public:
role(string ch,int x,string nam,int f);
void show();
int attack(role &r);
int eat(int n);
int beattack();
bool islived();
~role()
{
cout<<name<<"退出江湖......"<<endl;
}
private:
string name;
int blood;
bool life;
weapon wea;
};
role::role(string ch,int x,string nam,int f): name(ch), blood(x), wea(nam,f)
{
;
}
bool role::islived()
{
if(blood>0)
return 1;
else
return 0;
}
void role::show()
{
cout<<name<<" "<<"your blood is:"<<blood<<endl;
wea.show();
}
int role::attack(role &r)
{
if(islived())
{
blood=blood+wea.showweapon();
r.blood=r.blood-wea.showweapon();

}
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",40,"顺风耳",10);
role jack("jack",40,"千里眼",20);
mary.show();
jack.show();
cout<<"第一回合:"<<endl;
jack.attack(mary);
mary.show();
jack.show();
cout<<"第二回合:"<<endl;
mary.attack(jack);
mary.show();
jack.show();
cout<<"---end---"<<endl;
return 0;
}


运行结果:



知识点总结:

组合类的程序应注意的问题。

学习心得:

组合类应用广泛,应该多加练习。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: