Example & Tutorial understanding programming in easy ways.

What is the friend function in C++?

Friend function in C++
-A friend function of a class is defined outside the class scope but it has the right to access all private and protected members of the class.

How make friend function ?
using 'friend' keyword we make friend function


#include < iostream>
using namespace std;
class r4r {
int width;
friend void display( Box box );
void set( double wid );
// Member function definition
void r4r::set(int wid) {
width = wid;
// Note: display() is not a member function of any class.
void display(r4r obj) {
cout << "Data is: " << obj.width << endl;
int main() {
r4r obj;
// Use friend function to print the data
return 0;


Data is: 10

Read More →
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!