Template Program 4


Write a program to create a function template for swapping the two value.


#include<iostream.h>
#include<conio.h>

template<class t>
void swap(t a, t b)
{
  t temp;
  temp=a;
  a=b;
  b=temp;
  cout<<"The swapped value is"<<a <<" " <<b;
}

void main()
{
 int x,y;
 float a,b;
  clrscr();
  cout<<"\n Enter two int values";
  cin>>x>>y;
  cout<<"\n Before swap" <<x <<" "<<y;
  swap(x,y);
  cout<<"\n After swap" <<x<< " "<<y;
  cout<<"\n Enter two float  values";
  cin>>a>>b;
  cout<<"\n Before swap" <<a <<" "<<b;
  swap(a,b);
  cout<<"\n After swap" <<a<< " "<<b;
  getch();
 }