KEMBAR78
Friend Methods in Class | PDF | Information Technology | Models Of Computation
0% found this document useful (0 votes)
14 views5 pages

Friend Methods in Class

Friend methods in C++ allow non-member functions to access private and protected members of a class, which is useful for specific functionalities like operator overloading. Friendship is one-way, meaning if class A grants access to a function, that function does not automatically have access to class A's members from other classes. The document also compares friend functions with member functions of another class, highlighting their differences in scope, declaration, and use cases.

Uploaded by

Hasham Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Friend Methods in Class

Friend methods in C++ allow non-member functions to access private and protected members of a class, which is useful for specific functionalities like operator overloading. Friendship is one-way, meaning if class A grants access to a function, that function does not automatically have access to class A's members from other classes. The document also compares friend functions with member functions of another class, highlighting their differences in scope, declaration, and use cases.

Uploaded by

Hasham Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

‭OOPS Lecture 1 ; Friend Methods‬

‭What is a friend method?‬


I‭n C++,‬‭friend methods‬‭allow member methods of other‬‭classes 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‬ ‭A‬‭global‬‭(non-member) function.‬ ‭ ‬‭member function‬‭of‬


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 defined‬‭as a‬
‭member‬‭of 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 to‬‭another 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‬
‭ (‭
‭i‬nt‬‭
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‬
‭ =‭
‭o‬1‬
.‭
‭ r
‬eal‬
+‭
‭ o
‬2‬
.‭
‭ r
‬eal‬
;‬

o3‬
‭ .‬
‭ imag‬
‭ =‭
‭o‬1‬
.‭
‭ i
‬mag‬
+‭
‭ o
‬2‬
.‭
‭ i
‬mag‬
;‬

o3‬
‭ .‬
‭ display‬
‭ ();‬

return‬‭
‭ o3‬
;‬

}‬

int‬‭
‭ main‬
()‬

{‬

complex‬‭
‭ c1‬
(‭
‭ 1
‬‬,‬‭
‭ 2‬),‬
‭ c2‬
‭ (‭
‭ 1
‬‬,‭
‭2‬‭
)
‬;‬
calculator‬‭
‭ c‬;‬

c‬
‭ .‭
‭s‬umComplex‬
(‭
‭c‬1‬
,‭
‭ c
‬2‬
);‬

return‬‭
‭ 0‭
;‬‬
}‬

You might also like