阅读下列说明和C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。
【说明】
以下C++代码实现一个超市简单销售系统中的部分功能,顾客选择图书等物品(Item)加入购物车(ShoppingCart),到收银台(Cashier)对每个购物车中的物品统计其价格进行结账,设计如图6-1所示类图。

【C++代码】
using namespace std;class Book;class Visitor {public: virtual void visit(Book* book)=0; //其它物品visit方法}; class Item {public:virtual void accept(Visitor* visitor)=0; virtual doublegetPrice()=0;};class Book (1){private: double price;public: Book (double price){ //访问本元素 (2); } void accept(Visitor* visitor) { (3); }double getPrice(){ returnprice; }};class Cashier(4){private; double totalForCart;public: //访问Book类型对象的价格并累加 (5){ //假设Book类型的物品价格超过10元打8折 if(book->getPrice()getPrice(); } else totalForCart+=book->getPrice()*0.8; } //其它visit方法和折扣策略类似,此处略 double getTotal() { return totalForCart; }};class ShoppingCart {private: vector<item*>items;public: double calculatePrice() { Cashier* visitor=new Cashier(); for(int i=0;i <items.size();i++) (6); } double total=visitor->getTotal(); return total; } void add(Item*e) { items.push_back(e); }};
正确答案及解析
正确答案
解析
(1):public Item
(2)this->price=price
(3)visitor->visit(this)
(4)public visitor
(5)void visit(Book*book)
(6)item->accept(visitor)
【解析】
这里考察的是访问者模式。其定义如下:封装某些作用于某种数据结构中各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作
包含此试题的试卷
你可能感兴趣的试题
Advancements in ( )have contributed to the growth of the automotive industry through the creation and evolution of self-driving vehicles.
-
- A.Artificial Intelligence
- B.Cloud Computing
- C.Internet of Things
- D.Big Data
- 查看答案
In project human resource management , ( )is not a source of power for the project manager.
-
- A.referent power
- B.expert power
- C.reward power
- D.audit power
- 查看答案
At the project establishment stage , the feasibility study mainly includes techinical feasibility analysis , ( ), operation environment feasibility analysis and other aspects of feasibility analysis.
-
- A.detail feasibility analysis
- B.opportunity analysis
- C.economic feasibility analysis
- D.risk analysis
- 查看答案
( )is a grid that shows the project resources assigned to each work package.
-
- A.Stakeholder engagement assessment matrix
- B.Requirements traceability matrix
- C.Probability and impact matrix
- D.Responsibility assignment matrix
- 查看答案
Xinhua News Agency reported in January 2022,Chian will further promote the developmet of a digital economy during the 14th Five-Year Plan eriod(2021-2025). The plan also emphasized industrial ( )transformation.
-
- A.digital
- B.networking
- C.intelligentize
- D.informatization
- 查看答案