100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Friend Class in C++?

Understand C++ friend classes: explicit, one-directional access to private members, real use cases, and how it compares to Java.

mediumQ208 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A friend class in C++ is a class explicitly granted access to another class’s private and protected members, declared using the `friend` keyword inside the class that is granting the access.

Friendship is granted, never taken — class A cannot force access into class B; B must declare `friend class A;` itself. It is also non-mutual and non-transitive: if B befriends A, A does not automatically befriend B, and if B befriends A and A befriends C, C still cannot access B’s private members. Common uses include tightly coupled helper classes (like an iterator class needing access to its container’s internals) or operator-overload implementations that need direct access to private state on both sides. Overusing friendship weakens encapsulation, so it is reserved for genuinely tight collaborations rather than general convenience.

  • Enables tightly coupled classes to share internals without exposing them publicly
  • Keeps most members private while allowing a specific, controlled exception
  • Useful for iterator or builder classes that must reach into a container
  • Access is explicit and auditable — declared once, in one place

AI Mentor Explanation

A team’s medical department normally keeps a player’s injury file completely private, but the team explicitly grants the head coach’s office friend access to that file because their roles are tightly coupled around selection decisions. The medical department chose to grant this, and no other department automatically gets the same access just because the coach’s office has it. A C++ friend class works identically: one class explicitly grants another specific access to its private data, and that grant is one-directional and does not cascade.

Step-by-Step Explanation

  1. Step 1

    Identify the tight coupling

    Confirm two classes genuinely need to share private internals (e.g. a container and its iterator).

  2. Step 2

    Declare friendship in the owning class

    Inside the class holding the private data, write `friend class OtherClass;`.

  3. Step 3

    Access granted one-directionally

    OtherClass can now reach the private/protected members; the reverse is not automatic.

  4. Step 4

    Use sparingly

    Prefer public methods where possible; reserve friendship for genuinely tight collaborations.

What Interviewer Expects

  • Correct definition: explicit, one-directional grant of private/protected access
  • Understanding that friendship is not inherited and not transitive
  • A realistic use case (operator overloading, iterator-container pairing)
  • Awareness that overusing friend classes weakens encapsulation

Common Mistakes

  • Assuming friendship is mutual once declared on one side
  • Assuming friendship is inherited by subclasses of the friend class
  • Believing friend classes exist in Java (Java has no friend keyword)
  • Using friend classes as a general workaround instead of proper public APIs

Best Answer (HR Friendly)

A friend class in C++ is a class that another class explicitly trusts with access to its private and protected members. The class holding the private data has to declare the friendship itself, it only goes one way, and it doesn’t pass down to subclasses. It’s typically used for tightly coupled pairs of classes, like a container and its iterator, where full encapsulation would force awkward public workarounds.

Code Example

Java analogy: package-private access as the closest equivalent
// Java has no friend keyword. The nearest equivalent is package-private
// (default) access, which grants trusted classes in the same package
// direct access without a full public API.

class Account {
    private double balance; // hidden from other packages

    Account(double balance) { this.balance = balance; } // package-private constructor

    // Package-private accessor: only classes in this package can call it,
    // similar in spirit to a C++ friend class being explicitly trusted.
    double internalBalance() {
        return balance;
    }
}

class AccountAuditor {
    double audit(Account account) {
        return account.internalBalance(); // works: same package
    }
}

Follow-up Questions

  • Is friendship in C++ inherited by a friend class’s subclasses?
  • Can a single function (not a whole class) be declared a friend?
  • How does a friend class differ from Java’s package-private access?
  • Why is friendship considered a controlled violation of encapsulation rather than a full one?

MCQ Practice

1. Who declares the `friend class` relationship in C++?

The class granting access declares `friend class X;` inside itself; the grant is not made by the requesting class.

2. If class B is a friend of class A, is class A automatically a friend of class B?

Friendship is not mutual — A must separately declare B as a friend, and vice versa, for two-way access.

3. Does Java have a direct equivalent of C++’s friend class keyword?

Java has no friend keyword; package-private access is the nearest, though less explicit, mechanism for trusted access.

Flash Cards

Friend class in one line?A class explicitly granted access to another class’s private/protected members in C++.

Is friendship mutual?No — it must be declared separately by each class to go both ways.

Is friendship inherited?No — a friend class’s subclasses do not automatically gain the friendship.

Closest Java equivalent?Package-private (default) access, though it is implicit rather than explicitly named.

1 / 4

Continue Learning