GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 132 of 203    Print  
what is the difference between function templates and function overloading?

  
Total Answers and Comments: 3 Last Update: September 23, 2007     Asked by: astha 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Raj
 

The function template has only one definition for various types.
But its not the case for the function over loading.

For eg.,

#include "stdafx.h"
#include "stdio.h"
#include "windows.h"
#include<iostream.h>
#include "test.h"


template < class T>
T add(T a, T b)
{
 T c;

 c= a+b;
 
 return c;
}


int main()
{

 cout << add<int>(1,2)<<endl;
 cout << add<float>(1.0,2.0)endl;


return 0;


}


But for the function overloading there has to be different datatypes defined for each functions.



Above answer was rated as good by the following members:
rish_LP, YaelG, yzesong, j_l_larson
July 21, 2007 04:52:40   #1  
M.Senthil Cholan        

what is the difference between function templates ...
Function body will not differ in function Template function body can differ in function overloading
 
Is this answer useful? Yes | NoAnswer is useful 4   Answer is not useful 0Overall Rating: +4    
September 15, 2007 19:14:57   #2  
Raj        

RE: what is the difference between function templates ...

The function template has only one definition for various types.
But its not the case for the function over loading.

For eg.

#include "stdafx.h"
#include "stdio.h"
#include "windows.h"
#include<iostream.h>
#include "test.h"


template < class T>
T add(T a T b)
{
T c;

c a+b;

return c;
}


int main()
{

cout << add<int>(1 2)<<endl;
cout << add<float>(1.0 2.0)endl;


return 0;


}


But for the function overloading there has to be different datatypes defined for each functions.


 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
September 22, 2007 06:13:29   #3  
bornToLose        

RE: what is the difference between function templates ...
Function overloading is declaring function with same names but different return parameter and datatype. It is like normal function with same name


Function template is a generic type of function in which we provide a structure for function and its upto which how we used it for which type of data

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape