您的位置:首页 > 其它

第八周-角色类

2016-04-21 09:23 274 查看
代码:

/*
*Copyright (c) 2016, 烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:角色类;
*作    者:岳成艳;
*完成日期:2016年4月21号;
*版 本 号:vc++6.0;
*问题描述:类的组合
*输入描述:switch语句;
*程序输出:所带血量;
*/
#include <iostream>

using namespace std;
class Weapon
{
public:
Weapon (string nam,int f=100,int wkeyong=200,int wdamage=500):wname(nam),force(f),keyongdu( wkeyong), damage(wdamage)
{

}
int getforce();
void wreduceN();
int wdamage();
int Rekeyong();
void   WeaponChange (string ,int ,int ,int );
private:

string wname;
int force;
int damage;
int  keyongdu;

};
void  Weapon:: WeaponChange (string wnam="jvlong",int fi=1000,int wkeyongi=20000,int wdamagei=3000)

{

wname=wnam;
force=fi;
keyongdu=wkeyongi;
damage=wdamagei;

cout<<"武器名字为:"<<wname<<endl;
cout<<"它的攻击力为:"
<<force<<endl;
cout<<"它的可用度为:"
<<keyongdu<<endl;
cout<<"破坏力为"
<<damage<<endl;

}

int Weapon::getforce()//攻击他人;
{
return force;
}
void Weapon::wreduceN()//修复武器的可用度;
{
keyongdu+=500;
damage+=150;
cout<<"可用度为:"<<keyongdu<<endl;
cout<<"破坏力为:"<<damage<<endl;
}
int Weapon:: wdamage()
{
return damage;
}
int Weapon::Rekeyong()
{
return keyongdu;
}
class Role
{
public:
Role(string nam,int b,string wnam,int f,int wkeyong,int wdamage):name(nam),blood(b),weapon(wnam,f,wkeyong,wdamage){};
~Role();//析构函数
void eat(int b);
void attack(Role &r);
void beattack(Role &r);
bool is_Alived();
void show();
void    Redamage();
void change();

private:
string name;
int blood;
Weapon weapon;
bool life;

};

void  Role:: change()
{
weapon.WeaponChange();
}
void Role:: show()
{
life=is_Alived();
if(life)
{
cout<<name<<"有 " <<blood<<"血 "<<"  攻击力为:"<<weapon. wdamage()<<"可用度为: "<<weapon.Rekeyong()<<endl;

}
else
cout<<"该角色"<<name<<"现在有"<<blood<<"血"<<"it has been dead;"<<endl;

}
void Role::    Redamage()
{
weapon.wreduceN();
}
bool Role:: is_Alived()
{
if(blood>0)
return true;
else
return false;
}
void Role:: attack(Role &r)
{
if(is_Alived())
blood+=weapon.getforce();
blood+=weapon.Rekeyong();
r.blood-=weapon.wdamage();
if(r.blood<0)
r.life=false;
cout<<name<<"有 "<<blood<<"血"<<endl;
cout<<r.name<<"有 "<<r.blood<<"血"<<endl;
}
void Role:: beattack(Role &r)
{
if(is_Alived())
blood-=weapon.wdamage();
blood-=10*(100-2*weapon.Rekeyong());
r.blood+=weapon.getforce();
cout<<name<<"有 "<<blood<<"血"<<endl;
cout<<r.name<<"有 "<<r.blood<<"血"<<endl;

}
Role::~Role()
{
cout<<name<<"退出江湖。。"<<endl;

}
void Role:: eat(int b)

{
blood+=b;
cout<<name<<"有 "<<blood<<"血"<<endl;

}
int main()
{
int t;
Role mary("mary",500,"tushu",200,100,800);
Role seli("seli",450,"huer",600,700,800);
cout<<"您可以选择以下选项进行您的操作:";
cout   <<"一:攻击敌人"<<endl;
cout  <<"二:加血"<<endl;
cout<<"三:更换武器"<<endl;
cout<<"四:被攻击"<<endl;
cout<<"五:修理武器"<<endl;
cout<<"六:展示角色信息"<<endl;
cout<<"七:退出"<<endl;
while(1)
{
cin>>t;
switch(t)
{
case 1:mary.attack( seli);
break;
case 2:seli.eat(500);
break;
case 3:seli.change();
break;
case 4:mary.beattack(   seli);
break;
case 5:mary.Redamage();
break;
case 6:mary.show();seli.show();
break;
case 7:seli.~Role();
mary.~Role();break;

}

}
return 0;
}


运行测试:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: