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.
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
#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;
}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), notb.volume(). - It can access the class's
privateandprotectedmembers through an object. - The declaration can go in the
publicorprivatesection; it makes no difference.
A friend of two classes
A classic use: one function needs the private data of two classes.
#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;
}Dollar account is bigger
Friend class
A whole class can be a friend: all its member functions get access.
#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;
}before: 120 hp after: 135 hp
Friendship rules
- Not mutual: if A declares B a friend, B can access A, but A cannot access B.
- Not inherited: a friend of the base class is not automatically a friend of derived classes.
- Not transitive: a friend of a friend is not a friend.
Test yourself
4 questions · pick an answer to check it instantly
- 1
How is a friend function
fof classAcalled?A friend is not a member, so it is called like an ordinary function. - 2
If class A declares class B as a friend, which is true?
Friendship is one-way and not inherited. - 3
Does a friend function have a
thispointer?It is not a member function, so there is no current object. - 4
Why does the two-class example need
class Rupees;beforeDollars?A forward declaration tells the compiler the name is a class.