Templates in C++

Hello Learners, Today in this tutorial we will learn about the Templates in C++ with some easy and comprehensible example. A template is one of the powerful and simple features of C++ which allows a programmer to write generic programs. With the help of templates, the size of the program is reduced.

Templates Or Generics in C++


Templates:
It allows us to design one function for several families of functions means it allows us to design one function for several function declaration. Templates further divided into two types:

  • Function Template (Generic function)
  •  Class Template (Generic Class)

Templates are also called generics because one function is designed for the different tasks means it works for integers, float, doubles, etc.

  • Function Template
    Syntax Of Function Template:

    template <class t>
    
    return_type  Fun_name(parameters)
    {-------;
     --------;    // some code
     --------;
    }
    
    

Let’s see some examples of the template function

    // One Function For Different Data Types

#include<bits/stdc++.h> 
using namespace std; 

    template <class t>               // Template Creation
    float sum(t1 x , t2 y)
    {
        return (x+y);
    }
    
    int main()
        {
             // Calling sum for integer type data
            cout<<"Integer Sum = "<<sum(10 ,20)<<endl;
            
             // Calling sum for Float type data
            cout<<"Float Sum = "<<sum(1.2 ,2.3)<<endl;
            
            // Calling sum for integer-float type data
            cout<<"Integer - Float Sum = "<<sum(5 ,2.5)<<endl;
            
            // Calling sum for integer type data
            cout<<"Float-Integer Sum = "<<sum(1.2 ,5)<<endl;         

          return 0; 
        } 
Output:
Integer Sum = 30                                                                               
Float Sum = 3.5                                                                                
Integer - Float Sum = 7.5                                                                      
Float-Integer Sum = 6.2

 

Here, we see that one function can be used in 4 ways i.e why it is called a generic function. It is the advantage of using templates so many functions definition not required due to which program size will be reduced hence performance increases.

 

Now an example of function templates in an array

    // Function templates in array.

#include<bits/stdc++.h> 
using namespace std; 

 // t is return type 
 
    template <class t>               // Template Creation
    t sum(t arr[] , int size)  // size of an array is always integer
    {
        t sum1=0; 
        for (int i = 0; i < size; i++) {
            sum1 += arr[i];
        }
        return sum1;
    }
    
    int main()
        {
            int x[5] = {10, 20, 30, 40, 50};
            float y[3] = {1.1, 2.2, 3.3};
            
            std::cout << "Integer array sum = "<<  sum(x,5) << std::endl;
            std::cout << "Float array sum = "<<  sum(y,3) << std::endl;

            return 0
        }
Output:
Integer array sum = 150                                                                        
Float array sum = 6.6

 

Now let’s see an example of overloading of a function template

 

    // Overloading of a Template Function

#include<bits/stdc++.h> 
using namespace std; 

 // t is return type 
 
    template <class t>               // Template Creation
    t Sum(t a , t b)
    {
        return(a+b);
    }    
    template <class t>
    t Sum(t a, t b, t c)    // overloading of template Function
    {
        return(a+b+c);
    }
    int main()
        {
            
            std::cout << "Two Integer sum = " <<  Sum(10,5) << std::endl;
            std::cout << "Two Float sum = "   <<  Sum(4.4,3.2) << std::endl;
            std::cout << "Three Integer sum = "<< Sum(3,5,6) << std::endl;
            std::cout << "Three Float sum = " <<  Sum(1.5,3.1,2.2) << std::endl;
        
            return 0;
        }

Output:
Two Integer sum = 15                                                                           
Two Float sum = 7.6                                                                            
Three Integer sum = 14                                                                         
Three Float sum = 6.8

 

Class Template:

Using one class for different purposes.

    //  Class Template

#include<bits/stdc++.h> 
using namespace std; 

 
    template <class t>      // Template Creation
    class test
    {
        t a,b;
      
        public:
        void getdata()   // Function definition
        {
            std::cin >> a >> b;
        }    
        
        t sum();    // Function declaration
    };
    
    // Before definition of Function we have to write template <class t> 
    // this is the syntax of defining a Function outside the class. 
    
    template <class t>
    t test< t > :: sum()
    {
        return (a+b);
    }
    
    int main()
        {
            test<int> t1;
            test<float> t2;
            
            std::cout << "Enter two integers" << std::endl;
            
            t1.getdata();
            std::cout << "Sum = " << t1.sum() << std::endl;
            
            std::cout << "Enter two floats" << std::endl;
            
            t2.getdata();
            std::cout << "Sum = " << t2.sum() << std::endl;
            
            return 0;
        }
Output:
Enter two integers                                                                             
55 44                                                                                          
Sum = 99                                                                                       
Enter two floats                                                                               
3.3 4.4                                                                                        
Sum = 7.7

Hope this article was helpful to you. Keep Coding Keep Learning. Also Read:

  1. Function Templates in C++
  2. Template Parameters and Template Arguments in C++
  3. Variadic function templates in C++ with example

Leave a Reply

Your email address will not be published. Required fields are marked *