-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10-Friend Function
44 lines (36 loc) · 1 KB
/
10-Friend Function
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
> Friend Function is not a member function of a class to which it is a friend
> Friend function is declared in the class with friend keyword
> It must be defined outside the class to which it is friend
> Friend function can access any member of the class to which it is friend
> Friend Function can't access members of the class directly
> It has no caller object. c1.fun();
> It should not be defined with membership label. (Complex::fun()).
> A friend function is a function that is declared outside a class, but is capable of accessing the private and protected members of class
#include <bits/stdc++.h>
using namespace std;
class Complex
{
int a, b;
public:
void setData(int a, int b)
{
(*this).a = a;
(*this).b = b;
}
void showData()
{
printf("a = %d b = %d \n", a, b);
}
friend void sum(Complex);
};
void sum(Complex c)
{
cout << "Sum is " << c.a + c.b << endl;
}
int main()
{
Complex c1;
c1.setData(2, 4);
c1.showData();
sum(c1);
}