Like class templates, we can also define function templates. When we define a function template, we have to write a code for one function with generic types and use the same with required types as and when needed.


SYNTAX:-
          template<class T>
          return_type  function_name (arguments of type T)
          {
           Body of function with type T wherever appropriate ;
           }
Templates are a 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/function that operates on data of many types, instead of having to create a separate class/function for each type when used with functions, they are known as function templates. Whereas when used with classes they are called as class templates.

For example a simple function that finds the sum of two numbers:
               void  sum(int  a , int b)
                   {
                        cout<< a+b;
                   }
      Here the function is defined to take arguments of integer type and finds a sum. Suppose if we want to find the sum of two floats or two double, we would be required to write separate versions of the same function. This is nothing but function overloading, these results into three disadvantages:
  1.  Rewriting the same function body over and over for different types is time consuming.
  2.   The program consumes more disk space.
  3.  If we locate any error in one such function, we need to remember to correct it in each function body.
Advantages of templates:
  • It provides a way to code reuse ability.
  • Inheritance and containership provide a way to reuse object code.
  • Templates provide a way to reuse source code.
  • They significantly reduce source code size and increase code flexibility without reducing type safety.
  • In a function template a data type can be represented by a name that can stand for any type.
         Syntax:
                      template  <class   [name]>
                      [name]    function_name( [name]  variable1, [name]  variable2 .......)
                     {
                           Body of function
                      }
  • [name] is known as template argument.
  • Throughout the definition of the function, wherever a specific data type is declared, we substitute the template argument.
/*Program Illustration of function template */
#include<iostream.h>
template <class T>
void  sum(T  x ,T  y)
{
         cout<<x+y<<endl;
}
int  main( )
{
          cout<<Function Template<<endl;
          cout<<Sum of two integer nos:;
          sum(1,2);                //two integer arguments
          cout<<Sum of two float nos:;
          sum(3.4,6.8);           //two float arguments
          return 0;
}

Output:
           Function Template
           Sum of two integer no’s: 3
          Sum of two float no’s: 10.2

Here is another general format for function template where a function returns a value.
Syntax:
 template<class   T>
 T   function_name(arguments of type T)
{
            Body of function
 }

/*Program to find Maximum of two numbers using function template */
#include<iostream.h>
template<class T>
{
             return (a>b)?a:b;
}
int main( )
{
             int i=10,j=20;
             cout<<The maximum no of two integers:<< max( i , j );
             float a=3.14, b=6.28;
             cout<<”\nThe maximum no of two floats:<< max( a , b );
             return 0;
}

Output:

             The maximum no of two integers: 20
             The maximum no of two floats: 6.28   

In the above function template a data type can be represented by a name T, that can stand for any type. We can use any other name like type, my type etc., instead of T. T is known as a template argument. Through the definition of the function, where ever a specific data type like int would ordinarily be written, we substitute the template argument T.

*Program to displaying the Input value using function template */
#include<iostream.h>
template<class  Type>  
void  display(Type  x)
{
            cout<<The input value=<< x <<endl;
}
int main( )
{  
         display(10);
         display(5.86);
         display(A); 
         return 0; 
}  

Output: 
                The input value= 10 
                The input value= 5.86 
                The input value= A



Posted by Unknown On 23:38 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *