CodeClerks

TEMPLATES IN C++ PROGRAMMING



Enter a reason for deleting this comment

TEMPLATES IN C++ PROGRAMMING

Templates is one of thefeatures added to C++ recently. It is a new concept which enable us to define
genetic classes and functions and thus provides support for generic programming. Generic programmingis an approach where generic types are used as parameters in algorithms so that
they work for a variety of suitable data types and data structures.



Atemplate can be used to create family of classes pr functions. For example, a class template for an array classwould enable us to create arrays of various data types such as int array and float array. Similarly, we can define a template for a function,say mul(), that would help us create various versions of mul() for multiplying int,float and double type values.



Atemplate can be considered as a kind of macro. When an object of a specific type is defined for actual use, the template definition for that class in substituted with the required data type. Since a template is defined with a parameter that would be replaced by aspecified data type at the time of actual use of the class of function, the templates are sometimes called parameterized classes or functions.

Comments

Please login or sign up to leave a comment

Join