WAP to swap private data member of two classes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 | #include<iostream>
using namespace std;
class B;
class A
{
int a;
public:
void getdata(int t)
{
a=t;
}
void showdata()
{
cout<<"A:a = "<<a<<endl;
}
friend void swapn(A&, B&);
};
class B
{
int b;
public:
void getdata(int t)
{
b=t;
}
void showdata()
{
cout<<"B:b = "<<b<<endl;
}
friend void swapn(A&, B&);
};
void swapn(A& x, B& y)
{
int temp;
temp=x.a;
x.a = y.b;
y.b = temp;
}
int main()
{
A x;
B y;
x.getdata(5);
y.getdata(7);
x.showdata();
y.showdata();
swapn(x, y);
x.showdata();
y.showdata();
} |
OUTPUT
A:a = 5
B:b = 7
A:a = 7
B:b = 5
Tags:
C Plus