---
title: "Member function as friend of another class"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:member-function-friend-of-other-class"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/member-function-friend-of-other-class
site: BCT Engineering
---

# Member function as friend of another class

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2074 Chaitra, 6 marks
- Video: [How To Make Member Function Of One Class As Friend Of Another Class In C++](https://www.youtube.com/watch?v=VQxEyukjYfc) — CppNuts
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/member-function-friend-of-other-class
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

Explain how any member function of a class can be friend of other class with a suitable example.

## Short answer

To make a member function a friend, forward-declare the second class, define the first class with the function declaration only, then in the second class declare it as friend using scope resolution, e.g., friend int First::func(Second&);. Finally, define the function after both classes to access private members.

## Answer

A single member function of one class can be made a friend of another class instead of making the whole class a friend. In the second class we declare it as:

```cpp
friend return_type FirstClass::function(args);
```

This gives only that function access to the private members of the second class.

#### Steps

1. Forward-declare the second class (`class B;`) because the first class's function uses it as a parameter.
2. Define the first class `A` with the function **declared** only.
3. Define class `B` with the friend declaration using `A::function`.
4. Define `A::function` **after** `B` is complete, since it needs to access B's members.

#### Example

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

class B;                              // forward declaration

class A
{
    int a;
public:
    A(int v = 0) { a = v; }
    int sum(B &b);                    // declared only
};

class B
{
    int b;
public:
    B(int v = 0) { b = v; }
    friend int A::sum(B &);           // only A::sum is a friend
};

int A::sum(B &obj)                    // defined after B
{
    return a + obj.b;                 // accesses private b of B
}

int main()
{
    A x(10);
    B y(5);
    cout << "Sum = " << x.sum(y) << endl;   // 15
    return 0;
}
```

Only `A::sum()` can access `b`. Other members of `A` cannot, which is the difference from a friend class. Friendship is granted by `B`, not taken by `A`.

## Check yourself

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

1. What is the difference from a friend class?
   A. Only that one member function gets access to private members
   B. All members of A get access
   C. B gets access to A
   D. No difference
   Answer: A. Declaring friend int A::sum(B&) gives only that function access. (Key point: Single member friend)

2. Which declaration in class B makes A::sum a friend?
   A. friend class A;
   B. friend int A::sum(B &);
   C. friend int sum(B &);
   D. A::sum friend;
   Answer: B. Use friend return_type FirstClass::function(args); (Key point: Friend declaration syntax)

3. When should A::sum be defined?
   A. Before class B
   B. After B is complete
   C. Inside main()
   D. Inside B
   Answer: B. It accesses B's members, so B must be fully defined first. (Key point: Order of definition)

4. Who grants the friendship?
   A. A takes it itself
   B. B grants it
   C. main() grants it
   D. The compiler
   Answer: B. Friendship is granted by the class whose data is accessed, B, not taken by A. (Key point: Who grants friendship)

## Related in Object Oriented Programming

- [Friend class and encapsulation](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-class-encapsulation) — Object Oriented Programming, chapter 4
- [Need of friend function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-function-need) — Object Oriented Programming, chapter 4, asked 2×
- [Static data member and member function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-member-function) — Object Oriented Programming, chapter 4, asked 3×
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — Object Oriented Programming, chapter 5
- [Overloading via member and non-member function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-vs-nonmember-overload) — Object Oriented Programming, chapter 5
- [Member function overriding with virtual function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-member-override-reason) — Object Oriented Programming, chapter 7

## Related in other subjects

- [Insert a string into another string](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Ainsert-string-into-string) — Computer Programming, chapter 7
- [Euler's formula: one end fixed, other hinged](https://www.bctengineer.com/question.md?key=semester_3%2Fstrength_of_materials%3Aeuler-one-fixed-one-hinged) — Strength of Materials, chapter 8, asked 2×
- [Event management system summary and class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aoems-executive-summary-class-diagram) — Object Oriented Analysis and Design, chapter 2
- [Unequal angle tension member with HSFG bolts](https://www.bctengineer.com/question.md?key=semester_6%2Fdesign_of_steel_and_timber_structure%3Atension-unequal-angle-hsfg-200kn) — Design of Steel and Timber Structure, chapter 5
