---
title: "Need of friend function"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:friend-function-need"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/friend-function-need
site: BCT Engineering
---

# Need of friend function

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2074 Chaitra, 2 marks; 2070 Asar, 4 marks (asked 2 times)
- Video: [Lec-44: Friend Function/Class in C++ Explained with Examples | OOPs Concepts](https://www.youtube.com/watch?v=lcrZvDoYaj8) — Gate Smashers
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/friend-function-need
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

Why do we need friend function? Explain with example.

## Short answer

A friend function is a non-member function granted access to private members, declared with friend keyword. Needed to access private data of two or more classes in one function, or overload operators where left operand is not an object (e.g., cout << obj). It takes objects as arguments, not called through object.

## Answer

A **friend function** is a non-member function that is allowed to access the private and protected members of a class. It is declared inside the class with the keyword `friend`, but it is defined outside like an ordinary function, without the scope operator and without being called through an object.

#### Why it is needed

1. To operate on private data of **two or more different classes** with one function (e.g. add or compare values from two classes).
2. To overload operators when the left operand is not an object of the class, e.g. `cout << obj` or `5 + obj`.
3. To let a function outside the class use private data when making it a member is not suitable.

#### Characteristics

- Not in the scope of the class and not called with `obj.f()`.
- Takes the objects as arguments and uses `obj.member` to access data.
- Has no `this` pointer. Can be declared in the public or private part.

#### Example

```cpp
#include <iostream>
using namespace std;

class B;                          // forward declaration

class A
{
    int x;
public:
    A(int v) { x = v; }
    friend int maxValue(A, B);
};
class B
{
    int y;
public:
    B(int v) { y = v; }
    friend int maxValue(A, B);
};

int maxValue(A a, B b)            // friend of both classes
{
    return (a.x > b.y) ? a.x : b.y;
}

int main()
{
    A a(10);
    B b(25);
    cout << "Larger = " << maxValue(a, b) << endl;   // 25
    return 0;
}
```

The function `maxValue()` reads private `x` of `A` and private `y` of `B`, which would not be possible for an ordinary function.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is a friend function?
   A. A member function that is private
   B. A non-member function allowed to access private and protected members
   C. A function that inherits the class
   D. A function with a this pointer
   Answer: B. A friend is a non-member function the class grants access to its private and protected data. (Key point: Friend definition)

2. Why is a friend useful for adding values from two different classes?
   A. One function can access private data of both classes
   B. It makes the data public
   C. It avoids constructors
   D. It merges both classes
   Answer: A. A single friend of both classes can read the private data of each. (Key point: Two classes)

3. When is a friend needed for operator overloading?
   A. When the left operand is not an object of the class, e.g. cout << obj
   B. When the operator is unary
   C. When data is public
   D. When using static members
   Answer: A. A member operator needs the class object on the left; for cout << obj or 5 + obj a friend is needed. (Key point: Operator left operand)

4. Which is true of a friend function?
   A. Called as obj.f()
   B. Defined with the scope operator
   C. Has no this pointer and takes objects as arguments
   D. Always declared in the private section only
   Answer: C. It is an ordinary function, so it has no this pointer and receives objects as arguments. (Key point: Friend characteristics)

## Related in Object Oriented Programming

- [Member function as friend of another class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-function-friend-of-other-class) — Object Oriented Programming, chapter 4
- [Need of virtual function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-function-need) — Object Oriented Programming, chapter 7, asked 4×
- [Need of template; function template overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Awhy-template-function-overloading) — Object Oriented Programming, chapter 9
- [Friend class and encapsulation](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-class-encapsulation) — Object Oriented Programming, chapter 4
- [mdistance and edistance addition via friend](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amdistance-edistance-friend) — Object Oriented Programming, chapter 4
- [Operator overloading: need and definition](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-why-define) — Object Oriented Programming, chapter 5, asked 3×

## Related in other subjects

- [Why do we need DFT](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-need-dft) — Digital Signal Analysis and Processing, chapter 7, asked 6×
- [Why do we need FFT](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-need-fft) — Digital Signal Analysis and Processing, chapter 7, asked 2×
- [Need for difference equation; LCCDE and system function](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-difference-equation-lccde) — Digital Signal Analysis and Processing, chapter 1, asked 2×
- [Firewall, its need and types](https://www.bctengineer.com/question.md?key=semester_7%2Fcomputer_networks_and_security%3Afirewall-types-working) — Computer Networks and Security, chapter 8, asked 7×
