This C++ program demonstrates template design by defining a generic swap function that can swap values of any type. The swap function is called to swap integer and float variables, showing it works for multiple data types. Main gets user input for integer and float variables, calls the fun function to print values before and after swapping, then waits for user input and exits.
This C++ program demonstrates template design by defining a generic swap function that can swap values of any type. The swap function is called to swap integer and float variables, showing it works for multiple data types. Main gets user input for integer and float variables, calls the fun function to print values before and after swapping, then waits for user input and exits.
This C++ program demonstrates template design by defining a generic swap function that can swap values of any type. The swap function is called to swap integer and float variables, showing it works for multiple data types. Main gets user input for integer and float variables, calls the fun function to print values before and after swapping, then waits for user input and exits.
This C++ program demonstrates template design by defining a generic swap function that can swap values of any type. The swap function is called to swap integer and float variables, showing it works for multiple data types. Main gets user input for integer and float variables, calls the fun function to print values before and after swapping, then waits for user input and exits.
#include<iostream.h> #include<conio.h> template <class t> void swap(t &x,t &y) { t temp; temp=x; x=y; y=temp; } void fun (int m,int n,float a,float b) { cout<<"m and n before swap:"<<m<<"\t"<<n<<"\n"; swap(m,n); cout<<"m and n after swap:"<<m<<"\t"<<n<<"\n"; cout<<"a and b before swap:"<<a<<"\t"<<b<<"\n"; swap(a,b); cout<<"a and b after swap:"<<a<<"\t"<<b<<"\n"; } int main () { int m,n; float a,b; clrscr(); cout<<"enter integer value for m and n "; cin >> m>>n; cout<<"enter float value for a and b "; cin >> a>>b; fun(m,n,a,b); getch(); return 0; }