OOPS Lecture 1 ; Friend Methods
What is a friend method?
In C++,friend methodsallow member methods of otherclasses to access
the private and protected members of a class. This is a useful feature when
a specific function or class needs access to private data without being a
member of the class itself.
Syntax:
o declare a function as a friend of a class, the friend keyword is used. This
T
keyword is placed inside the class definition, preceding the function
prototype. The function is then allowed to access the private and protected
members of that class.
class
ClassName
{
private:
int
privateData
;
public:
friend
returnType
functionName
(
parameters
);
//
Friend function declaration
};
Properties
Non-Member Function:
friend function is not a member of the class but can access its private
A
and protected members. It is simply a function declared outside the class.
Access Control:
lthough a friend function is not a member, it has access to all the private
A
and protected members of the class. This breaks encapsulation to some
xtent, but it is used when necessary for specific purposes like operator
e
overloading or utility functions.
One-Way Friendship:
riendship is not mutual. If class A declares a function f as a friend, it
F
doesn’t mean f is a friend of class A. Another class B would need to
explicitly declare f as a friend for it to access B’s private data.
Not Inherited:
riendship is not inherited. If a class A is a friend of class B, and C inherits
F
from B, class C does not automatically have access to A’s private
members.
Defined Outside the Class:
hile the function is declared as a friend inside the class, its definition
W
usually occurs outside the class. This keeps the class definition clean while
still enabling external functions to access its private members.
ifference between friend function and friend function in
D
class.
Aspect Friend Function riend Function in a
F
Class
Position Aglobal(non-member) function. member functionof
A
another class.
Declaration eclared as a friend inside a class,
D eclared as a friend inside
D
but defined outside the class. one class and definedas a
memberof another class.
Scope Can access private and protected Can access private and
embers of the class where it’s
m rotected members of the
p
declared as a friend. class where it’s declared
as a friend but is a member
of another class.
riendship
F riendship is granted explicitly to a
F riendship is granted from
F
Source global function. one class to a function that
belongs toanother class.
Example global function like
A member function like
A
printLength(Box b) accessing addComplex in the
private members of the Box class. Calculator class accessing
private members of the
Complex class.
ccess to Class
A ccesses the class’s private and
A member of one class can
A
Members protected members without being a access private members of
member of the class. the class it’s a friend of.
efinition
D Defined outside the class. efined as a member
D
Location function of another class.
Use Case sed when a function, not part of
U sed when a function, part
U
any class, needs access to private of another class, needs to
members of a class. access private members of
a class.
Code:
#include
<iostream>
using
namespace
std
;
class
complex
; // forward declaration
class
calculator
{
public:
complex
sumComplex
(
complex
,
complex
);
};
class
complex
{
private:
int
real
,
imag
;
public:
friend
complex
calculator
::
sumComplex
(
c
omplex
o1,
complex
o2);
complex
() {}
complex
(
int
r,
int
i)
{
real
=
r;
imag
=
i;
}
void
display
()
{
cout
<<
real
<<
" + "
<<
imag
<<
"i "
<<
endl
;
}
};
complex
calculator
::
sumComplex
(
complex
o1,
complex
o2)
{
complex
o3
;
o3
.
real
=
o1
.
r
eal
+
o
2
.
r
eal
;
o3
.
imag
=
o1
.
i
mag
+
o
2
.
i
mag
;
o3
.
display
();
return
o3
;
}
int
main
()
{
complex
c1
(
1
,
2),
c2
(
1
,
2
)
;
calculator
c;
c
.
sumComplex
(
c1
,
c
2
);
return
0
;
}