jituTechnology

jituTechnology

>4.1 Write a program to swap the private data of two classes using friend function.

>

/*Write a program to swap the private data of two classes using friend function.

Programmed by: KiranShrestha
Roll No.: 10121
Programmed on: 31st July, 2011*/
#include <iostream.h>
#include <conio.h>
class b;
class a
{
int x;
public:
void assign(int t)
{
x =t;
}
void display()
{
cout<<“value of x is: “<<x<<endl;
}
friend void swap(a&,b&);
};
class b
{
int y;
public:
void assign(int w)
{
y=w;
}
void display()
{
cout<<“value of y is: “<<y<<endl;
}
friend void swap(a&,b&);
};
void swap(a &c,b&d)
{
int temp;
temp =c.x;
c.x = d.y;
d.y = temp;
}
int main()
{
a n;
b m;
inta,b;
cout<<“enter x =”;
cin>>a ;
cout<<“Enter y =”;
cin>>b;
n.assign(a);
m.assign(b);
swap(n,m);
n.display();
m.display();
getch();
}
Output Sample
Enter x= 4
Enter y= 6
Value of x is: 6
Value of y is: 4
Add a comment

Comments (3)

  1. shuhasJanuary 16, 2012 Reply
    >nice very informative. check more like this
  2. yogesh mahajanJanuary 28, 2012 Reply
    >very good
    thank you
  3. AnonymousFebruary 9, 2012 Reply
    >nyc work and coding….

Add a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.



Connect with Me

GPO 8973 NPC 541; Kathmandu Nepal
Phone: +977 9851236800

Calendar

April 2024
S M T W T F S
 123456
78910111213
14151617181920
21222324252627
282930