What are Friend Functions in C++?
Learn what friend functions are in C++, how a class grants private access, why operator<< uses them, and the rules on inheritance and mutual friendship.
Expected Interview Answer
A friend function in C++ is a non-member function that a class explicitly grants access to its private and protected members using the friend keyword.
Declared inside the class but defined outside it, a friend function is not a member — it has no this pointer and is not called on an object. Friendship is granted, not taken: only the class can name its friends, so it does not break encapsulation arbitrarily. Common uses include overloading operators like << for streaming, and functions that need intimate access to two different classes. Friendship is not inherited and is not mutual unless declared both ways.
- Lets external functions access private members when genuinely needed
- Enables clean operator overloading such as operator<<
- Allows a function to work with private data of two classes
- Keeps encapsulation controlled since only the class grants access
- Avoids exposing data through unnecessary public getters
AI Mentor Explanation
A friend function is like a trusted physio the team captain personally allows into the dressing room: not a player on the field, yet granted access to private team areas the public never sees. The access is deliberately given by the team, not seized, and it does not make the physio a team member — exactly as a friend function reaches private data without becoming part of the class.
Step-by-Step Explanation
Step 1
Declare the friend
Inside the class, write friend ReturnType functionName(...); to grant access.
Step 2
Define it outside
Define the function normally outside the class; it is not a member and has no this pointer.
Step 3
Access private members
The friend can read and write private and protected members of objects passed to it.
Step 4
Common use: operators
Overload operator<< as a friend so it can stream private fields while remaining a free function.
Step 5
Understand the limits
Friendship is not inherited, not transitive, and not mutual unless each class declares the other.
What Interviewer Expects
- Definition of a friend function as a non-member with private access
- Understanding that friendship is granted by the class
- Awareness it has no this pointer
- Typical use cases like operator<< overloading
- Knowing friendship is not inherited or mutual by default
Common Mistakes
- Calling a friend function on an object like a member
- Assuming friendship is inherited by derived classes
- Thinking friendship is automatically mutual between two classes
- Believing a friend function breaks encapsulation uncontrollably
- Expecting the friend function to have a this pointer
Best Answer (HR Friendly)
“A friend function is an outside function that a class specifically trusts to access its private data. It is not part of the class, but by naming it as a friend, the class gives it special permission, which is handy for things like printing an object's internal values.”
Code Example
#include <iostream>
class Account {
double balance_;
public:
Account(double b) : balance_(b) {}
// grant friendship to a non-member function
friend void printBalance(const Account& a);
// friend operator<< for streaming private data
friend std::ostream& operator<<(std::ostream& os, const Account& a);
};
void printBalance(const Account& a) {
// allowed: direct access to private balance_
std::cout << "Balance: " << a.balance_ << '\n';
}
std::ostream& operator<<(std::ostream& os, const Account& a) {
os << "Account(" << a.balance_ << ")";
return os;
}
int main() {
Account acc(150.75);
printBalance(acc); // not called on the object
std::cout << acc << '\n';
return 0;
}Follow-up Questions
- Does a friend function have a this pointer?
- Is friendship inherited by derived classes?
- Why is operator<< often implemented as a friend?
- What is a friend class and how does it differ?
- Does declaring A a friend of B make B a friend of A?
MCQ Practice
1. A friend function in C++:
A friend function is not a member; it is granted access to the class's private and protected members and has no this pointer.
2. Friendship in C++ is:
Only the class can grant friendship, and it is neither inherited nor mutual unless each class declares the other.
3. A common reason to use a friend function is:
operator<< is often a friend so it can access private members while remaining a free function taking the stream on the left.
Flash Cards
What is a friend function? — A non-member function granted access to a class's private and protected members via the friend keyword.
Does a friend function have this? — No. It is not a member and is not called on an object, so it has no this pointer.
Is friendship inherited? — No. Friendship is neither inherited nor transitive, and not mutual unless each class declares the other.
Why make operator<< a friend? — So it can access private fields while remaining a free function that takes the ostream as its left operand.