GENERIC CLASS AND GENERIC FUNCTIONS (TEMPLATE_CLASSES)
Generic class and Generic Function(template classes):
Generic function:
a) If the same functionality has to be performed for different data types - a template function can be created.
b) The compiler creates different versions for different data types.
e.g.
template<class T>
T Function_Template(T x, T y)
{
T z;
z = x + y;
return T;
}
The above function takes parameters of data type T and returns T.
Generic class:
a) Container classes[vector, list, etc] are examples of template classes.
b) The compiler creates different versions for different data types.
e.g.
template<class T>
class Template_Class
{
T x;
T y;
public:
T Temp_Function(T x, T y);
};
template<class T>
T Template_Class::Temp_Function(T x, T y)
{
T z;
z = x + y;
return z;
}
Template_Class<int> T_int;
Template_Class<float> T_float;
Template_Class<char> T_char;
Refer : Standard Template Library (STL)