Professional Documents
Culture Documents
WINSEM2021-22 BCSE102L TH VL2021220504676 Reference Material I 07-06-2022 Module 8 Function Template
WINSEM2021-22 BCSE102L TH VL2021220504676 Reference Material I 07-06-2022 Module 8 Function Template
Conventional Approach
void printInt( int n )
{
cout << “The Value is " << n ;
}
void printChar( char ch )
{
cout << “The Value is " << ch ;
}
void printFloat( float x )
{
cout<<“The value is”<<x;
}
void printDouble( double d )
{
cout<<“The value is”<<d;
}
Function overloading Approach
void Print( int n )
{
cout << “The Value is " << n ;
}
void Print( char ch )
{
cout << “The Value is " << ch ;
}
void Print( float x )
{
cout<<“The value is”<<x;
}
void Print( double d )
{
cout<<“The value is”<<d;
}
Generic function Approach
template<class SomeType> Template parameter
(class, user defined type,
void Print( SomeType val ) built-in types)
{
cout << “The value is " << val ;
}
Approach-1(Conventional)
data types
Approach-2(Function overloading)
• Eliminates the need to come up with many different names for identical
tasks.
Approach-3(Generic function)
Template Functions
One Function Definition (a function template)
Compiler Generates Individual Functions
How to perform the swapping on different data
items?
• The above program performs well and is
producing the output as well.
• But it is less efficient. Because the same
function “swap” is redefined by multiple
times.
• How to increase the efficiency of the above
program?
Templates
• Template is a kind of macro that supports the
generic programming which allows to develop the
reusable components.
• It is one of the main features of object oriented
language such as C++.
• Actually, it allows the declaration of data items
without specifying their exact data type. Two types:
1.Function template
2.Class template
Need of Function Templates
• Reusability
template<class type>
returntype functionname(arguments)
{ …………..
statements;
……….
}
Guidelines to use the function
template
1. No argument template function
template<class T>
T fun() //Error: T is not used as an argument
{
return n;
}
2. Template type argument unused
template<class T>
void test(int x) //Error: T is not used as an argument
{
T temp;
------
------
}
3.Usage of partial number of template
arguments
template<class T, class U>
void fun(T &x) //Error: U is not used as an argument
{
U temp;
------
------
}
Example Program
Overloaded Function Templates
• The function template can also be overloaded
with multiple declarations.
• As similar to function overloading, the
overloaded function templates must differ
interms of number of parameters or their
type.
• Declaration of a function template for
functions having multiple parameters of
different types requires multiple generic
arguments or template arguments
Example Program
How to use multiple parameters in
function template??
•In main(), the statement
assign_B(50,10.15,s2); leads to compilation
errors.
•Since, the above program is neither having the
normal function nor function template
matching with its parameters data type.
•Therefore, the solution to the problem
encountered in the above program is to declare
the second template function in the above
program as follows:
How to use multiple parameters in
function template??