program
to demonstrate the function template
#include
<iostream.h>
/*
Function
/*
Function
templates are a way of
defining a whole
family of functions which are
family of functions which are
essentially the
same except that they work on different
same except that they work on different
types
*/
// Define the
*/
// Define the
template
template<typename T>
void swap(T& x, T&
template<typename T>
void swap(T& x, T&
y)
{
T tmp = x;
x = y;
y = tmp;
}
// Write
{
T tmp = x;
x = y;
y = tmp;
}
// Write
some code to make use of
the template
int main () {
int x =
int main () {
int x =
6;
int y = 12;
char *left = "To the West";
char *right = "To the
int y = 12;
char *left = "To the West";
char *right = "To the
East";
swap(x,y);
swap(left,right);
cout << "x is now "
swap(x,y);
swap(left,right);
cout << "x is now "
<< x << endl;
cout << "y is now " << y <<
cout << "y is now " << y <<
endl;
cout << "left is now " << left << endl;
cout
cout << "left is now " << left << endl;
cout
<< "right is now " << right << endl;
}
}