Answer to Question #186177 in C++ for Ajith

Question #186177

Design a template function for swapping the int, double and char type values between two variables. Note: Use function overloading.



1
Expert's answer
2021-05-01T08:12:56-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS