Flipkart

Tuesday, August 6, 2013

program to demonstrate the function template In C++


program to demonstrate the function template 
#include <iostream.h>

/*

Function
templates are a way of defining a whole
family of functions which are
essentially the
same except that they work on different
types

*/


// Define the
template

template<typename T>
void swap(T& x, T&
y)
{
T tmp = x;
x = y;
y = tmp;
}

// Write
some code to make use of the template

int main () {

int x =
6;
int y = 12;

char *left = "To the West";
char *right = "To the
East";

swap(x,y);
swap(left,right);

cout << "x is now "
<< x << endl;
cout << "y is now " << y <<
endl;

cout << "left is now " << left << endl;
cout
<< "right is now " << right << endl;
}

https://youtu.be/jA3imJeU9-U