希赛网整理了2017上半年软件设计师考试下午真题,以下是第五部分。
试题五(共15分〉
阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
某快餐厅主要制作井出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种类可能不同,但其制作过程相同。前台服务员(Waiter)调度厨师制作套餐。现采用生成器(Builder)模式实现制作过程,得到如图5-1所示的类图。
【C++代码】
#include<iostream>
#include <string>
using namespace std;
class Pizza {
private: string parts;
public:
void setParts(string parts) { this->parts=parts; }
string getParts() { return parts; }
};
class PizzaBuilder {
protected:Pizza* pizza;
public:
Pizza* getPizza() { retum pizza; }
void createNewPizza() {pizza = new Pizza();}
( 1 );
}
class HawaiianPizzaBuilder :public PizzaBuilder {
public:
void buildParts() { pizza->setParts("cross +mild + ham&pineapple"); }
class SpicyPizzaBuider: public PizzaBuilder {
public:
void buildParts() { pizza->setParts("pan baked +hot + ham&pineapple"); }
}
Class Waiter{
Private:
PizzaBuilder* pizzaBuilder;
public:
void setPizzaBuilder(PizzaBuilder* pizzaBuilder) { /*设置构建器*/
( 2 )
}
Pizza* getPizza() { return pizzaBuilder->getPizza(); }
void construct() { /*构建*/
pizzaBuilder->createNewPizza();
( 3 )
}
};
int main(){
Waiter*waiter=new Waiter();
PizzaBuilder*hawaiian pizzabuilder=new HawaiianPizzaBuilder()
( 4 );
( 5 );
cout<< "pizza: "<< waiter->getPizza()->getParts()<< endl;
}
程序的输出结果为:
pizza:cross+mild+ham&pineapple