When is a template a better solution than a base class?
Answer Posted / neelkamal yadav
When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus, the genericity) to the designer of the container or manager class.
Prior to templates, you had to use inheritance; your design might include a generic List container class and an application-specific Employee class. To put employees in a list, a ListedEmployee class is multiply derived (contrived) from the Employee and List classes. These solutions were unwieldy and error-prone. Templates solved that problem.
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
How do you write a function that can reverse a linked-list?
Write a program which is required to process the time of a clock in hours and minutes, entered from the keyboard. With this program, there are two requirements for any data entered by a user: 1. The data must be of the correct type (in this case, two ints). 2. The data must be in the correct range: this means that, for the minutes, negative numbers and any number above 59 must be rejected; for the hours, negative numbers and any number above 23 must be rejected. Output error message for invalid data input. Output the time one and a half hour after the time input. i.e. Hour: 22 Min: 32 One and a half hour after 22:32 is 00:02
Which recursive sorting technique always makes recursive calls to sort subarrays that are about half size of the original array?
Is there a sort function in c++?
Can we use struct in c++?
What is the difference between an external iterator and an internal iterator? Describe an advantage of the external iterator.
If a round rectangle has straight edges and rounded corners, your roundrect class inherits both from rectangle and from circle, and they in turn both inherit from shape, how many shapes are created when you create a roundrect?
What do you mean by “this” pointer?
Explain the different access specifiers for the class member in c++.
Can we use this pointer inside static member function?
Mention the ways in which parameterized can be invoked. Give an example of each.
Define a nested class.
What is #include iostream?
What is name hiding in c++?
Explain deep copy and a shallow copy?