Templates in C++

What is Template?

  • Template is a blueprint for creating a generic class or a function.
  • It is the foundation of generic programming.
  • It enables you to define a family of functions or classes that can operate on different types of information.
  • The C++ template is based on the ISO/ANSI C++ standard.
  • Templates are the better solution than C macros and void pointers and it is mostly useful when working with collections and smart pointers.
  • Using template reduces multiple functions and decreases the code size. The task of writing the program will also be simple for the programmer.
  • Template is used to create a generic functions with the keyword template.
Templates can be used in two different ways.
1. Function Template
2. Class Template

1. Function Template

  • Function templates are used to create a set of functions that apply the same algorithm to different data types.
  • It works like a function with the only difference that it works on different data types at once, but different functions are needed to perform identical task on different data types.
  • Function overloading is used to perform identical operations on two or more types of data, but function template is best approach to perform this task by writing less code and the code is easier to maintain.
    Syntax:
    template <class_type> return_datatype function_name (argument_lists)
    {
        //Statements;
    }

Example: Program demonstrating the generic function or template

#include<iostream>
using namespace std;
template <class A> A addition(A x, A y)
{
     return ( x + y );
}
int main()
{
     cout<<"Addition1 : "<<addition(4,4)<<endl;
     cout<<"Addition2 : "<<addition(4.2,3.6)<<endl;
     cout<<"Addition3 : "<<addition(4.0,4.7)<<endl;
     return 0;
}


Output:
Addition1 : 8
Addition2 : 7.8
Addition3 : 8.7

In the above program
  • In first cout statement values are passed as integer.
  • In second cout statement, values are passed as float.
  • In third cout statement, value 4.0 & 4.7 are passed instead of 4 & 4.7 and it will be integer and float which are different; while the generic function claims both the parameters to be of same data type.

2. Class Template

  • We can also define class templates same like function template.
  • Class templates are used to develop a set of typesafe classes.
    Syntax : Class Template
    template <class_type> class class_name
    {
        //Statements
    }
  • Programmer can define more than one generic data type by using a comma (,) for separating the list.
  • Class templates are used for creating a family of classes that operate one a type.
  • Class templates are parameterized types.
  • Class template implies that a separate class could be created for each conceivable value of the parameters (known as template arguments) passed in.
  • Template arguments can be types or constant values of a specified type.

Example: Program demonstrating the Class Template

#include<iostream>
using namespace std;
template <class A> class Base
{
    public:
    float average();
};
template <class A> float Base <A> :: average()
{
    float avg;
    avg = (62 + 70 + 55 + 60 + 75 ) / 5.0;
    return avg;
}
int main()
{
    Base <float> b;
    cout<<"Average is : "<<b.average()<<endl;
    return 0;
}


Output:
Average is : 64.4

Overloading of Function Templates

Template function can be overloaded using  template for a special case of data.

Example: Program demonstrating the Overloading of Function Template

//Overloaded functions using a Template function

#include <iostream>
using namespace std;
template <class A>A addition(A num1, A num2)
{
      return num1 + num2;
}
template <class A>A addition(float num1, A num2)
{
      return num1 + num2;
}
template <class A>A addition(char num1, A num3)
{
      return(num1 + num3);
}
int main ()
{
     cout<<"Addition of Integer Numbers : "<<addition(10,20)<<endl;
     cout<<"Addition of Float Numbers : "<<addition(10.0,15.5)<<endl;
     cout<<"Addition of Characters : "<<addition('C',5)<<endl;
     return 0;
}


Output:
Addition of Integer Numbers : 30
Addition of Float Numbers : 25.5
Addition of Characters : 72

The above program shows the addition of a special case char data type with integer type data.  

template <class A>A addition(char num1, A num3)
{
      return(num1+num3);
}
cout<<"Addition of Characters : "<<addition('C',5)<<endl;


In the above statement, in cout, addition function passes 'C' and 5 value, where it takes the ASCII value of C that is 67 and assigns it to the num1 parameter and displays the addition.