C++ Friend Function and Friend Class | Access Private Members and Use Cases


This complete tutorial on C++ Friend Function and Friend Class explains how functions or classes can access private and protected members of other classes. It covers friend functions, friend classes, and their practical use cases. Following best practices, it helps learners write flexible and modular C++ programs while controlling access.

Friend Function and Friend Class – Complete Tutorial

1. What is a Friend Function?

A friend function is a function that is not a member of a class, but has access to the private and protected members of the class.

  1. Declared using the keyword friend inside the class
  2. Can access class internals without being a member

Syntax:


friend return_type function_name(parameters);

Example:


#include <iostream>
using namespace std;

class Box {
private:
int width;

public:
Box(int w) : width(w) {}

// Friend function declaration
friend void printWidth(Box b);
};

// Friend function definition
void printWidth(Box b) {
cout << "Width: " << b.width << endl;
}

int main() {
Box box(10);
printWidth(box); // Access private member
}

Key Points:

  1. Friend functions cannot be called using object.member
  2. They enhance flexibility without breaking encapsulation too much
  3. Not bound by access specifiers (public, private, protected)

2. What is a Friend Class?

A friend class can access private and protected members of another class.

  1. Declared using the keyword friend inside the target class
  2. Useful when two classes are closely related

Example:


class Engine;

class Car {
private:
int speed;

public:
Car(int s) : speed(s) {}
// Friend class declaration
friend class Engine;
};

class Engine {
public:
void showSpeed(Car &c) {
cout << "Car speed: " << c.speed << " km/h" << endl;
}
};

int main() {
Car c(120);
Engine e;
e.showSpeed(c); // Access private member
}

3. Use Cases of Friend Functions and Classes

  1. Operator overloading
  2. Sometimes operator functions are implemented as friends to access private members.
  3. Tightly coupled classes
  4. When one class needs full access to another class for efficiency.
  5. Non-member utility functions
  6. Functions that need read/write access to multiple objects of a class without being a member.
  7. Input/output stream operators (<<, >>)
  8. Stream operators are often implemented as friend functions to access private members for display or input.

Best Practices

  1. Use friend functions/classes sparingly
  2. Prefer encapsulation first; only use friends when necessary
  3. Avoid making everything a friend (can break encapsulation)
  4. Document friend relationships clearly for maintainability

Common Mistakes

  1. Declaring a friend outside the class incorrectly
  2. Overusing friend classes leading to tightly coupled design
  3. Assuming friend functions are members (they are not)

Summary

In this chapter, you learned about C++ Friend Functions and Friend Classes, including:

  1. Friend function declaration and use
  2. Friend class access
  3. Practical use cases
  4. Best practices and common pitfalls

Friend mechanisms allow controlled access to private members while keeping flexibility in class design.