Javatpoint Logo

91-9990449935

 0120-4256464

Javatpoint Logo

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.


Declaration of friend function in C++


C++ friend function Example

Let's see the simple example of C++ friend function used to print the length of a box.

Output:

Length of box: 10  
Next TopicC++ Inheritance




Latest 4 Tutorials on JavaTpoint