When is a template a better solution than a base class?



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

Post New Answer

More C++ General Interview Questions

Explain static and dynamic memory allocation with an example each.

0 Answers  


What do you mean by function pointer?

0 Answers  


Which is best c++ or java?

0 Answers  


Describe the main characteristics of static functions?

1 Answers  


If you hear the cpu fan is running and the monitor power is still on, but you did not see anything show up in the monitor screen. What would you do to find out what is going wrong?

0 Answers  


What are the various arithmetic operators in c++?

0 Answers  


What are the different data types present in C++?

4 Answers  


How can you prevent accessing of the private parts of my class by other programmers (violating encapsulation)?

0 Answers  


What is the hardest coding language to learn?

0 Answers  


Is multimap sorted c++?

0 Answers  


Is it possible to have a recursive inline function in c++?

0 Answers  


what is the behaviour of C and C++ compiler for the below statements. int *p; p = malloc(100); Is the behaviour same ? or different ?

2 Answers  


Categories