In the world of C++ programming, data encapsulation and abstraction are vital pillars. Access specifiers like private, protected, and public play a crucial role in enforcing these principles. However, there are certain situations where external functions or classes need access to private data—this is where friend functions in C++ come into play.
In this blog post, we’ll break down the concept of friend functions, explore their syntax, and walk through real examples to help you understand when and how to use them effectively.


A friend function is a function that is not a member of a class but has the privilege to access its private and protected members. It allows for controlled sharing of data between classes or functions without violating object-oriented design.
Here’s the basic syntax of a friend function:
Thread t = new Thread(); // Thread is in the New stateclass ClassName {
friend ReturnType FunctionName(Arguments);
};
Although it's declared inside the class, it’snot a member function and is defined separately outside the class body.
Let’s look at a simple example to illustrate the use of a friend function.
You have a class Sample that contains a private data member. You want a function displayData() to access and display that private data.
#includeusing namespace std; class Sample { private: int data; public: Sample() : data(10) {} // Friend function declaration friend void displayData(Sample s); }; // Friend function definition void displayData(Sample s) { cout << "Data: " << s.data << endl; } int main() { Sample obj; displayData(obj); return 0; }
Data: 10
Suppose you want a function to access private members from one class and operate in another. Let’s say you have a Box class and you want a friend function showDimensions to display its private data.
#includeusing namespace std; class Box { private: int length; int width; public: Box(int l, int w) : length(l), width(w) {} // Declare friend function friend void showDimensions(Box b); }; void showDimensions(Box b) { cout << "Length: " << b.length << endl; cout << "Width: " << b.width << endl; } int main() { Box myBox(10, 5); showDimensions(myBox); return 0; }
Length: 10
Width: 5
Beyond individual functions, you can declare an entire class as a friend to give all its methods access to private members of another class.
class B;
class A {
private:
int value;
public:
A() : value(100) {}
friend class B;
};
class B {
public:
void showValue(A a) {
cout << "Value from class A: " << a.value << endl;
}
};
Use friend functions when:
If you’re exploring C++ and its deeper concepts like friend functions, you're already on the right track toward becoming a skilled developer. But writing code is only half the battle—ensuring your code is tested and reliable is just as important.
Master testing skills that employers demand and become a complete software professional.
Ready to upgrade your tech journey? [Enroll Now in the Software Testing Course]
Friend functions in C++ offer a unique way to balance encapsulation and flexibility. When used wisely, they can enhance collaboration between classes and functions. Whether you're aiming to deepen your programming expertise or step into software QA, understanding such foundational concepts is key.
Start coding smart—and testing smarter.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR