C++ program to Swap variables using Function Overloading

By | 01.01.2017

Swap variables using Function Overloading


Write a C++ program to Swap variables using Function Overloading. Here’s a Simple C++ program to Swap variables using Function Overloading in C++ Programming Language.


What is Overloading in C++ ?


C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.


Function overloading : :

You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You can not overload function declarations that differ only by return type.


Operators overloading : :

You can redefine or overload most of the built-in operators available in C++. Thus a programmer can use operators with user-defined types as well.

Overloaded operators are functions with special names the keyword operator followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.


Below is the source code for C++ program to Swap variables using Function Overloading which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ program to Swap variables using Function Overloading */

#include<iostream>
using namespace std;
void swap(int &ix,int &iy);
void swap(float &fx,float &fy);
void swap(char &cx,char &cy);
int main()
{
                int ix,iy;
float fx,fy;
char cx,cy;

cout<<"Enter 2 integers:";
cin>>ix>>iy;
cout<<"Enter 2 floating point no:s:";
cin>>fx>>fy;
cout<<"Enter 2 characters:";
cin>>cx>>cy;
cout<<"\nIntegers:";
cout<<"\nix="<<ix<<"\niy="<<iy;
swap(ix,iy);
cout<<"\nAfter swapping";
cout<<"\nix="<<ix<<"\niy="<<iy;
cout<<"\nFloating point no:s";
cout<<"\nfx="<<fx<<"\nfy="<<fy;
swap(fx,fy);
cout<<"\nAfter swapping";
cout<<"\nfx="<<fx<<"\nfy="<<fy;
cout<<"\nCharacters";
cout<<"\ncx="<<cx<<"\ncy="<<cy;
swap(cx,cy);
cout<<"\nAfter swapping";
cout<<"\ncx="<<cx<<"\ncy="<<cy;
return 0;
}
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void swap(float &a, float &b)
                {
float temp;
temp=a;
a=b;
b=temp;
}
void swap(char &a, char &b)
{
char temp;
temp=a;
a=b;
b=temp;
}

OUTPUT : :


/*  C++ program to Swap variables using Function Overloading */

Enter 2 integers:300 800                                                                                                                                                                                                       
Enter 2 floating point no:s:2.3 4.5                                                                                                                                                                                            
Enter 2 characters:d q                                                                                                                                                                                                         
                                                                                                                                                                                                                               
Integers:                                                                                                                                                                                                                      
ix=300                                                                                                                                                                                                                         
iy=800                                                                                                                                                                                                                         
After swapping                                                                                                                                                                                                                 
ix=800                                                                                                                                                                                                                         
iy=300                                                                                                                                                                                                                         
Floating point no:s                                                                                                                                                                                                            
fx=2.3                                                                                                                                                                                                                         
fy=4.5                                                                                                                                                                                                                         
After swapping                                                                                                                                                                                                                 
fx=4.5                                                                                                                                                                                                                         
fy=2.3                                                                                                                                                                                                                         
Characters                                                                                                                                                                                                                     
cx=d                                                                                                                                                                                                                           
cy=q                                                                                                                                                                                                                           
After swapping                                                                                                                                                                                                                 
cx=q                                                                                                                                                                                                                           
cy=d

Above is the source code and output for C++ program to Swap variables using Function Overloading which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

4.3 4 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments