带武器的角色类

问题及代码:

/*文件名称:带武器的角色类
完成日期:2016年4月17日
作者:马艳艳
问题描述:类的组合创建带武器的角色类
输入描述: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;
}

运行结果:

知识点总结:

类的组合要在一个类先完成并初始化的前提下,作为内嵌成员,调用;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值