What is Generic Programming? How it is implemented in C++.Write General format of class templates and function Template. Write program to swap Number using Function Template. Function prototype is given below:
void swap(int, int , float , float )
Swap two integer numbers and swap two float number.
Code :
#include<iostream.h>
#include<conio.h>
template<class T>
void swap(T &x,T&y)
{
T temp;
temp=x;
x=y;
y=temp;
}
void function(int m,int n,float a,float b)
{
cout<<"------Before Swap------"<<endl;
cout<<"M = "<<m<<endl;
cout<<"N = "<<n<<endl;
swap(m,n);
cout<<"\n---Interger Swap---"<<endl<<"M = "<<m<<endl<<"N = "<<n<<endl;
cout<<"\n------Before Swap------"<<endl;
cout<<"A = "<<a<<endl;
cout<<"B = "<<b<<endl;
swap(a,b);
cout<<"\n---Float Swap---"<<endl<<"A = "<<a<<endl<<"B = "<<b<<endl;
}
void main()
{
clrscr();
function(10,20,1.23,2.34);
getch();
}
Output :
No comments:
Post a Comment