#include<iostream.h>
#include<conio.h>
template <class T>
T swap(T &a, T &b)
{
T temp;
temp = a;
a = b;
b = temp;
return b;
}
int main()
{
clrscr();
int x, y;
float p, q;
double dv1, dv2;
cout<<endl;
cout<<"Enter two integers" <<endl;
cin >>x>>y;
cout<<endl;
cout<<"Before swapping, x = "<<x<<" and y = "<<y<<endl;
swap (x,y);
cout<<"After swapping, x = " <<x<< " and y = "<<y<<endl;
cout<<endl;
cout<<"Enter two floating-point numbers "<<endl;
cout<<endl;
cin>>p>>q;
cout<<"Before swapping, p = "<<p<<"and q = "<<q<<endl;
swap(p,q);
cout<<endl;
cout<<"After swapping, p = " <<p<< " and q = "<<q<<endl;
cout<<"Enter two double-precision numbers"<<endl;
cin>>dv1>>dv2;
cout<<endl;
cout<<"Before swapping, dv1 = "<<dv1<<" and dv2 ="<<dv2<<endl;
swap (dv1, dv2);
cout<<"After swapping, dv1 = "<<dv1<<" and dv2 = "<<dv2<<endl;
getch();
return 0;
}
|
|

| This program is to define a function template to swap the contents of two data items of type int, float, and double. |
|
| |
|
|
|
|