Written by Sai Sandeep Thota on March 25, 2013 in C++ > Programming

C++ Program to Implement Swapping using Templates

GET ALERTS:

Get our Latest updates delivered to your mailbox!
#include<conio.h>
#include<iostream.h>

template<class swap>
void swap(swap &i, swap &j)
{
	swap t;
	t=i;
	i=j;
	j=t;
}


void main() {
	int i,j;
	char a,b;
	float c,d;
	clrscr();
	cout<<"\nEnter 2 Integer Values:";
	cin>>i>>j;
	swap(i,j);
	cout<<"\nInteget Values after Swapping:";
	cout<<i<<"\t"<<j<<"\n\n";
	cout<<"\nEnter 2 Character Values:";
	cin>>a>>b;
	swap(a,b);
	cout<<"\nCharacter Values after Swapping:";
	cout<<a<<"\t"<<b<<"\n\n";
	cout<<"\nEnter 2 Float Values:";
	cin>>c>>d;
	swap(c,d);
	cout<<"\nFloat Values after Swapping:";
	cout<<c<<"\t"<<d<<"\n\n";
	getch();
}

Output:
swapping-using-templates

Comments on this entry are closed.

Previous post:

Next post: