Friend Functions and Friend Classes in C++

Friend Functions and Friend Classes in C++

Most of the time, you hear about encapsulation and how C++ allows friend functions or friend classes to access private data. The C++ friend function is frequently misused, or there is a lack of understanding about when to use a friend class instead of a normal member function. This article will cover the secrets of declaring friend functions, function overloading, accessing private and protected members, and the all-important differences between friend classes and friend functions.

Table of Contents:

What is the Friend Function in C++?

In C++, the friend function is a function that is not a member of a class, but it allows access to its private and protected members. These members are accessed only within the class. By using a friend function, we can easily access the external functions. We use the friend keyword inside a class definition to declare a friend function.  

A friend can be declared a function, a function template, a member function, or a class template.

Declaring a Friend Function

By using the keyword friend, we declare a friend function inside a class. This function does not belong to the class, but it has permission to access the private data. It is defined outside the class, like a normal function.  

Example 1: Using a Friend Function to Access Private Data

Cpp

Output:

Output

The above program uses a friend function in C++. Here, the friend function is declared inside the class, but it was defined outside the class. In the main function, an object is created, s1, and showData(s1) is used to print the private data.

Example 2: Add Members of Two Different Classes

The friend function is used to access and manipulate the data from multiple classes. 

Cpp

Output: 

Output

To access the private members of ClassA and ClassB, the program uses the friend function. The add() function sums their private data by declaring them as friend in both classes.

C++ Function Overloading using Friend Function

In C++, function overloading allows multiple functions with the same name but different parameters. Based on the different argument types or counts, friend functions can be overloaded. 

Example 3: Overloading a Friend Function

Cpp

Output:

Output

The above C++ program uses function overloading with a friend function. The Example class has a private member that is a value; it is accessed by using two overloaded display functions that are declared as friends. It prints the value with the first version and the string message with the second version.

Characteristics of Friend Functions

  1. Access to Private Members – Access to private data and protected data of a class.
  2. Declared Inside, Defined Outside – Declared as a friend of the class but defined outside.
  3. Not a Member Function – It is not called by an object of the class.
  4. Can Be Overloaded – Friend functions may be overloaded like regular functions.
  5. Used for Overloading Operators  – You can use it while overloading those operators that require access to private data.
  6. Breaks Strict Encapsulation - It breaks encapsulation a bit as it accesses private members.

Applications of the Friend Function

  • Accessing Private Members – Permit external functions to gain access to private and protected data.
  • Operator Overloading - It is used to overload the operators like +, -, *, etc., to access private members.
  • Multiple Class Interaction – Function can access the private data of multiple classes (e.g., adding the object of various classes).
  • Implementing Friend Class – Required when a whole class needs to access the private members of another class.
  • Flexibility in Encapsulation – Allows controlled access to private members without exposing them to the world.
  • Interfacing With Non-Member Functions – Handy for dealing with some non-member functions, but not wanting to make it a member function.

Friend Class in C++

In C++, the friend class is a class that gives access to the private and protected members of another class.

Friend class is similar to the friend functions, except that it grants the permission to individual functions. This is helpful when multiple classes need to work closely together, share data, or have a high degree of interaction while still maintaining some level of encapsulation and data hiding.

Declaring a Friend Class

By using the keyword friend, a class can declare another class as its friend. This means all the private and protected members of the class are accessible to the friend class. 

Accessing Private and Protected Members

If we declared the class as a friend, then all the member functions of the class can directly access the private and protected data of another class. 

Example 4: C++ Friend Class

Cpp

Output:

Output

The following C++ program demonstrates the friend class. The Sample class has a private member secret, which is not accessible outside of the class. But now Helper is a friend class, and all the functions of Helper, including showData(), can access the private members of the Sample. The showData() function prints the secret value of a Sample object. So in main(), we create the object s1 and pass 50 to its constructor. Note that h.showData(s1) will print the private data.

Difference Between a Friend Class and a Friend Function

Friend Function Friend Class
Access Level Only the specific function is granted access All functions of the friend class get access
Scope Defined outside the class, but can access private/protected members The whole class gets access to another class’s private/protected members
Use Case Used for specific operations, like arithmetic functions or comparisons Used when two classes need to share internal details, like a helper or utility class
Security More restrictive (only one function gets access) Less restrictive (entire class gets access)

Conclusion

In C++, friend functions and friend classes allow you to access private and protected members of a class without breaking encapsulation completely. Operator overloading, multiple class interaction, and accessing private data for these friend functions are beneficial for all of them, as they do not need to be members of the class. Although friend functions and classes enhance flexibility, they also reduce strict encapsulation, so they should be used with caution. Generally, they are strong supporting actors that govern the interactions of classes of objects and tend to a balance between security and utility.

You can learn more about C++ in the C++ article, and also explore C++ Interview Questions prepared by industry experts.

Friend Functions in C++ – FAQs

Q1. What is a friend function in C++?

Friend function is a function that is not a member of a class, but it allows access to its private and protected members.

Q2. How do you declare a friend function?

Use the keyword friend to declare a friend function inside a class.

Q3. What is the difference between a friend function and a friend class?

A friend function allows one function to access the private member, whereas a friend class allows all its functions to access them.

Q4. Can a friend function be overloaded in C++?

Yes, by using different parameter lists, a friend function can be overloaded.

Q5. Why use a friend class in C++?

The friend class is a class that gives access to the private and protected members of another class.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.