A template is a formula for creating a generic class or a function. C++ templates enable you to define a family of functions or classes that can operate on different types of information.
A function template by itself is not a type, or a function, or any other entity. No code is generated from a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be determined so that the compiler can generate an actual function (or class, from a class template).
Que- Find maximum of different data types using templates in a generic function;
#include "iostream" #include "string" #include"conio.h" using namespace std; template <typename T> T maximum (T a, T b) { return a < b ? b:a; } int main () { int i = 50; int j = 40; cout << "Maximum in (i, j): " << maximum(i, j) << endl; double f = 16.9; double g = 76.1; cout << "Maximum in(f, g): " << maximum(f, g) << endl; string h = "Tanu"; string k = "Welcome"; cout << "Maximum in(h, k): " << maximum(h, k) << endl; cout<<"\nPROGRAMMING @ C#ODE STUDIO"; getch(); return 0; }