Question: 1. Objective: Class Template and STL A) The code sample provided will feature two classes E1 and E2 , and their base class EBase .

1. Objective: Class Template and STL

A) The code sample provided will feature two classes E1 and E2, and their base class EBase. Two additional classes provided, C1 and C2, will wrap a STL container (already a template) and also have additional methods, which capture some workflow as a template. C1 and C2 will store E1 and E2 types of objects in their respective STL stores. We observe that the code in C1 and C2 is identical, and only the type on which they operate is different either E1 or E2. It is therefore a good candidate for a class template. Your assignment will be to write a class CT as a template that replaces C1 and C2. [ same number of lines of code as in C1 or C2]

Summary and take away: The purpose is to arrive at a template class that captures a sample workflow in a given problem domain and can operate on multiple data types. It will be designed as a wrapper around a STL container, which already is a template, where multiple data types can be plugged in, such as E1 or E2. A general constraint for a template, however, is that the types are indeed pluggable, that is: they will have all the features (methods and operators, natural ones or overloaded), needed for the template logic to run properly. We can have those checks enforced by having an EBase class capturing all those common methods and operators. In addition, the class hierarchy will enable polymorphic overridden methods, so our high level workflow can be customized, depending on which type, E1 or E2, is plugged in.

Notice, that at the end of day, only a templated CT will be needed. It will capture the high level process flow as its algorithm, but also offer specialized logic through polymorphism. This sophisticated set up becomes a design pattern.

B) Then, in main, stand up the instances CT1 and CT2 of type CT, holding E1 and E2 types respectively. [ 2 lines of code]

2.

Objective: Generic Algorithms

A) This part will add an additional method to both C1 and C2, and also to CT. The method will wrap one of the Generic Algorithms, find or search - the signature will be provided, or can be looked up at cplusplus.com. As you recall, Generic Algorithms rely on the Iterator concept, but often only implicitly by making use of being() and end() methods attached to the container they operate on. [3-5 lines of code to write in C1 and C2]

B) Add a matching method to CT class template [3-5 lines of code in CT]

C) The success of the Generic Algorithm find depends on what operation being present in the objects held in the container? What about search?

D) Extra credit: Write code for this operation. (Hint: this code can live in the base class EBase)

Please put the code with the respective question. Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!