[go: up one dir, main page]

0% found this document useful (0 votes)
25 views2 pages

Friend Function

A friend function in C++ can access the private and protected members of a class. A friend function is declared inside a class using the friend keyword. The function can then access private data without using getter/setter functions, but must be called normally without an object.

Uploaded by

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

Friend Function

A friend function in C++ can access the private and protected members of a class. A friend function is declared inside a class using the friend keyword. The function can then access private data without using getter/setter functions, but must be called normally without an object.

Uploaded by

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

C++ Friend function

 If a function is defined as a friend function in C+


+, then the protected and private data of a class
can be accessed using the function.
 By using the keyword friend compiler knows the
given function is a friend function.
 For accessing the data, the declaration of a
friend function should be done inside the body
of a class starting with the keyword friend.
Syntax of friend function in C++

1.class class_name    
2.{    
3.    friend return_type function_name(argument/s);     
       // syntax of friend function.  
4.};    
In the above declaration, the friend function is
preceded by the keyword friend. The function can be
defined anywhere in the program like a normal C++
function. The function definition does not use either
the keyword friend or scope resolution operator.
Characteristics of a Friend function:
o The function is not in the scope of the class to
which it has been declared as a friend.
o It cannot be called using the object as it is not in
the scope of that class.
o It can be invoked like a normal function without
using the object.
o It cannot access the member names directly and
has to use an object name and dot membership
operator with the member name.
o It can be declared either in the private or the
public part.

You might also like