-
Notifications
You must be signed in to change notification settings - Fork 0
/
H-Friendly_Function.cpp
58 lines (51 loc) · 996 Bytes
/
H-Friendly_Function.cpp
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
/*
FRIEND FUNCTION:
A non member function is not alloed to access private data members
BUT IT IS POSSIBLE USING FRIEND FUNCTION
Friend function is not in the scope of the class , so it can be invoked like normal
function
*/
#include <iostream>
#include <conio.h>
using namespace std;
class B; // forward declaration of class B
class A
{
int value;
public:
void getdata(int x)
{
value = x;
}
friend void sum(A, B); // declaring friend function
};
class B
{
int value;
public:
void getdata(int y)
{
value = y;
}
friend void sum(A, B); // declaring friend function
};
void sum(A a, B b) // friend function definition
{
cout << "sum is " << a.value + b.value; // we can access the Private (Value) part in Friend Function with the help of Objects.
}
int main()
{
system("cls");
A a;
B b;
int x, y;
cout << "enter first number : " << endl;
cin >> x;
cout << "enter second number : " << endl;
cin >> y;
a.getdata(x);
b.getdata(y);
sum(a, b);
getch();
return 0;
}