What are generic classes? Why are they useful? 

When we declare a generic class we are able to define all algorithms used by that class but the actual type data being manipulated will be specified as a parameter when object of that class is created. Generic classes are useful when a class contains generalized logic. For example the algorithm that is used to maintain a queue of integer can also be used for a queue of character. The compiler will automatically generate the correct type of object based upon the type you specify when the object is created. General form generic class:




template <class type> class class-name
{
//body of the template;
}


Here the type is place holder type-name that will be specified when the object of the class is created. We can define more than one generic data-type by using comma separated list.
To create an object of template class is:

Class-name <type> object;

Member functions of a generic class are themselves automatically generic. They need not be explicitly specified by using template.


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: