PROGRAM TO SWAP TWO NUMBERS USING FRIEND FUNCTION



Hello Friends!!!!! Today I am going to Learn a program to swap of two numbers using friend function in C++. In this program we have two classes and a friend function which is friendly with both classes.
So Let's Start=>

//Name : Mayank Sharma //
//Roll No : 1101026 //
//CSE(4th Sem)

///////////* Write a program to Swap two numbers using Friend Function */////////////

#include
#include
class robot;
class angel
{
int x;
public:
void indata(int a)
{
x=a;
}
void display()
{
cout< }
friend void swap(angel &,robot &);
};
class robot
{
int y;
public:
void indata(int a)
{
y=a;
}
void display()
{
cout< }
friend void swap(angel &,robot &);
};
void swap(angel & c,robot & d)
{
int temp=c.x;
c.x=d.y;
d.y=temp;
}
void main()
{
clrscr();
angel n;
robot r;
n.indata(100);
r.indata(200);
cout<<"values before swapping"<<"\n";
n.display();
r.display();
swap(n,r);
cout<<"after swaping"<<"\n";
n.display();
r.display();
getch();
}
I think this post will help you.I think this post will help you.
Thanks!!!
If any query about this post then put your comment in comment box below..

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites