Listing 1 A function template for swapping two objects of the same type

// swap.cpp
#include <iostream.h>

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

main()
{
   int a = 1, b = 2;
   double c = 1.1, d = 2;
   char *s = "hello", *t = "there";

   swap(a,b);
   cout << "a = " << a << ", b = " << b << '\n';

   swap(c,d);
   cout<< "c = " << c << ", d = " << d << '\n';

   swap(s,t);
   cout<< "s = " << s << ", t = " << t << '\n';

   return 0;
}

/* Output;
a = 2, b = 1
c = 2.2, d = 1.1
s = there, t = hello

// End of File