---
title: "Friend class and encapsulation"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:friend-class-encapsulation"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/friend-class-encapsulation
site: BCT Engineering
---

# Friend class and encapsulation

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2068 Chaitra, 2+3 marks
- Video: [Friend functions and classes in C++ (Programming for beginners)](https://www.youtube.com/watch?v=KXDzBglp83M) — CodeBeauty
- 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-class-encapsulation
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

What do you mean by friend function and friend class? Do friends violate encapsulation?

## Short answer

A friend function is a non-member given access to private members by a class declaration with friend. A friend class grants all its member functions access to another class's private members. Friends loosen encapsulation but do not violate it because the class itself decides who is a friend; however, excessive use reduces data hiding.

## Answer

#### Friend function

A **friend function** is a non-member function that is given permission, by the class, to access its private and protected members. It is declared inside the class with the keyword `friend`.

```cpp
class Box
{
    int w;
public:
    Box(int x) { w = x; }
    friend void show(Box b);   // friend function
};
void show(Box b) { cout << b.w; }
```

#### Friend class

A **friend class** is a class all of whose member functions can access the private and protected members of another class. It is declared in the second class as `friend class A;`. Friendship is not mutual (if A is a friend of B, B is not automatically a friend of A) and not inherited.

#### Do friends violate encapsulation?

Not really, though they loosen it. Reasons:

- The class itself decides who is its friend, by writing the declaration inside the class. Outsiders cannot grant themselves access.
- Friends are listed in the class definition, so they are part of the class's public interface and are easy to find.
- Without friends we would have to make data public or add many get/set functions, which weakens encapsulation more.

However, a friend can read and change private data directly, so too many friends reduce data hiding and make the code harder to maintain. Friends should be used sparingly, only where closely related classes or operators need them.

## Check yourself

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

1. What can the member functions of a friend class do?
   A. Access private and protected members of the other class
   B. Only access public members
   C. Inherit its members
   D. Override its functions
   Answer: A. All member functions of a friend class can access the other class's private and protected members. (Key point: Friend class meaning)

2. If A is a friend of B, is B a friend of A?
   A. Yes always
   B. No, friendship is not mutual
   C. Yes if public
   D. Only if inherited
   Answer: B. Friendship is not mutual and not inherited. (Key point: Mutual friendship)

3. Why do friends do not really violate encapsulation?
   A. The class itself declares who its friends are
   B. Friends cannot access private data
   C. Friends are always member functions
   D. Friends are chosen by outsiders
   Answer: A. Access is granted inside the class definition; outsiders cannot grant themselves access. (Key point: Encapsulation argument)

4. What is the drawback of too many friends?
   A. They reduce data hiding and make maintenance harder
   B. They slow the compiler
   C. They remove constructors
   D. They make data public to everyone
   Answer: A. Friends can directly change private data, so overuse weakens data hiding. (Key point: Downside)

## 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 friend function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-function-need) — Object Oriented Programming, chapter 4, asked 2×
- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 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
- [Pure virtual function and abstract class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apure-virtual-abstract-class) — Object Oriented Programming, chapter 7, asked 5×
- [LandMeasure class and sum function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Alandmeasure-class-sum) — Object Oriented Programming, chapter 4

## Related in other subjects

- [Contractor friend's dinner and guesthouse offer](https://www.bctengineer.com/question.md?key=semester_8%2Fengineering_professional_practice%3Afriend-dinner-guesthouse-ethics) — Engineering Professional Practice, chapter 7
- [Consultant's friend supplies material; community objects](https://www.bctengineer.com/question.md?key=semester_8%2Fengineering_professional_practice%3Aconsultant-friend-supplier-quit) — Engineering Professional Practice, chapter 7
- [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
