Loading...
C++ Templates

C++ Templates

In this tutorial, we will learn about Templates in C++ and how to use the power of templates for generic programming with the help of examples.

Templates

Templates are powerful features of C++ which allows you to write generic programs. The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types.
For example:- a software company may need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass data type as a parameter.

Ways to use Templates

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

  • Function Templates
  • Class Templates

Function Templates

A function template works in a similar to a normal function, with one key difference. A single function template can work with different data types at once but, a single normal function can only work with one set of data types.

Normally, if you need to perform operations on two or more data types, you use function overloading. However, a better approach would be to use function templates because you can perform the same task writing less and maintainable code.

For example :- Quick sorting algorithm is implemented using a generic function, it can be implemented to an array of integers or array of floats.


How to declare a function template?

A function template starts with the keyword template followed by template parameter/s inside  < > which is followed by function declaration.

template <class T>
T someFunction(T arg)
{
    ....  .. ....
}

In the above code, T is a template argument that accepts different data types (int, float), and class is a keyword.

You can also use keyword typename instead of class in the above example.

When, an argument of a data type is passed to someFunction( ), compiler generates a new version of someFunction() for the given data type.


Example 1: C++ Program to display largest 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 function
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:\n";
    cin >> i1 >> i2;
    cout << Large(i1, i2) <<" is larger." << endl;
    
    cout << "\nEnter two floating-point numbers:\n";
    cin >> f1 >> f2;
    cout << Large(f1, f2) <<" is larger." << endl;
    
    cout << "\nEnter two characters:\n";
    cin >> c1 >> c2;
    cout << Large(c1, c2) << " has larger ASCII value.";
    
    return 0;
}

Output

Enter two integers:
2
6
6 is larger.
 
Enter two floating-point numbers:
5.6
3.2
5.6 is larger.
 
Enter two characters:
z
Z
z has larger ASCII value.

In the above program, a function template Large() is defined that accepts two arguments n1 and n2 of data type T. T signifies that argument can be of any data type.

Large() function returns the largest among the two arguments using a simple conditional operation.

Inside the main() function, variables of three different data types: int, float and char are declared. The variables are then passed to the Large() function template as normal functions. During run-time, when a integer, floating-point data and char data are passed to the template function, compiler knows it has to generate a Large() function to accept the arguments and does so.

This way, using only a single function template replaced three identical normal functions and made your code maintainable.


Example 2: C++ Program to swap data using function templates.

#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

In this program, instead of calling a function by passing a value, a call by reference is issued.

The Swap() function template takes two arguments and swaps them by reference.


Example 3: C++ Function template with Multiple Parameters

#include <iostream>
using namespace std;
template < class A, class B >
    void displayResult(A num1, B num2)
    {
        cout << "Values of num1 and num2: " << num1 <<", " << num2 << endl;
};

int main()
{
    displayResult(2, 4.5);
    return 0;
}

Output

Values of num1 and num2: 2, 4.5

Class Templates

Class Template can also be defined similarly to the Function Template. When a class uses the concept of Template, then the class is known as generic class. Like function templates, class templates are useful when a class defines something that is independent of the data type.

Sometimes, you need a class implementation that is same for all classes, only the data types used are different. However, class templates make it easy to reuse the same code for all data types.


How to declare a class template?

template <class T>
class className
{
   ... .. ...
public:
   T var;
   T operation(T arg);
   ... .. ...
};

In the above declaration, T is the template argument which is a placeholder for the data type used.

Inside the class body, a member variable var and a member function operation() are both of type T.


How to create a class template object?

To create a class template object, you need to define the data type inside a < > when creation.

className<dataType> classObject;

For example:

className<int> classObject;
className<float> classObject;
className<string> classObject;

Example 4: C++ Program to create Simple calculator using Class template

Program to add, subtract, multiply and divide two numbers using class template

#include <iostream>
using namespace std;

template <class T>
class Calculator
{
private:
    T num1, num2;
    
public:
    Calculator(T n1, T n2)
    {
        num1 = n1;
        num2 = n2;
    }
    
void displayResult()
{
    cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
    cout << "Addition is: " << add() << endl;
    cout << "Subtraction is: " << subtract() << endl;
    cout << "Product is: " << multiply() << endl;
    cout << "Division is: " << divide() << endl;
}
    
    T add() { return num1 + num2; }
    T subtract() { return num1 - num2; }
    T multiply() { return num1 * num2; }
    T divide() { return num1 / num2; }
};

int main()
{
    Calculator<int> intCalc(2, 1);
    Calculator<float> floatCalc(2.4, 1.2);
    
    cout << "Int results:" << endl;
    intCalc.displayResult();
    
    cout << endl << "Float results:" << endl;
    floatCalc.displayResult();
	
    return 0;
}

Output

Int results:
Numbers are: 2 and 1.
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2

Float results:
Numbers are: 2.4 and 1.2.
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2

In the above program, a class template Calculator is declared.

The class contains two private members of type T: num1 & num2, and a constructor to initalize the members.

It also contains public member functions to calculate the addition, subtraction, multiplication and division of the numbers which return the value of data type defined by the user. Likewise, a function displayResult() to display the final output to the screen.


Example 5: C++ Class template with Multiple Parameters

#include <iostream>
using namespace std;

template <class (T1, T2)>
class A
{
    T1 num1;
    T2 num2;
public:
    A(T n1, T n2)
    {
        num1 = n1;
        num2 = n2;
    }
    
    void displayResult()
    {
        cout << "Values of num1 and num2: " << n1 <<", " << n2 << endl;
    }
};

int main()
{
    A<int, float> d(2, 2.4);
    d.displayResult();
    return 0;
}

Output

Values of num1 and num2: 2, 2.4

Advantages of Templates

  • C++ supports a powerful feature known as a template to implement the concept of generic programming.
  • A template allows us to create a family of classes or family of functions to handle different data types.
  • Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster.
  • Multiple parameters can be used in both class and function template.
  • Template functions can also be overloaded.
  • We can also use nontype arguments such as built-in or derived data types as template arguments.

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Templates in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Exception Handling.

- Related Topics