Sunday 10 January 2016

Templates in C++ Programming

Templates in C++ programming allows function or class to work on different data types without writing different codes for different data types. Templates are often used in larger software and program for the purpose of code reusability and flexibility of program.

Function Templates

A function templates works in similar manner as function except a single function template can work on different types but, different functions are needed to perform identical task on different data. If you need to perform identical operations on two or more types of data then, you can use function overloading. But, the better approach would be to use function templates because you can perform this task by writing less code and code is easier to maintain.

How to define function template?

A function template starts with keyword template followed by template parameter/s inside  < > which is followed by function declaration.
template <class T>
 T some_function(T arg)
{
   .... ... ....
}
In above code, T is a template argument and class is a keyword. You can use keyword typename instead of class in above example. When, an argument is passed to some_function( ), compiler generates new version of some_function() to work on argument of that type.

Example of Function Template


/* C++ program to display larger number among two numbers using function templates. */
/* If two characters are passed to function template, character with larger ASCII value is displayed. */

#include <iostream>
using namespace std;
template <class T>
T Large(T n1, T n2)
{
 return (n1>n2) ? n1:n2;
}
int main()
{
 int i1, i2;
 float f1, f2;
 char c1, c2;
 cout<<"Enter two integers: ";
 cin>>i1>>i2;
 cout<<Large(i1, i2)<<" is larger.";
 cout<<"\n\nEnter two floating-point numbers: ";
 cin>>f1>>f2;
 cout<<Large(f1, f2)<<" is larger.";
 cout<<"\n\nEnter two characters: ";
 cin>>c1>>c2;
 cout<<Large(c1, c2)<<" has larger ASCII value.";
 return 0;
}

Explanation
In this program, data of three different types: int, float and char is passed to function template and this template returns the larger of two data passed. In function template data type is represented by name: T in above example. During run-time, when integer data is passed to template function then, compiler knows the type to use is int. Similarly, when floating-point data and char data is passed, it knows the type to use is float and char respectively. After knowing the information of a type, it generates the specific version of Large( ) to work for that type.

Example to Swap Datas Using Concept of Templates


/* C++ program to swap datas entered by user. */

#include <iostream>
using namespace std;
template <typename T>
void Swap(T &n1, T &n2)
{
 T temp;
 temp = n1;
 n1 = n2;
 n2 = temp;
}

int main()
{
 int i1=1, i2=2;
 float f1=1.1, f2=2.2;
 char c1='a', c2='b';
 cout<<"Before passing data to function template.\n";
 cout<<"i1="<<i1<<"\ni2="<<i2;
 cout<<"\nf1="<<f1<<"\nf2="<<f2;
 cout<<"\nc1="<<c1<<"\nc2="<<c2;

 Swap(i1, i2);
 Swap(f1, f2);
 Swap(c1, c2);

        cout<<"\n\nAfter passing data to function template.\n";
 cout<<"i1="<<i1<<"\ni2="<<i2;
 cout<<"\nf1="<<f1<<"\nf2="<<f2;
 cout<<"\nc1="<<c1<<"\nc2="<<c2;
 return 0;
}
Output
Before passing data to function template.
i1=1
i2=2
f1=1.1
f2=2.2
c1=a
c2=b

After passing data to function template.
i1=2
i2=1
f1=2.2
f2=1.1
c1=b
c2=a