c++

Templates in C++

In this tutorial, we are going to learn about C++ templates.

A template is a simple and powerful tool in C++ programming. Using this we pass data types as parameter so we don’t need to write codes again and again.

One of the best real life example of templates are forms. Forms may be of various kinds like bank form, admission form, account opening form etc. but the main aim of a form is to make data more generalized and accessible.

Templates are often used in larger codebase for the purpose of code reusability and flexibility of the programs.

The concept of templates can be used in two different ways:


Function templates

look at the following program

#include <iostream>
using namespace std;
int greater(int a,int b){   // first function
    if(a>b)
    return a;
    else
    return b;
}
double greater(double a,double b){
    if(a>b) // second funtion
    return a;
    else
    return b;
}
int main(){
    cout<< greater(5,4); //first function call
    cout<< greater(4.5,6.7); //second function call
    return 0;
}

As you can see in the above program, we want to pass values of two different data types. But for this we need to create two functions, have change only in data type of argument.

This can be resolved by making function templates.

The syntax of function template is given below:

template<class type> type funcName(type arg1,….);

the syntax consists keyword template followed by angular brackets containing a keyword class and a variable datatype type. you can represent type by any name and letter just as a variable. After type in angular bracket, there is one more type followed by function name and parameters.

Look at the following example

Example:

#include<iostream>
using namespace std;
template <class X> X greater(X a,X b){ //function template
    if(a>b)
    return a;
    else
    return b;
}
int main(){
    cout<< greater(5,4);
    cout<< greater(4.5,6.7); 
    return 0;
}

in the above program, we have X as type according to the syntax. Here X is acting as variable data type, which depends upon the type of value passed in the argument of the function.

For example, when 5 and 4 were passed in the argument, X was acting as int datatype. And when 4.5 and 6.7 were passed, X was acting as double.


Class templates

Class template is also known as generic class

The syntax of class template is given below.

template class className{….};

like functions, classes also requires parameters for their member functions. Thus they also need to have a general datatype, which change according to value passed. Hence we need class templates to make it possible.

Go through the following example for better understanding.

Example:

#include<iostream>
using namespace std;
template <typename T> //class template
class height{
T feet;
public:
void setData(T x)
{
feet=x
}
T getData(){
return feet;
}
};
int main(){
height <int>h1;
h1.setData(5);
cout<<"height is : "<< h1.getData<< endl;
height <double>h2;
height h2;
h2.setData(5.3);
cout<<"height is : "<< h2.getData()<< endl;
return 0;
}

Output:

height is : 5
height is : 5.3

in the above program, as you can see we need to pass values to a member function setData().

Hence we declared class height as class template as

template 
class height{…};

here we passed int value to the member function by declaring object as

height h1; 

similarly, passed double value to the same member function by declaring object as

height h2;