-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28moreff_fc.cpp
More file actions
62 lines (56 loc) · 1.21 KB
/
Copy path28moreff_fc.cpp
File metadata and controls
62 lines (56 loc) · 1.21 KB
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
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
using namespace std;
class Y;
// forward declaration is important if a class is passed as an argument before it is even created
class X
{
int data;
friend void swap(X &, Y &); //pass by reference
friend void display(X , Y);
public:
void setValue(int value)
{
data = value;
}
friend void add(X, Y); // here, in a friend function Y is declared as an argument but Y do not exist yet
};
class Y
{
int data;
friend void swap(X &, Y &);
friend void display(X , Y);
public:
void setValue(int value)
{
data = value;
}
friend void add(X, Y); // since Y has been declared, here there will be no error accessing data of Y
};
void add(X o1, Y o2)
{
cout << "Sum of the data of X and Y is " << o1.data + o2.data;
}
void swap(X &o1, Y &o2) //pass by reference
{
int temp = o1.data;
o1.data = o2.data;
o2.data = temp;
}
void display(X o1, Y o2)
{
cout<<endl<<"a = "<<o1.data<<" b = "<<o2.data<<endl;
}
int main()
{
X a;
Y b;
a.setValue(6);
b.setValue(3);
add(a, b);
cout<<endl<<"Before swap : "<<endl;
display(a, b);
swap(a, b);
cout<<endl<<"After swap : "<<endl;
display(a, b);
return 0;
}