When is a template a better solution than a base class?
Answer / 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 |
Given the following seqment of code containing a group of nested if instructions: y = 9; if ((x==3) || (x == 5)) y++; else if (x == 2) y *= 2; else if (x == 4 ) y-= 7; else y = 8; Enter a segment of code (without any IF statements) that does exectly the same thing using the switch structure.
What is double in c++?
Difference between inline functions and macros?
How do you define/declare constants in c++?
What is the importance of mutable keyword?
What is the best c++ ide?
What is the Difference between "vector" and "array"?
15 Answers Covansys, Gambit, TCS, Wipro,
Can malloc be used in c++?
What is the most common mistake on c++ and oo projects?
Out of fgets() and gets() which function is safe to use?
How is static data member similar to a global variable?
Where can I run c++ program?