You are on page 1of 5

Friend Function

Friend Function
• If a function is defined as a friend function then, the private
and protected data of a class can be accessed using the
function.
• For accessing the data, the declaration of a friend function
should be made inside the body of the class (can be anywhere
inside class either in private or public section) starting with
keyword friend.
Characteristics
1. A friend function is not in the scope of the class, in which it has been declared
as friend.
2. It cannot be called using the object of that class.
3. It can be invoked like a normal function without any object.
4. Unlike member functions, it cannot use the member names directly.
5. It can be declared in public or private part without affecting its meaning.
6. Usually, it has objects as arguments.
7. Perhaps the most common use of friend functions is overloading << and >>
for I/O
Examples

1. WAP to swap two numbers using temporary variable and friend function.

2. WAP to find maximum of two numbers using friend function.

You might also like