TEMPLATES IN C++ :

GENERAL CONCEPT :

  • Templates are the mechanism that makes it possible to use one function or class to handle many different data-types.
  • By using templates, we can design a single class or function that operates on data of many types, instead of making a separate one for each data-type.
  • When used with Functions, they are called FUNCTION TEMPLATES and when used with Classes, they are called CLASS TEMPLATES.

HOW THEY WORK ?

  • Templates are expanded at compile-time. This is like macros. The difference is, compiler does type checking before template expansion.
  • The idea is simple, source code contains only function/class, but compiled code may contain multiple copies of same function/class.

(1) FUNCTION TEMPLATES :


  • Generic functions use the concept of a function template. Generic functions define a set of operations that can be applied to the various types of data.
  • For example, if sorting algorithms is implemented using a generic function, it can be implemented to an array of integers or array of floats.

SYNTAX :

template <class T>
return_type func_name(args)  
{  
    // body of function.  
    // T is the placeholder name for the data-type used by current called arguments.
}

EXAMPLES :

(1) For same data-types :

#include <iostream>
using namespace std;
template<class T>
T add(T &a,T &b)
{
    T result = a+b;
    return result;   
}
int main()
{
  int i =2;
  int j =3;
  float m = 2.3;
  float n = 1.2;
  cout << "Addition of i and j is : " << add(i,j) << "\n";
  cout << "Addition of m and n is : " << add(m,n) << "\n";
  return 0;
}
    Output : 
        Addition of i and j is : 5
        Addition of m and n is : 3.5

(2) For different data-types :

#include <iostream>
using namespace std;
template<class X,class Y>
void fun(X a,Y b)
{
    cout << "Value of a is : " << a << endl;
    cout << "Value of b is : " << b << endl;
}

int main()
{
   fun(15,12.3);
   return 0;
}
    Output:
        Value of a is : 15
        Value of b is : 12.3

We can overload a template function as well.


(2) 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.

SYNTAX :

(1) Definition:

        template  

        class class_name  

        {  

            // body of class  

        };

(2) Declaration of object :

        class_name obj;

EXAMPLE :

(1) With same data-types :

#include <iostream>  
using namespace std;  
template<class T>  
class A   
{  
    public:  
        T num1 = 5;  
        T num2 = 6;  
        void add()  
        {  
            cout << "Addition of num1 and num2 : " << num1+num2 << endl;  
        }    
};
int main()  
{  
    A<int> d;  
    d.add();  
    return 0;  
}  
    Output:
        Addition of num1 and num2 : 11

(2) With multiple data-types :

#include <iostream>  
using namespace std;  
template<class T1, class T2>  
class A   
{  
    T1 a;  
    T2 b;  
    public:  
    A(T1 x,T2 y)  
    {  
       a = x;  
       b = y;  
    }  
    void display()  
    {  
        cout << "Values of a and b are : " << a <<", " << b << endl;  
    }  
};  
int main()  
{  
   A<int,float> d(5,6.5);  
   d.display();  
   return 0;  
}  
    Output:
        Values of a and b are : 5, 6.5

(3) No-type template arguments :

template<class T, int size>  
class array  
{  
        T arr[size];    // automatic array initialization.  
};  

NOTES :

  • What happens when there is static member in a template class/function?
  • –> Each instance of a template contains its own static variable.
  • 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.
  • Multiple parameters can be used in both class and function template.
  • Template functions can also be overloaded.
  • We can also use non-type arguments such as built-in or derived data types as template arguments.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s