首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】某发票(Invoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。 【C+
阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】某发票(Invoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。 【C+
admin
2017-11-28
9
问题
阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】某发票(Invoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。
【C++代码】
#include
using namespace std;
class Invoice{
public:
(1) {
cout<<“This is the content of the invoice!”<
}
};
class Decorator:public Invoice{
Invoice*ticket;
public:
Decorator(Invoice*t) {ticket=t;}
void printInVoice(){
if(ticket!=NULL)
(2);
}
};
class HeadDecorator:public Decorator{
public:
HeadDecorator(Invoice*t):Decorator(t){ }
void printInvoice(){
cout<<“This iS the header of the invoice!”<
(3) ;
}
};
class FootDecorator:public Decorator{
public:
FootDecorator(Invoice*t):Decorator(t){)
void printInvoice(){
(4) ;
cout<<“This is the footnote of the invoice!”<
}
};
int main(void) {
Invoice t;
FootDecorator f(&t);
HeadDecorator h(&f);
h.print工nVoice();
cout<<“一一一一一一一一一一一一一一一一一一一”<
FootDecorator a(NULL);
HeadDecorator b((5));
b.printInVoice();
return 0;
}
程序的输出结果为:
This is the header of the invoice!
This is the content of the invoice!
This is the footnote of the invoice!
……………………
This is the header of the invoice!
This is the footnote of the invoice!
选项
答案
(1)virtual void printlnvoice() (2)ticket一>printlnvoice0 (3)Decorator::printlnvoice() (4)Decorator::printlnvoice() (5)&a
解析
本题考查装饰(Decorator)模式的基本概念和应用。
装饰模式属于结构型设计模式,其设计意图是动态地给一个对象添加一些额外的职责。就增加功能而言,装饰模式比生成子类更加灵活。装饰模式的结构如图5-2所示。
其中:
Component定义一个对象接口,可以给这些对象动态地添加职责。
ConcreteComponent定义一个对象,可以给这个对象添加一些职责。
Decorator维持一个指向Component对象的指针,并定义一个与Component接口一致的接口。
ConcreteDecorator向组件添加职责。
装饰模式适用于:
在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
处理那些可以撤销的职责。
当不能采用生成子类的方式进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是,由于类定义被隐藏,或类定义不能用于生成子类。
本题将装饰模式用于实现打印发票问题。图5.1的类图中,类Invoice对应图5-2中的Component,其功能是打印发票的内容;HeadDecorator和FootDecorator是两个ConcreteDecorator,向组件中添加打印发票抬头和发票脚注的功能。
方法printlnvoice是Invoice中定义的接口,Component类中应定义一个与之一致的接口。在C++中,父类和子类之间共享接口,通常采用虚拟函数。由此可知,空(1)处应填写“virtual void printlnvoice()”。这个接口在类Decorator、HeadDecorator和FootDecorator中分别进行了重置,分别对应代码中的空(2)~(4)。
类Decorator中保持了一个指向Component对象的指针——ticket,用来接收所要装饰的组件Invoice。因此空(2)处应填写“ticket.>printlnvoice0”。类HeadDecorator和FootDecorator是在打印发票内容的基础上,打印发票的抬头和脚注,所以空(3)、(4)处都应填写“Decorator::printlnvoice0”。
最后一空考查的是装饰模式的调用,由main()函数中给出的第一次调用可以获得一些提示,推断出空(5)出应填写“&a”。
转载请注明原文地址:https://kaotiyun.com/show/CKDZ777K
本试题收录于:
软件设计师下午应用技术考试题库软考中级分类
0
软件设计师下午应用技术考试
软考中级
相关试题推荐
阅读以下说明,回答问题1~3。【说明】Windows组网是指把Windows终端和服务器连接起来。如图5-6所示给出了在Windows操作系统中的典型LAN配置。
在Internet上捕获并分析图8-16所示的网络中两个内部网络经由Internet通信的L2TPv2数据帧,请从以下4个选项中选择正确的答案填写到图8-17的(1)~(4)空缺处的相应位置。【供选择的答案】A.L2TPv2头
SSL是一个协议独立的加密方案,在网络信息分组的应用层和传输层之间提供了安全的通道。SSL主要包括SSL修改密文协议、SSL握手协议、SSL告警协议、SSL记录协议等,其协议栈见图7-16。请根据SSL协议栈结构,将(1)~(4)处空缺的协议名称填写完整。
根据该单位防火墙与外部网络相关的网络连接参数,请将以下命令行中(1)~(4)空缺处的内容填写完整,以完成对防火墙相应的网络接口进行地址初始化的配置。FireWall(config)#ipaddressinside(1)(2)
在交换机上可以配置虚拟局域网(VLAN),以下是部分配置清单。回答问题1、问题2、问题3、问题4和问题5,将解答填入对应栏内。>enablegconfigtEnterconfigurationcommands,oneperli
ADSL技术可以充分利用现有铜线网络,只要在用户线路两端加装ADSL设备即可为用户提供服务。请从以下术语选择适当的编号,将图5-9所示的拓扑结构中(1)~(4)空缺处的名称填写完整。【供选择的答案】A.程控交换机B.二层交换机
阅读以下交换机Switch01的部分配置信息,结合图2-8所示的网络拓扑图将(1)~(8)空缺处的内容(命令或解释)填写完整。Switch>enable(进入特权模式)S
在如图1-23所示的网络拓扑结构图中,被路由协议可以使封装后的数据包通过互连网络进行中继传输,它由(1)使用。【供选择的答案】A.PCIB.RouterA和RouterBC.Internet网D.Rcrate
随机试题
下列属于蝶式套利的有()。Ⅰ.在同一交易所,同时买入10手5月份白糖合约、卖出20手7月份白糖合约、买入10手9月份白糖合约Ⅱ.在同一交易所,同时买入40手5月份大豆合约、卖出80手5月份豆油合约、买入40手5月份豆粕合约Ⅲ.在同
A.近曲小管B.髓袢升支细段C.髓袢升支粗段D.集合管尿浓缩的部位是
患者男性,50岁,嗜酒多年,鼻头及双面颊发红数年,毛孔扩大,渐鼻头肥大,呈结节状,表现凹凸不平,舌暗红苔白腻,脉沉缓。为
医疗费用控制针对医疗服务机构监督常用的方法不包括
某市政工程公司中标承建地铁区间隧道工程标段,隧道断面为马蹄形,由拱顶、直边墙和底板仰拱组成,钢筋混凝土结构,采用喷锚暗挖法施工。工程施工项目部进场后,对沿线地上、地下构筑物及施工环境进行调研后,确定了三个一级风险源:①垂直穿越主干道;②斜穿砖混结构办公楼
葵丘之盟(厦门大学2002年专门史中国通史真题;四川大学2013年中国通史真题)
古希腊文明发源于()。
某西方国家高等院校的学费急剧上涨,其增长率几乎达到通货膨胀率的两倍。1980—1995年中等家庭的收入只提高了82%,而公立大学的学费的涨幅比家庭收入的涨幅几乎大了3倍,私立院校的学费在家庭收入中所占的比例几乎是1980年的2倍。高等教育的费用已经令中产阶
Economics,asweknowit,isthesocialscienceconcernedwiththeproduction,distribution,exchange,andconsumptionofgoods
PlagiarismLinesBlurforStudentsinDigitalAgeAtRhodeIslandCollege,afreshmancopiedandpastedfromaWebsite’sfr
最新回复
(
0
)