Design a template function for swapping the int, double and char type values between two variables. Note: Use function overloading.
#include<iostream>
using namespace std;
class SwapOVerload
{
public:
//Swap int parameters
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
//Swap double use temp as placeholder
void swap(double x,double y)
{
double temp;
temp=x;
x=y;
y=temp;
}
//Swap char types temp variable as placeholder
void swap(char x,char y)
{
char temp;
temp=x;
x=y;
y=temp;
}
};
int main ()
{
//Output to be implemented as needed.
SwapOVerload so;//Create object
so.swap(10,20); //This will be called to swap ints
so.swap(10.5,20.5); //This will be called to swap doubles
so.swap('X','Y');//This will be called to swap chars
return 0;
}
Comments
Leave a comment