Bytebook Compiler

Classes & Objects

Friend Functions & Classes

Grant selected functions or classes access to private members — when it is useful, how it works, and why to use it sparingly.

3 min read3 runnable examples4-question quiz

Private members are hidden from everything outside the class. Sometimes a function that is not a member genuinely needs that access — for example, a function that compares two objects of different classes, or an operator like << whose left side is cout. A class can declare such a function its friend.

Friend function

C++
#include <iostream>
using namespace std;

class Box {
    double length, width, height;
public:
    Box(double l, double w, double h) : length(l), width(w), height(h) {}
    friend double volume(const Box &b);        // grant access
};

double volume(const Box &b) {                  // NOT a member: no Box:: and no this
    return b.length * b.width * b.height;      // but it may read private members
}

int main() {
    Box b(2, 3, 4);
    cout << "Volume: " << volume(b) << endl;
    return 0;
}
Output
Volume: 24

Properties of a friend function:

  • It is declared inside the class with friend, but it is not a member — it is called like a normal function, volume(b), not b.volume().
  • It can access the class's private and protected members through an object.
  • The declaration can go in the public or private section; it makes no difference.

A friend of two classes

A classic use: one function needs the private data of two classes.

C++
#include <iostream>
using namespace std;

class Rupees;                                  // forward declaration

class Dollars {
    double amount;
public:
    Dollars(double a) : amount(a) {}
    friend bool richer(const Dollars &d, const Rupees &r);
};

class Rupees {
    double amount;
public:
    Rupees(double a) : amount(a) {}
    friend bool richer(const Dollars &d, const Rupees &r);
};

bool richer(const Dollars &d, const Rupees &r) {
    const double rate = 84.0;                  // rupees per dollar
    return d.amount * rate > r.amount;
}

int main() {
    Dollars savingsUS(150);
    Rupees savingsIN(11000);
    cout << (richer(savingsUS, savingsIN) ? "Dollar account is bigger" : "Rupee account is bigger") << endl;
    return 0;
}
Output
Dollar account is bigger

Friend class

A whole class can be a friend: all its member functions get access.

C++
#include <iostream>
using namespace std;

class Engine {
    int horsepower = 120;
    friend class Mechanic;                     // Mechanic may see inside
};

class Mechanic {
public:
    void tune(Engine &e) { e.horsepower += 15; }
    int inspect(const Engine &e) { return e.horsepower; }
};

int main() {
    Engine e;
    Mechanic m;
    cout << "before: " << m.inspect(e) << " hp" << endl;
    m.tune(e);
    cout << "after:  " << m.inspect(e) << " hp" << endl;
    return 0;
}
Output
before: 120 hp
after:  135 hp

Friendship rules

  1. Not mutual: if A declares B a friend, B can access A, but A cannot access B.
  2. Not inherited: a friend of the base class is not automatically a friend of derived classes.
  3. Not transitive: a friend of a friend is not a friend.

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    How is a friend function f of class A called?

  2. 2

    If class A declares class B as a friend, which is true?

  3. 3

    Does a friend function have a this pointer?

  4. 4

    Why does the two-class example need class Rupees; before Dollars?