Like template
classes, we can use more than one generic data types in the function template.
The general
format is:
template <class T1,
class T2…….>
return_type
function_name (arguments of types T1, T2…..)
{
Body of the function;
|
}
/*1.Program to illustrate function template
with multiple parameters */
#include<iostream.h>
template <class T1, class T2>
void display (T1 x , T2 y)
{
cout << x << ” & ”<< y<<endl;
}
int main( )
{
display(“TARAK”,”JAGADEESH”);
display (‘G’,10.5);
display (99.5,200);
return 0;
}
Output:
TARAKA &
JAGADEESH
G
& 10.5
99.5
& 200
In the above example the place
holder types T1 and T2 are replaced by the string, string and char, float and
float, int respectively, when the compiler generates specific instances of display( ) within main( ).
/*2)Program to illustrate function template
with multiple parameters */
#include<iostream.h>
template <class T1, class T2>
void sum(T1 x , T2 y)
{
cout << x + y<<endl;
}
int main( )
{
cout<<”The sum of int and float no is:”;
sum( 4 , 10.6 );
cout<<”The sum of float and int no is:”;
sum( 8.35 , 20 );
return 0;
}
Output:
The sum of
int and float no is: 14.6
The
sum of float and int no is: 28.35
0 comments:
Post a Comment