C++

Template in C++

In C++, Templates are powerful features. It allows us to write some special kind of programs. It means, we can create a single function or single class with different data types are used with the help of templates.

Characteristics of Template

  1. Template is a keyword in C++.
  2. A template is used to define function template and class template.
  3. Template is a procedure to make our functions or classes generalize as far as data type is concern.

Classification of Templates

There are two types of templates available in C++.

  1. Function Template
  2. Class Template.

a. Programming Example 1 of Function Template

#include<iostream>
using namespace std ;
void add ( int x , int y )
{
    cout<< “ Sum is :\n ”<< x+y << endl ;
}

int main()
{
   add ( 10, 15 ) ;
   add ( 10.50 , 10.25 ) ;
}

Output

Explanation
From the above program, we will know that why does template need in this particular type of program.

Here, we simply define a add function and two integer values are passed as an argument. It simply prints the sum of these two variables.

Inside the main function, we call the add function and pass two values 10 and 15. It simply adds these two values and we get the values 25.

But in the second add function when we pass two double type values 10.50 and 10.25, then the problem occurs in the sum result. Because here, we pass two double types of values inside the integer variables. As a result, it is shown incorrect result.

To resolve this problem, we introduce template.

b. Programming Example 2 of Function Template

#include<iostream>
using namespace std ;
template   //introducing function template
C add ( C x , C y )
{
   cout<< “ sum is :\n”<< x+y << endl ;
}


int main()
{
   sum ( 10 , 15 ) ;
   sum ( 10.50 , 10.25 ) ;
}

Output

Explanation
Where we define add function, we write the keyword template. Then in the angular bracket, we write class keyword. Then write a place holder named C. After that inside the add function where we wrote the data type integer, all these integer are replaced by the place holder C. This place holder C will be replaced by proper data type when we pass the values inside the sum function.

When we pass 10 and 15, C is replaced by the integer values but in the second case when we pass two double type values 1.50 and 10.25, then C will be replaced by the double data types. It is the advantage of using template in the program.

c. Programming Example 3 of Class Template

#include<iostream>
using namespace std ;
class Example
{
   private :
    int x , y ;
      public :
        Example ( int a , int b )
         {
       
x =  a ;
y =  b ;
}

void test()
   {
      If( x > y )
    {
       cout<< x<< “ is the largest number ”<< endl ;
    }
      else
    {
       cout<< y<< “ is the largest number ”<< endl ;
    }
  }
};


int main()
{
   Example ob1(10 , 15 ) ;
    ob1.test () ;
   Example ob2( 10.50 , 10.25 )
     ob2.test() ;
     return 0 ;
}

Output

Explanation
Here, we declare a class called Example. In the class, we call the constructor Example and pass two integer type variables to assign the value of variable x and y. The test function shows us which value is the largest.

Inside the main function, when we pass two values 10 and 15 during the creation of object ob, it shows the correct result.

But in the next case, when we pass two double data type values inside the integer type variables, it shows wrong result.

To resolve this problem again, we introduce template in the next program.

 d. Programming Example 4 of Class Template

#include<iostream>
using namespace std ;
template
class Example
{
   private :
    C x , y ;
      public :
        Example ( C a , C b )
         {
       
x  =  a ;
y =  b ;
}

void test()
   {
      If( x > y )
    {
       cout<< x<< “ is the largest number ”<< endl ;
    }
      else
    {
       cout<< y<< “ is the largest number ”<< endl ;
    }
  }
};

int main()
{
   Example  ob1( 10 , 15 ) ;
    ob1.test() ;
   Example   ob2( 10.50 , 10.25 ) ;
     ob2.test() ;
     return 0 ;
}

Output

Explanation
Before the definition of Example class, we write a line:

Template < class C >

As we discussed earlier, template is a keyword. Then inside the angular bracket, we write the keyword class, then a place holder named C.

After that every integer data type has to be replaced with C.

When we pass two integer types values 10 and 15, C is replaced by the integer variables. In the next case when we pass two double values 10.50 and 10.25, then C is replaced by the double data type.

The advantage is that for the different data type we do not write different code.

Conclusion

Discussing in detail about the concept of template it is clear to us that by creating template we can work with different data type in a single program or in a generalize program.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com