找考题网-背景图
填空题

阅读下列说明和C++代码。
[说明]
某大型商场内安装了多个简易的纸巾售卖机,自动出售2元钱一包的纸巾,且每次仅售出一包纸巾。纸巾售卖机的状态图如图5-1所示。

采用状态(State)模式来实现该纸巾售卖机,得到如图5-2所示的类图。其中类State为抽象类,定义了投币、退币、出纸巾等方法接口。类SoldState、SoldOutState、NoQuarterState和HasQuarterState分别对应图5-1中纸巾售卖机的4种状态:售出纸巾、纸巾售完、没有投币、有2元钱。

[C++代码]
#include <iostream>
using namespace std;
//以下为类的定义部分
class TissueMachine; //类的提前引用
class State
public:
virtual void insertQuarter()=0; //投币
virtual void ejectQuarter()=0; //退币
virtual void turnCrank()=0; //按下“出纸巾”按钮
virtual void dispense()=0; //出纸巾
;
/*类SoldOutState、NoQuarterState、HasQuarterState、SoldState的定义省略,
每个类中均定义了私有数据成员TissueMachine* tissueMachine;*/
class TissueMachine
private:
(1) *soldOutState, *noQuarterState, *hasQuarterState,*soldState,
*state;
int count; //纸巾数
public:
TissueMachine (int numbers);
void setState (State* state);
State* getHasQuarterState();
State* getNoQuarterState()j
State* getSoldState();
State* getSoldOutState();
int getCount()j
//其余代码省略
;
//以下为类的实现部分
void NoQuarterState ::insertQuarter()
tissueMachine->setState( (2) );

void HasQuarterState ::ejectQuarter()
tissueMachine->setState( (3) );

void SoldState ::dispense()
if (tissueMachine->getCount()>0)
tissueMachine->setState( (4) );

else
tissueMachine->setState( (5) );

//其余代码省略

(2)处应填()。

【参考答案】

tissueMachine->getHasQuarterState()
热门试题