C++ Friend Functions and Friend Classes: Mastering Access Control
C++ Friend Functions and Friend Classes: Mastering Access Control
This tutorial demonstrates how to declare and use friend functions and friend classes in C++, complete with clear examples.
Data hiding is a cornerstone of object‑oriented programming, preventing external code from directly accessing a class’s private members. Protected members are only visible to derived classes and remain inaccessible from the outside world.
Consider the following example:
class MyClass {
private:
int member1;
};
int main() {
MyClass obj;
// Error! Cannot access private members from here.
obj.member1 = 5;
}
Despite this restriction, C++ offers a mechanism called friend functions that can bypass the usual access control and operate on a class’s private and protected data.
A friend class is a similar concept, allowing an entire class to act as a friend. Both features are powerful tools when used judiciously, enhancing encapsulation without compromising flexibility.
Friend Function in C++
A friend function can read and modify a class’s private and protected members. It is declared with the friend keyword inside the class definition.
class ClassName {
// ...
friend returnType functionName(arguments);
// ...
}
Example 1: Basic Friend Function
// C++ program to demonstrate the working of friend function
#include <iostream>
using namespace std;
class Distance {
private:
int meter;
// friend function
friend int addFive(Distance);
public:
Distance() : meter(0) {}
};
// friend function definition
int addFive(Distance d) {
// accessing private members from the friend function
d.meter += 5;
return d.meter;
}
int main() {
Distance D;
cout << "Distance: " << addFive(D);
return 0;
}
Output
Distance: 5
The function addFive() demonstrates that a friend function can freely access the private meter field. While this example is simple, it illustrates the core idea of friend functions.
Example 2: Operating on Two Classes
// Add members of two different classes using friend functions
#include <iostream>
using namespace std;
// forward declaration
class ClassB;
class ClassA {
public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}
private:
int numA;
// friend function declaration
friend int add(ClassA, ClassB);
};
class ClassB {
public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}
private:
int numB;
// friend function declaration
friend int add(ClassA, ClassB);
};
// access members of both classes
int add(ClassA objectA, ClassB objectB) {
return (objectA.numA + objectB.numB);
}
int main() {
ClassA objectA;
ClassB objectB;
cout << "Sum: " << add(objectA, objectB);
return 0;
}
Output
Sum: 13
In this program, both ClassA and ClassB declare the function add() as a friend. This grants the function access to the private data of both classes, enabling the sum of numA and numB.
Note that the friend declaration in ClassA references ClassB before it is defined. A forward declaration class ClassB; resolves this dependency.
Friend Class in C++
Declaring an entire class as a friend gives all of its member functions access to the private and protected members of the host class. The syntax is straightforward:
class ClassB;
class ClassA {
// ClassB is a friend class of ClassA
friend class ClassB;
// ...
}
class ClassB {
// ...
}
Because ClassB is a friend of ClassA, any method inside ClassB can freely read or modify ClassA’s private data. The reverse is not true; ClassA cannot access ClassB’s private members unless it declares ClassB as a friend.
Example 3: Friend Class in Action
// C++ program to demonstrate the working of friend class
#include <iostream>
using namespace std;
// forward declaration
class ClassB;
class ClassA {
private:
int numA;
// friend class declaration
friend class ClassB;
public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}
};
class ClassB {
private:
int numB;
public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}
// member function to add numA from ClassA and numB from ClassB
int add() {
ClassA objectA;
return objectA.numA + numB;
}
};
int main() {
ClassB objectB;
cout << "Sum: " << objectB.add();
return 0;
}
Output
Sum: 13
The add() method in ClassB can access ClassA’s private member numA because ClassA declared ClassB as a friend. This example shows how friend classes enable tight cooperation while preserving encapsulation.
Overall, friend functions and friend classes are valuable tools in C++ when you need controlled access across class boundaries. Use them sparingly and only when the design benefits from such exceptions to the usual access rules.
C Language
- C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
- C++ Inheritance Models: Multiple, Multilevel, Hierarchical
- Mastering C++ Virtual Functions: Concepts, Examples, and the Override Keyword
- C++ Structures vs Classes: A Practical Guide for Embedded Developers
- C++ Classes & Objects: A Practical Guide with Code Examples
- Master Java: Understanding Objects, Classes, and Core OOP Concepts
- Understanding Storage Classes in C++: Scope, Lifetime, and Usage
- Mastering C++ Overloading: Functions & Operators Explained
- Understanding Polymorphism in C++: A Practical Guide
- C++ Interfaces: Mastering Abstract Classes & Pure Virtual Functions