天天看點

帶武器的角色類

問題及代碼:

/*檔案名稱:帶武器的角色類
完成日期: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;
}

           

運作結果:

帶武器的角色類

知識點總結:

類的組合要在一個類先完成并初始化的前提下,作為内嵌成員,調用;

繼續閱讀