Write a template function that swaps the values of two arguments passed to it. In main( ), use the function with integers and characters.
#include<iostream.h>
using namespace std;
template <class T>
void swap(T &a,T &b)
{
T temp=a;
a=b;
b=temp;
}
void func(int x,int y,char w,char z)
{
cout<<"x and y before swap: "<<x<<y<<"\n";
swap(x,y)
cout<<"x and y after swap: "<<x<<y<<"\n";
cout<<"w and z before swap: "<<w<<z<<"\n";
swap(w,z)
cout<<"w and z after swap: "<<w<<z<<"\n";
}
int main()
{
fun(10,20,’s’,’S’);
return 0;
}
You might also like:-
#include<iostream.h>
using namespace std;
template <class T>
void swap(T &a,T &b)
{
T temp=a;
a=b;
b=temp;
}
void func(int x,int y,char w,char z)
{
cout<<"x and y before swap: "<<x<<y<<"\n";
swap(x,y)
cout<<"x and y after swap: "<<x<<y<<"\n";
cout<<"w and z before swap: "<<w<<z<<"\n";
swap(w,z)
cout<<"w and z after swap: "<<w<<z<<"\n";
}
int main()
{
fun(10,20,’s’,’S’);
return 0;
}
You might also like:-
- PROGRAM TO PRINT A DIAMOND IN A BOX - 2
- LARGEST IN AN ARRAY
- Operator Overloading-Matrix
- C++ PROGRAM TO DO ELECTRICITY BILL CALCULATION
No comments:
Post a Comment