0% found this document useful (0 votes)
2 views

Friend Functions

A friend function in C++ is a non-member function that can access private and protected members of a class, declared using the keyword 'friend'. It allows for operations on class members without needing an object, although it breaks encapsulation principles. The document includes a code example demonstrating the use of a friend function to sum complex numbers.

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)
2 views

Friend Functions

A friend function in C++ is a non-member function that can access private and protected members of a class, declared using the keyword 'friend'. It allows for operations on class members without needing an object, although it breaks encapsulation principles. The document includes a code example demonstrating the use of a friend function to sum complex numbers.

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/ 3

‭OOPS Lecture 1 ; Friend Functions‬

‭What is a friend function ?‬


‭ ‬‭friend function‬‭is a special type of function in‬‭C++ that can access‬
A
‭private and protected members of a class, even though it is‬‭not a member‬
‭of the class‬‭. It is declared using the keyword friend‬‭inside the class‬
‭definition.‬

‭Syntax:‬
friend‬‭
‭ ReturnType‬‭
FunctionName‬
(‭
‭P
‬arameters‬
);‬

‭Purpose:‬
‭ friend function is used when a non-member function needs access to the‬
A
‭private or protected members of a class.‬

‭Properties:‬
‭Friend functions are not members of the class.‬

‭They are declared inside the class but defined outside the class.‬

‭ friend function has access to all members (private, protected, public) of‬
A
‭the class.‬

‭Called/used without using objects , e.g‬

complex‬‭
‭ c3‬‭
=‬‭
sumComplex‬
(‬
‭ c1‬
‭ ,‬
‭ c2‬
‭ );‬

‭This function is invoked directly without using any object , e.g‬

c3.‬
‭ sumComplex‬
‭ (‭
‭ c
‬1‬
,‭
‭c‬2‬
)// wrong‬

‭Can’t Access‬ ‭
datamembers‬‭directly. Requires an object‬‭and‬
‭access them by‬‭
objectname‬
.‭
‭d‬atamember‬
‭Limitations‬‭:‬
‭•‬ ‭ riend functions break the encapsulation principle by allowing‬
F
‭external functions to access private members.‬

‭Code‬‭:‬
#include‬‭
‭ <iostream>‬
using‬‭
‭ namespace‬‭
std‬
;‬

class‬‭
‭ complex‬
{‬

private:‬

int‬‭
‭ real‬
;‬

int‬‭
‭ imag‬
;‬

public:‬

complex‬
‭ () {} // Default Constructor‬

complex‬
‭ (‭
‭i‬nt‬‭
real,‬‭
int‬‭
imag)‬
{‬

this‬
‭ ->‬
‭ real‬‭
‭ =‬‭
real;‬
this‬
‭ ->‬
‭ imag‬‭
‭ =‬‭
imag;‬
}‬

void‬‭
‭ display‬
()‬

{‬

cout<<real<<‬
‭ " + "‬
‭ <<imag<<‬
‭ "i "‬
‭ <<endl‬
‭ ;‬

}‬

friend‬‭
‭ complex‬‭
sumComplex‬
(‬
‭complex‬‭
‭ o1,‬‭
complex‬‭
o2);‬
};‬

complex‬‭
‭ sumComplex‬
(‭
‭c‬omplex‬‭
o1,‬
complex‬‭
‭ o2)‬
{‬

complex‬‭
‭ temp‬
;‬

temp‬
‭ .‬
‭ real‬‭
‭ =‬‭
o1‬
.‬
‭ real‬‭
‭ +‬‭
o2‬
.‬
‭ real‬
‭ ;‬

temp‬
‭ .‬
‭ imag‬‭
‭ =‬‭
o1‬
.‬
‭ imag‬‭
‭ +‬‭
o2‬
.‬
‭ imag‬
‭ ;‬

return‬‭
‭ temp‬
;‬

}‬

int‬‭
‭ main‬
()‬

{‬

complex‬‭
‭ c1‬
(‭
‭ 3
‬‬,‬‭
‭ 4‬);‬

c1‬
‭ .‬
‭ display‬
‭ ();‬

complex‬‭
‭ c2‬
(‭
‭ 1
‬‬,‭
‭2‬‭
)
‬;‬
c2‬
‭ .‬
‭ display‬
‭ ();‬

complex‬‭
‭ c3‬‭
=‬‭
sumComplex‬
(‭
‭c‬1‬
,‭
‭ c
‬2‬
);‬

cout<<‬
‭ "The sum of Complex numbers is = "‬
‭ ;‬

c3‬
‭ .‬
‭ display‬
‭ ();‬

return‬‭
‭ 0‭
;‬‬
}‬

You might also like