-
Notifications
You must be signed in to change notification settings - Fork 0
/
ollie138_OOPs_Concepts-5.cpp
120 lines (86 loc) · 3.28 KB
/
ollie138_OOPs_Concepts-5.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<bits/stdc++.h>
using namespace std;
/*
NOTES:
(1) Friend function is not a member function of the class to which it is a friend.
(2) Friend function is declared inside the class with a friend keyword, but defined outside the class.
(3) Friend function can access any member of the class to which it is a friend.
(4) Friend function cannot accesss members of the class directly.
(5) Since friend function is not a member of the class to which it is a friend, therefore,
it does not have any caller object. This means that a friend function cannot be called with
the help of an object of the class to which it is a friend. Using objects, we can only
access the member functions of the class, not non-member functions.
(6) Friend functions are defined without a membership label outside the class. Only member
functions are defined with a membership label outside the class and friend function is not
a member function.
(7) Every object of a class has its own set of instance member variables and
instance member functions. Function belonging to which object of the class has to be
executed depends on which object of that class is calling that function
i.e. it depends on the caller object. But, since a friend function is not an instance
member function, it is common for all the objects of that class i.e. the same friend
function is shared by all the objects of that class. Since, a friend function does not have
a caller object, it is not directly clear as to which object is calling the friend function.
Inorder to solve this problem of non-determinism, we pass the object as an argument to the
friend function.
(8) Friend function can be declared as private , public or protected. Access modifiers do not
have any affect on the accessibility of the friend function because friend function is not
a member function of the class. Access modifiers can only affect the accessibility of the
member functions not non-member functions.
(9) A Friend function can be a friend to many classes.
(10) Friend function can be used to access the private or protected members of two or more
classes simultaneously to which it is a friend. This is not possible with any member
function.
*/
class Myclass
{
private:
int a , b;
public:
// Default constructor
Myclass()
{
cout << "Hi !!! I am a default constructor !!!" << endl;
}
// Parameterized constructor
Myclass(int x , int y)
{
cout << "Hi !!! I am a parameterized constructor !!!" << endl;
this -> a = x;
this -> b = y;
}
void set_data(int x , int y)
{
a = x;
b = y;
}
void show_data()
{
cout << "a: " << a << endl;
cout << "b: " << b << endl;
}
// Declaration of friend function fun1
friend void fun1();
// Declaration of friend function fun2
friend void fun2(Myclass);
};
void fun1()
{
cout << "Hi !!! Iam a friend function of Myclass !!!" << endl;
}
void fun2(Myclass c)
{
cout << "Sum: " << c.a + c.b << endl;
}
int main()
{
cout << endl;
Myclass c1 , c2(2 , 3) , c3(4 , 5);
cout << endl;
c1.set_data(10 , 20);
cout << endl;
fun1();
cout << endl;
fun2(c1);
cout << endl;
return 0;
}