2 Ways to Implement Friend Function in C++ (Including Practical Example)

Free C++ course with real-time projects Start Now!!

C++ Friend Function: Where friends have access to your private members

It is one of the most popular and important features of classes and objects in C++. Let us understand the concept with a real-life example.

There are numerous things that we keep confidential from strangers in order to maintain our own privacy, but we might encounter certain unforeseen situations where we need to share this confidential information with some people.

We do not share this confidential or private data or information to any stranger, but only to the people, we are close to. Just like we share some of the private things of our lives with our friends, C++ allows classes to share their private data members and member functions with friend functions when required.

Friend Function in C++

In programming terminology, a friend function helps you access the private and protected members of another class in which it is declared with the keyword ‘friend’.

It is important to note that in order to use a friend function and to access the non-public members of a class, we need to declare it within the body of the class itself.

Wait!! Have you checked our Tutorial on Classes and Object in C++

Friend functions in C++ can be of two types:

  • A Method of another class: When we have more than one class and we want to access the non-public data members of a particular class, we declare a friend class.
  • A Global function: A global friend function allows you to access all the private and protected members of the class, unlike a normal friend function that allows you to access only specific members of the class.

C++ gains an upper edge over other programming languages that do not support the concept of friend functions, like Java.

Friend Function in C++

Syntax of Friend Function in C++

Now that we have understood what are friend functions, let us proceed further by understanding the declaration and definition of a friend function in C++.

1. Declaration

class Class_name
{

Access specifiers // private or protected
.
.
friend return_type function_name(argument(s));
.
.

}

2. Definition

class Class_name
{

Access specifiers // private or protected
.
.
friend return_type function_name(parameter(s));
.
.
}

return_type function_name(parameter(s))
{
.
.
.

/* Accessing private and protected data members of the class with the help of friend function with name function_name */

.
.
.

}

C++ Friend Function Implementation

After developing an understanding as to what friend functions are, let us see how we can implement it.

As discussed earlier, there are two ways in which we can implement friend functions in C++ as a method of a class or as a global friend function.

Learn Virtual Function in C++ with Real-time Example

1. Implementation through a method of another class

Here is a C++ that shows the implementation of friend functions as a method of a class:

#include <iostream> 
using namespace std;

class Base 
{ 
private: 
int base_value; 
public: 
Base() // Default constructor 
{ 
base_value = 10; 
} 
friend class Derived; // Friend Class 
}; 

class Derived 
{ 
private: 
int derived_value; 
public:

/*Accessing private members of the base class using friend class Derived */
void display(Base& x) 
{ 
cout<<"The base value accessed using friend class is: " << x.base_value<<endl; 
} 
}; 

int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

Base base_value; 
Derived derived_value; 
derived_value.display(base_value); 
return 0; 
}

Code-

 Implementation ofFriend Function in C++ with Another class

Output-

Output of C++ Friend Function

Have you checked the Inline Function in C++?

2. Implementation through Global Friends

Here is a C++ that shows the implementation of global friend functions in a class:

#include <iostream>
using namespace std;

class Program 
{
// private by default
string data;

public:
friend void display( Program value );
void input( string val );
};

void Program::input( string val ) // Input definition of member function
{
data = val;
}

void display( Program value ) 
{
cout<<"The data stored in the string is : "<<value.data<<endl;
}

int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

Program value;

value.input("DataFlair");
display( value ); // Use of friend function to display the string
return 0;
}

Code-

Implementation through Global Friends in C++

Output-

Output of Global Friends in C++

Characteristics of Friend Function in C++

In order to summarize some of the glitches involved in friend functions, we need to keep the following key points while working with friend functions in C++.

The implementation of friend functions is not commutative in nature unlike seen in Inheritance. For example, if class A is a friend to class B, then it does not mean class B is also a friend to class A. This means that class A can access the non-public members of class B, but, class B cannot access the non-public members of class A.

You cannot implement the concept of Inheritance and friend functions in the same class.

Excessive use of friend functions in C++ degrades the importance of data encapsulation. Therefore, it should be used only where it is necessary.

Summary

It is time to summarise our tutorial now. If you have gone through this entire tutorial, then you have developed a good understanding of friend functions in C++. We covered each and everything related to C++ friend functions in detail.

We started off this tutorial by taking a simple real-life example and understood the relevance of friend function with Classes and Objects. Now, you’ll not find any difficulties related to this topic. If you still find problems, let us know in the comment section we will help you in the best possible way.

Check out this amazing tutorial Polymorphism in C++ An Essential Guide

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

2 Responses

  1. tariq faqira says:

    I would to say thanks man and i would like to say for all people to visit this site to learning.

Leave a Reply

Your email address will not be published. Required fields are marked *