CodeBlanks Logo Icon CodeBlanks

friend

Learn external access permission with friend.

friend

Sometimes an external function or another class needs to access a class's private members. You do this with the friend keyword. A function declared as friend can access private members without being a member of the class.

Example: class Account { private: int balance; public: friend void printBalance(const Account&); };. Here printBalance is declared a friend function. Its body is written outside the class: void printBalance(const Account& a) { std::cout << a.balance; }. This function can access balance, because it was declared a friend. friend should not be overused; it breaks encapsulation.

When to use friend?

friend is used carefully, because it weakens encapsulation. But some situations make it practical. The most common use is operator overloading, especially when the left-hand side is not the class.

Example: the function std::ostream& operator<<(std::ostream& os, const Account& a) writes an Account to a stream. Because the left-hand side is std::ostream, this cannot be a member function; it must be a friend. Another use: two classes that need to work closely with each other's internal state (for example, a tree and a node). Still, prefer a public interface over friend whenever possible.

Practice Add a friend function to the Account class

Define an Account class. Under private: add int balance;. Under public: add the constructor Account(int b) { balance = b; } and the friend declaration friend void show(const Account&);. Outside the class write void show(const Account& a) { std::cout << a.balance; }. In main create Account acc(100); and call show(acc).

Starter code

main.cpp
#include <iostream>

// Define the Account class and the show friend function here

int main() {
    // Create an Account instance and call show
    return 0;
}

Sample solution

main.cpp
#include <iostream>

class Account {
    int balance;
public:
    Account(int b) {
        balance = b;
    }
    friend void show(const Account&);
};

void show(const Account& a) {
    std::cout << a.balance;
}

int main() {
    Account acc(100);
    show(acc);
    return 0;
}

Expected output

100

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each piece to its role

Term Meaning
friend void print() Friend declaration
printBalance(a) Friend call
a.balance Private member access
const Account& Reference parameter

2 What can a function declared as friend do?

main.cpp
friend void printBalance(const Account&);
  • Access public members of the class
  • Access private members of the class Correct answer
  • Access only static members
  • Inherit from the class

Explanation: A friend function can access private members.

3 Write the friend keyword

Question
public: ____ void printBalance(const Account&);
Answer
public: friend void printBalance(const Account&);

Explanation: friend grants the function private access.

All lessons in Intermediate