資源簡介
一、 請把類Tool的定義補充完整,要求:
1. 類Tool有成員變量strength(int)和 type (char)
2. 成員函數void setStrength(int),設置strength
3. 從類Tool 繼承產生3個子類Rock, Paper, Scissors,他們各有一個構造函數,帶有int參數strength, 以及char型參數type 'r' ( Rock), 'p' ( Paper), 's' (Scissors)
4. 3個子類都各自有一個公有函數bool fight(Tool),比較各自的strength,要求成員變量strength在戰斗過程中不會發生變化,比較的規則如下:
1) Rock在和 scissors戰斗時, strength會臨時加倍,但在和 paper戰斗時, strength會臨時減半;
2) paper在和 rock戰斗時, strength會臨時加倍, 但在和scissors戰斗時, strength會臨時減半;
3) scissors在和 paper 戰斗時, strength會臨時加倍, 但在和Rock戰斗時, strength會臨時減半;
代碼片段和文件信息
#include?
#include?
using?namespace?std;
class?Tool
{
public?:
int?strength;//武力值?
char?type;//對象類型代表?
void?setStrentg(int?strength)
{
this->strength?=?strength;
}
};?
class?Rock?:public?Tool//基于?Tool類的子類Rock?
{
public:
Rock(int?temp)
{
type?=?‘r‘;
strength?=?temp;
}
bool?fight(Tool?a)
{
int?temp?=?this->strength;
if?(type?==?‘s‘)
{
temp?*=?2;
}//遇到剪子,武力值翻倍?
if?(type?==?‘p‘)
{
temp?/=?2;
}//遇到布,武力值減半?
if?(temp?>a.strength)
return?true;
return?false;
}
};
//布和剪子類同理可得?
class?Paper?:public?Tool
{
public?:
Paper(int?temp)
{
type?=?‘p‘;
strength?=?temp;
}
bool?fight(Tool?a)
{
int?temp?=?this->strength;
- 上一篇:運輸問題c語言代碼
- 下一篇:opencv簡單的暗通道去霧算法
評論
共有 條評論