.

Templates in C++

A template is simple and important tool for c++ to pass data type as a parameter and so no need to write the same code for different data types. C++ adds two keywords to support the templates. Template and type name are the keywords which can be replaced as className.

Function Template

A function template is similar to normal function with the difference in the key. A single function template can work with different data types but similar to single function with set of data types. The examples for templates are sort ( ), max ( ), min ( ) and printArray ( ).

Example

classNameTemplate

classNametemplate is used when a classNamedefines the independent data type. This can be useful for classes like linked list, binary tree, stack, queue and array.

Example

.