- In C++ a template is a simple and yet very powerful tool.
- We don’t need to write the same code for different data types because it is simple idea to pass data type as a parameter.
- Rather than maintaining and writing the multiple codes, we can write one sort () and pass data type as a parameter.
- At compiler time templates are expanded, hence this is like macros.
- At C++ there are two types of support templates, they are ‘template’ and ‘typename’.
- The difference is, the compiler does type checking before template expansion and the idea is source code contains only function/class, but compiled code may contain multiple copies of same function/class.
- Templates can be represented in two ways they are Function templates and Class templates.

Function Template
- Generic functions use the concept of function template and it defines a set of operations that can be applied to the various types of data.
- The function will operate on depends on the type of the data passed as a parameter.
- It can be implemented to an array of floats or array of integers, quick sorting algorithm is implemented using a generic function.
- Using keyword template generic function is created and template defines what function will do.
Sample Code
[pastacode lang=”cpp” manual=”%23include%20%3Ciostream%3E%20%20%0Ausing%20namespace%20std%3B%20%20%0Atemplate%3Cclass%20T%3E%20T%20add(T%20%26a%2CT%20%26b)%20%20%0A%7B%20%20%0A%20%20%20%20T%20result%20%3D%20a%2Bb%3B%20%20%0A%20%20%20%20return%20result%3B%20%20%0A%20%20%20%20%20%20%0A%7D%20%20%0Aint%20main()%20%20%0A%7B%20%20%0A%20%20int%20i%20%3D2%3B%20%20%0A%20%20int%20j%20%3D3%3B%20%20%0A%20%20float%20m%20%3D%202.3%3B%20%20%0A%20%20float%20n%20%3D%201.2%3B%20%20%0A%20%20cout%3C%3C%22Addition%20of%20i%20and%20j%20is%20%3A%22%3C%3Cadd(i%2Cj)%3B%20%20%0A%20%20cout%3C%3C’%5Cn’%3B%20%20%0A%20%20cout%3C%3C%22Addition%20of%20m%20and%20n%20is%20%3A%22%3C%3Cadd(m%2Cn)%3B%20%20%0A%20%20return%200%3B%20%20%0A%7D%20%20%0A%0A” message=”” highlight=”” provider=”manual”/]Output

Class Template
- Class template is also similar to function template.
- If class uses the concept of template, then the class is known as generic class.
- In class template Ttype is a placeholder name which will be determined when the class is instantiated.
- We can define one or more than generic data type using a comma-separated list. It can be used inside the class body.
Sample Code
[pastacode lang=”cpp” manual=”%23include%C2%A0%3Ciostream%3E%C2%A0%C2%A0%0A%0Ausing%C2%A0namespace%C2%A0std%3B%C2%A0%C2%A0%0A%0Atemplate%3Cclass%C2%A0T%3E%C2%A0%C2%A0%0A%0Aclass%C2%A0A%C2%A0%C2%A0%C2%A0%0A%0A%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0public%3A%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0T%C2%A0num1%C2%A0%3D%C2%A05%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0T%C2%A0num2%C2%A0%3D%C2%A06%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0void%C2%A0add()%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0std%3A%3Acout%C2%A0%3C%3C%C2%A0%22Addition%C2%A0of%C2%A0num1%C2%A0and%C2%A0num2%C2%A0%3A%C2%A0%22%C2%A0%3C%3C%C2%A0num1%2Bnum2%3C%3Cstd%3A%3Aendl%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7D%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%0A%0A%7D%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%0A%0Aint%C2%A0main()%C2%A0%C2%A0%0A%0A%7B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0A%3Cint%3E%C2%A0d%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0d.add()%3B%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0return%C2%A00%3B%C2%A0%C2%A0%0A%0A%7D%C2%A0%C2%A0″ message=”” highlight=”” provider=”manual”/]Output

