No one shares secrets with strangers for privacy reasons. But everyone has that one friend with whom they share most of their secrets, if not all. Friend class in C++ refers to the same concept. Public data members and functions are accessible by every class in C++ and many other programming languages. But the C++ friend class is special and can access even the private data members and functions of other classes.

Friend Keyword in C++

Now, you know the benefits of having a friend class in C++. But, to declare any class as a friend class, you do it with the friend keyword. You can use the friend keyword to any class to declare it as a friend class. This keyword enables any class to access private and protected members of other classes and functions.

Use of Friend Class in C++

Friend class has numerous uses and benefits. Some of the primary use cases include:

  • Accessing private and protected members of other classes (as you would know by now)
  • Declaring all the functions of a class as friend functions
  • Allowing to extend storage and access its part while maintaining encapsulation
  • Enabling classes to share private members' information
  • Widely used where two or more classes have interrelated data members

Syntax of Implementing Friend Class in C++

The syntax for implementing a friend class is:

class ClassB;

class ClassA {

   // ClassB is a friend class of ClassA

   friend class ClassB;

   ... .. ...

}

class ClassB {

   ... .. ...

}

As you can see in the syntax, all you need to do is use the keyword friend in front of a class to make it a friend class. Using this syntax will make ClassB a friend class of ClassA. Since ClassB becomes the friend class, it will have access to all the public, private, and protected members of ClassA. However, the opposite will not be true. That’s because C++ only allows granting the friend relation and not taking it. Hence, ClassA will not have access to private members of ClassB.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Examples of Friend Class in C++

Now that you have seen the syntax to implement a friend class in C++ and its use, look at some examples to understand the concept better.

Example 1: A Simple Example to Access Private Members of Other Class

#include <iostream>

using namespace std;

class Exmp_A{

    int i =3;

    // Declaring the friend class

    friend class Exmp_B;

};

class Exmp_B

{

  public:

    void display(Exmp_A &a)

    {

        cout<<"The value of i is : "<<a.i;

    }

};

int main(){

    Exmp_A a;

    Exmp_B b;

    b.display(a);

    return 0;

}

Output:

Friend_Class_In_Cpp_1

As you can see in the output, Class Exmp_B could access the private data member i of class Exmp_A as it was a friend class.

Example 2: To Perform Calculations With a Friend Class in C++

In this example, you will declare class Exmp_B as the friend class and perform an additional operation to add a public integer from class Exmp_B and a private integer from class Exmp_A.

#include <iostream>

using namespace std;

class Exmp_A{

    private:

        int x;

        // Declaring the friend class

        friend class Exmp_B;

    public:

        // Initializing x value using a constructor

        Exmp_A() : x(9) {}

};

class Exmp_B{

    private:

        int y;

    public:

        // Initializing y value using a constructor

        Exmp_B() : y(13) {}    

    // Function to perform addition

    int sum(){

        Exmp_A a;

        return a.x + y;

    }

};

int main(){

    Exmp_B b;

    cout << "Sum is: " << b.sum();

    return 0;

}

Output:

Friend_Class_In_Cpp_2

Friend Function in C++

Similar to the friend class in C++, you can also declare a function as a friend. Declaring a function as friend grants it a special ability to access private and protected data members of a class. You can declare a friend function as a member of another class or a global function.

Different Ways to Implement a Friend Function in C++

As mentioned previously, there are two ways to implement a friend function in C++:

  • Through a method of another class
  • Through a global function

Implementing Friend Function in C++ Through a Method of Another Class

#include <iostream> 

using namespace std;

class Exmp_A{

    private: 

    int A_value; 

    public:

    // Default Constructor

    Exmp_A(){

        A_value = 20; 

    }

    friend class Exmp_B; // Friend Class 

};

class Exmp_B{ 

    private:

    int B_value;

    public:

    // Accessing private members

    void display(Exmp_A& i) {

        cout<<"The private member's value accessed using friend class is: " << i.A_value<<endl; 

    }

};

int main(){

cout<<"Welcome to Simplilearn!"<<endl<<endl;

Exmp_A A_value;

Exmp_B B_value;

B_value.display(A_value);

return 0;

}

Output:

Friend_Class_In_Cpp_3

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Implementing Friend Function in C++ Through a Global Function

#include <iostream>

using namespace std;

class Example{

    // A private member by default

    string s;

public:

        friend void show( Example value );

    void input( string val );

};

void Example::input( string val ){

    s = val;

}

void show( Example value ){

    cout<<"Value of private string data is : "<<value.s<<endl;

}

int main(){

    cout<<"Welcome to Simplilearn!"<<endl<<endl;

    Example value;

    value.input("Simplilearn");

    // Using friend function to display string

    show( value );

    return 0;

}

Output:

Friend_Class_In_Cpp_4.

Example of Friend Function in C++

In the below example, you will create and use a C++ friend function to access private data members of another function and display it in the output.

#include <iostream>

using namespace std;

class Example{

    private:

        int length;

    public:

        Example(): length(0) {}

        friend int printLength(Example); //friend function

};

int printLength(Example e){

    e.length = 20;

    return e.length;

}

int main(){

    Example e;

    cout<<"Length of Example box: "<< printLength(e)<<endl;

    return 0;

}

Output:

Friend_Class_In_Cpp_5

Wrapping It Up

In this article, you learned about the friend class in C++ and how it allows one class to access private and protected members of another class. You can use this to share data members with derived classes while maintaining the essence of inheritance. 

Similar to the friend class in C++, there are several other fundamental concepts of C++ programming that you can learn by referring to Simplilearn’s C++ Tutorial for Beginners. The tutorial is designed to help the newbies grasp the basics of C++ language, such as for loop and arrays.

Besides referring to the tutorial, you can also sign up for our SkillUp platform to hone your skills and grow your knowledge. It offers free online courses for numerous programming languages, including C++. This means that you can learn a new skill by sitting at your home in pajamas. 

However, having the mastery of a single language is not apt in today’s world. Since full-stack developers are in much more demand, it is essential to become a jack and a master of all horses. Our Post Graduate Program In Full Stack Web Development is crafted to help you with just that. By helping you learn around 30 software development skills and tools from different programming languages, the course prepares you to get an excellent job in the programming field.

Have any questions for us? Leave them in the comments section of this article, our experts will get back to you on the same, ASAP!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 24 Feb, 2025

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 8 Jan, 2025

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 8 Jan, 2025

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449