C++ program to swap two variables using function overloading

FUNCTION OVERLOADING

AIM:
A program to demonstrate how function overloading is carried out for swapping of two variables of the various data types, namely integer, floating point number and character types

ALGORITHAM:

• Start the process
• Get the integer values of ix,iy
• Get the floating values of fx,fy
• Get the character values of cx,cy
• Call swap(ix,iy)
o Assign temp<-a o Assighn a<-b,b<-temp • Swapping the integer values • Print the value of ix and iy • Swapping floating values • Print the values oh fx and fy • Swapping on characters • Print the value of cx,cy • Stop the process PROGRAM

#include<iostream.h>
#include<conio.h>
void swap(int &ix,int &iy);
void swap(float &fx,float &fy);
void swap(char &cx,char &cy);
void main()
{
		int ix,iy;
float fx,fy;
char cx,cy;
clrscr();
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;
getch();
}
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;
}

Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.

Output:

Enter 2 integers: 100 200
Enter 2 floating point no:s :-11.11 22.22
Enter 2 characters: s t

Integers:
Ix=100
Iy=200
After swapping
Ix=200
Iy=100
Floating point no:
Fx=-11.11
Fy=22.22
After swapping
Fx=22.22
Fy=-11.11
Characters
Cx=s
Cy=t
After swapping
Cx=t
Cx=s


23 Responses to “C++ program to swap two variables using function overloading”

  1. Fletcher Fitzhardinge

    Wow! At last I got a web site from where I bee able to in fact get helpful data
    concerning my study aand knowledge.

    Reply
  2. Jennie Mcwhorter

    I like what you guys are usually up too. This sort of clever
    work and reporting! Keep up the terrific works guys I’ve included youu guys to our blogroll.

    Reply

Leave a Reply