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


Please Help Members By Posting Answers For Below Questions

What is the use of register keyword with the variables?

551


What is an iterator?

690


Explain container class.

689


Which bit wise operator is suitable for turning off a particular bit in a number?

645


What is a class template in c++?

527






Is c better than c++?

621


How to declare an array of pointers to integer?

586


What is a driver program?

633


Where can I run c++ program?

605


How do you clear a buffer in c++?

541


What is a manipulator in c++?

717


What is while loops?

619


What does flush do?

563


Define 'std'.

610


What is atoi?

546