C++ Templates

The concept of templates in C++ brings a revolutionary enhancement to the language as it provides support for generic programming. In other words, templates allows functions as well as classes to operate on different data types (i.e. generic data types). This concept is quite similar to generics in Java.

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

  • Function Templates
  • Class Templates

 

Function Template

The general syntax of a function template is shown below:

template <class type> return_type function_name(parameter list) {
   // body of function
} 

Here, type is the placeholder name for a data type that can be used by the generic function.

When, an argument of a data type is passed to the generic function compiler generates a new version of the function for the given data type.

Example 1. The following is the example of a function template that returns the maximum of three values considering different data types (int, float and char)−

#include <iostream>
using namespace std;

// function template 
template <class T> T findMax(T a, T b, T c)
{
if(a>b && a>c) {
return a;
}
else if(b>a && b>c) {
return b;
}
else {
return c;
}
}

int main()
{
int a1, a2, a3;
float b1, b2, b3;
char c1, c2, c3;

cout << "Enter three integers: " << endl;
cin >> a1 >> a2 >> a3;
cout << findMax(a1, a2, a3) << " is maximum." << endl;

cout << "\nEnter three decimal numbers: " << endl;
cin >> b1 >> b2 >> b3;
cout << findMax(b1, b2, b3) << " is maximum." << endl;

cout << "\nEnter three characters:" << endl;
cin >> c1 >> c2 >> c3;
cout << findMax(c1, c2, c3) << " is having maximum ASCII value.";

return 0;
}

Output:

Enter three integers:
32 45 23
45 is maximum.

Enter three decimal numbers:
36.9 43.5 52.3
52.3 is maximum.

Enter three characters:
o m g
o is having maximum ASCII value.

 

Example 2. The following is the example of a function template that performs swapping of two values using call by reference for different data types (int, float and char)−

#include <iostream>
using namespace std;

// function template 
template <class T> void swapByRef(T &a, T &b)
{
T t = a;
a = b;
b = t;
}

int main()
{
int a1 = 10, a2 = 20;
float b1 = 11.1, b2 = 22.2;
char c1 = 'o', c2 = 'm';

cout << "Before passing data to function template." << endl;
cout << "a1 = " << a1 << "\na2 = " << a2;
cout << "\nb1 = " << b1 << "\nb2 = " << b2;
cout << "\nc1 = " << c1 << "\nc2 = " << c2;

swapByRef(a1, a2);
swapByRef(b1, b2);
swapByRef(c1, c2);

cout << "\n\nAfter passing data to function template." << endl;
cout << "a1 = " << a1 << "\na2 = " << a2;
cout << "\nb1 = " << b1 << "\nb2 = " << b2;
cout << "\nc1 = " << c1 << "\nc2 = " << c2;

return 0;
}

Output:

Before passing data to function template.
a1 = 10
a2 = 20
b1 = 11.1
b2 = 22.2
c1 = o
c2 = m

After passing data to function template.
a1 = 20
a2 = 10
b1 = 22.2
b2 = 11.1
c1 = m
c2 = o

 

 

Class Template

The general syntax of a class template is given below:

template <class type> class class-name {
  //code goes here
}

Here, type is the placeholder type name, which will be used when a class is instantiated. We can also define more than one generic data type by using a comma-separated list.

Example 3. Program to add two numbers using class template considering different data types (int and float):

#include <iostream>
using namespace std;

// class template
template <class T> class Sum
{
private:
T num1, num2;

public:
Sum(T a, T b) {
num1 = a;
num2 = b;
}

void printSum() {
cout << "Result: " << (num1 + num2) << endl;
}
}

int main()
{
Sum<int> intResult(12, 23);
Sum<float> floatResult(34.27, 41.68);

cout << "Int Sum:~" << endl;
intResult.printSum();

cout << endl << "Float Sum:~" << endl;
floatResult.printSum();

return 0;
}

Output:

Int Sum:~
Result: 35

Float Sum:~
Result: 75.95

 

In the above program, a class template Sum is declared. The class definition contains two private data members of type Tnum1 and num2, and a parameterized constructor to initialize the data members.

It also contains a public member function printSum() to display the result to the screen after performing the addition.

In the main() function, two different Sum objects intResult and floatResult are created for data types: int and float respectively. The values are initialized using the public constructor.

Notice that we use <int> and <float> while creating the objects, which tell the compiler the data type used for the class creation.

This creates a class definition each for int and float, which are then used accordingly.

Then, printSum() function of both objects is called which performs the Sum operation and displays the output.