Topic 11

Friend Functions & Friend Classes

3 min read3 questions to test yourself

1Definition

A Friend Function is a non-member function that is granted special permission to access the private and protected members of a class. A Friend Class is a class whose member functions can all access the private and protected members of another class.

2Simple Explanation

Normally, strict encapsulation prevents any outside function or external class from viewing private internal variables. Declaring a function or class as a Friend acts like issuing a VIP Access Pass, allowing that specific external entity to bypass normal private access barriers.

3Real-Life Example / Analogy

  • Personal Password & Trusted Friend: You keep your email password private from the general public. However, in an emergency, you give your trusted personal friend access to your password credentials.

4Diagram / Visual Representation

friend access bypasses private boundary

Class: Account
private: balance = 5000
friend void auditAccount(Account)

External function: auditAccount()
Can read private balance through object argument

5Working / How It Works

  1. Declare the external function inside the class using the friend keyword (e.g., friend void printData(ClassA obj);).
  2. The declaration can appear anywhere inside the class (private or public block).
  3. The actual function implementation is defined outside the class scope without needing the ClassA:: scope resolution prefix.

6Types / Components / Characteristics

  • Key Characteristics:
    • Non-Member Status: A friend function is not a member function of the class.
    • No this Pointer: It cannot access members directly by name; it must accept an object instance parameter (obj.privateMember).
    • Non-Transitive: If Class A is a friend of Class B, and Class B is a friend of Class C, Class A is not automatically a friend of Class C.
    • Non-Reciprocal: Friendship is granted, not taken. If A is a friend of B, B is not automatically a friend of A.

7Solved Examples / Problems

Problem Statement: Write a C++ program demonstrating a Friend Function calculating the product of private variables from a class.

C++
#include <iostream>
using namespace std;
class Numbers {
private:
    int numA, numB;
public:
    Numbers(int a, int b) : numA(a), numB(b) {}
    // Friend Function Declaration
    friend int calculateProduct(Numbers n);
};
// Friend Function Definition (outside class)
int calculateProduct(Numbers n) {
    // Accessing private members directly using object argument
    return n.numA * n.numB;
}
int main() {
    Numbers obj(4, 5);
    cout << "Calculated Product: " << calculateProduct(obj) << endl;
    return 0;
}

Output:

Output
Calculated Product: 20

8Advantages, Disadvantages & Applications

  • Advantages:
    • Allows efficient operator overloading for stream IO operators (<< and >>).
    • Facilitates tight coordination between two distinct classes without public exposure.
  • Disadvantages:
    • Weakens strict encapsulation and data hiding guarantees if overused.
  • Applications: Bridging complex math vector and matrix classes, custom IO stream formatters.

9Important Points to Remember

  • Friendship is neither inherited nor transitive.
  • Friend declarations are not controlled by access specifier locations (private: or public:).

10Test Your Understanding

  1. Conceptual Question: Is friendship inherited by a derived child class?
    Show answer
    No. Friendship is strictly non-inheritable.
  2. MCQ: Which keyword grants a non-member function access to private class attributes?
    • A) virtual
    • B) friend
    • C) inline
    • D) static
    Show answer
    B) friend
  3. Application Question: Why does friend void display(); fail when written as cout << num; inside the friend body?
    Show answer
    Friend functions lack a this pointer; they must accept an object parameter and access members via obj.num.

Finished this topic?

0 of 14 topics read in Object-Oriented Programming

Handwritten notes for the exam

Unit-wise SPPU notes and PYQs for this subject.

Helpful? Share it with your class

Free for everyone, no login needed.

Share topic