What is a Friend Function?
A friend function is defined outside the class but is granted access to its private and protected members. This is made possible by explicitly declaring the function as a friend inside the class.
Syntax
class ClassName {
friend returnType functionName(arguments);
};
The friend function can be a regular function, a member function of another class or a global function.
Example: Friend Function
#include
using namespace std;
class Box {
private:
int length;
public:
Box() : length(0) {}
// Friend function declaration
friend void setLength(Box &b, int l);
};
// Friend function definition
void setLength(Box &b, int l) {
b.length = l;
cout << “Length set to: ” << b.length << endl;
}
int main() {
Box b1;
setLength(b1, 25); // Accesses private member!
return 0;
}
Note: setLength is NOT a member of Box, but it can modify the private length because it’s a friend.
What is a Friend Class?
Sometimes, you want to allow an entire class to access another class’s private or protected members. In such cases, you use a friend class.
Syntax
class ClassB; // Forward declaration
class ClassA {
friend class ClassB; // ClassB is a friend of ClassA
};
Example: Friend Class
#include
using namespace std;
class Engine;
class Car {
private:
int horsepower;
public:
Car() : horsepower(300) {}
friend class Engine; // Engine can access Car’s private data
};
class Engine {
public:
void displayHorsepower(Car &c) {
cout << “Horsepower: ” << c.horsepower << ” HP” << endl;
}
};
int main() {
Car myCar;
Engine myEngine;
myEngine.displayHorsepower(myCar); // Accessing private member of Car
return 0;
}
Key Points
Feature | Friend Function | Friend Class |
Scope | Specific to function(s) | Applies to all members of the class |
Access to private | Yes | Yes |
Declared using | friend returnType function | friend class ClassName; |
Inheritance affected | No | No |
Use Cases of Friend Functions/Classes
- Operator overloading(e.g., << and >> for cout/cin)
- Allowing controlled access to two different classes
- Facilitating communication between related classeswithout breaking encapsulation
Advantages vs Disadvantages
Advantages
- Useful in operator overloading
- Allows tight coupling between classes for specific use-cases
- Can improve performance by reducing the need for getters/setters
Disadvantages
- Breaks encapsulation
- Increases coupling(tight dependency between classes)
- Difficult to maintain if overused
Conclusion
While friend functions and friend classes offer valuable flexibility in C++, they should be used judiciously. These features are best suited for scenarios where close collaboration between classes is necessary, and conventional interfaces are not sufficient. Used wisely, they can simplify design and improve performance—but overuse can compromise encapsulation and maintainability.
If you’re passionate about mastering advanced C++ concepts and real-world programming skills, the Indian Institute of Embedded Systems (IIES) offers top-notch education and hands-on training in embedded systems. With its expert faculty and industry-aligned curriculum, IIES is an excellent destination for students aiming to build a strong career in embedded technologies.