Friend Function and Friend Classes in C++

Friend Function and Friend Classes in C++:

In this article, I am going to discuss Friend Function and Friend Classes in C++ with Examples. Please read our previous article where we discussed Abstract Classes in C++ with Example. At the end of this article, you will understand how to create friend functions and friend classes in C++ with examples.

Why Friend Function and Friend Classes in C++?

We know, data hiding is one of the most important concepts in object-oriented programming. It restricts the access of private and protected members of a class from outside of the class. That means a class cannot access the private members of another class. Similarly, a class that doesn’t inherit another class cannot access its protected members. Here, we are not talking about public members because they are accessible outside the class upon an object.

Here, we will learn how to access those private and protected members from outside the class using friend functions and friend classes without inheritance. Of course, yes, you can access the public members using friend functions and friend classes in C++.

Friend Function in C++:

A friend function in C++ is defined as a function that can access private, protected, and public members of a class. A friend function can be a member of another class or can be a global function in C++. Let us look at friend functions in C++. If we have a class called Test as follows:

Friend Function in C++

It is having a private member that is integer x, a protected member that is integer y, and a public member that is integer z. That’s it. We have written a simple class. Now we will write a function fun as follows:

Why Friend Function and Friend Classes in C++?

Inside the function, we have created the object t of class Test. Then we are assigning the value of x, y, and z members of the class Test. So, can we access them?

See we have a function that is accessing the data members of the class Test. Is it possible? Can a function access all the members of the object of the class Test? Yes, if it is a member function but, in this case, it is not a member function. This is the outside function. And this is like a global function.

So outside or global function is having an object of the class Test and upon an object, it is trying to access private, protected, and public members. We already know that upon object only public members are accessible. So, it cannot access protected and private members. These members are not allowed to be accessed.

But we can access public members that already we know. But we want function Fun access protected as well as private members of class Test. So, allow this function to access them. Allow means who should allow? Class Test should allow it. So how class can allow it? Look at this. A class should have a declaration that functions as

void Fun();

Though the function doesn’t belong to the class Test so we have to add the keyword friend as shown in the below image. By using the keyword friend compiler knows the given function is a friend function. For accessing the data members, the declaration of a friend function in C++ must be done inside the body of a class starting with the keyword friend.

Friend Function and Friend Classes in C++ with Examples

Now we can access all the members of the class Test in function Fun. So, the friend function is a global function that is an outside function that can access all the members of a class upon an object, not directly upon an object. If the object is there then it can access private, protected, and public members.

The class should say that although the function doesn’t belong to us, it’s a friend for us. This is useful in operator overloading mostly.

Example to Understand Friend Function in C++:
#include <iostream>
using namespace std;

class Test
{
    private:
        int x;
    protected:
        int y;
    public:
        int z;
    friend void Fun();
};

void Fun()
{
    Test t;
    t.x = 10;
    t.y = 20;
    t.z = 30;
    cout << " X : " << t.x << endl;
    cout << " Y : " << t.y << endl;
    cout << " Z : " << t.z << endl;
}

int main()
{
    Fun();
    return 0;
}
Output:

Example to Understand Friend Function in C++

Note: A friend function of a class is defined outside that class scope but it has the right to access all private, protected, and public members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

Characteristics of a Friend Function in C++:
  1. The function is not in the scope of the class to which it has been declared as a friend.
  2. It cannot be called using the object as it is not in the scope of that class.
  3. It can be invoked like a normal function without using the object.
  4. It cannot access the member names directly and has to use an object name and dot membership operator with the member’s name.
  5. It can be declared either in the private or the public part.
Friend Classes in C++:

In C++, a friend class can access private, protected, and public members of another class in which it is declared a friend. It is sometimes useful to allow a particular class to access private members of other classes. Now let us look at friend classes in C++. So far that we have an example here.

Friend Classes in C++

Here we have a class called My and it is having only one private member that is integer a of value 10. Then we have another class called Your which is taking an object m of My class. This is having has a relationship. Upon object which members of My class we can access? Only public members. but it’s not having any public members. It is having only one private member. Can we access that in Your class? There is a function Fun that is trying to print the value of a. Is it allowed? No, not allowed.

Here you will get an error that we cannot access private members of the class My. But we want Your class to access the private members of My class upon an object. Directly we cannot access it because Your class is not inheriting from My class. It is having a has-a relationship. It’s not is-a relationship. So, we want to access upon an object. How is it possible? If you want Your class to access private members of My class upon an object then you have to declare Your class as a friend inside My class as,

Friend Classes in C++ with Examples

With the above changes in place, now Your class can access private, protected, and public members of My class. All the members we can access. So that is the use of friend classes in C++. They can access members of objects of other classes.

One more thing we have to do. When we have written friend in the above piece of code, then the compiler will say that there is no class like Your. So, we must write down the name of the class before using it. So, we have to write one more line as follows:

Friend Classes in C++ with Examples

Don’t write brackets there. If you write brackets there then that’s a re-declaration of a class. Just write the name of the class there. If a compiler starts from the first line, then it comes to class Your which is defined after the class My. So, it needs a definition of Your. So, we just have a name declared before the class My definition. The body of Your definition is after the class My.

This is useful in container classes like Your classes having an object of My class. So, we can say Your class is a container of My class. In container classes, if they want to access private or protected members then we can declare them as friends inside other classes.

Example to Understand Friend Classes in C++:
#include <iostream>
using namespace std;

class Your;
class My
{
    private:
    int x;
    protected:
    int y;
    public:
    int z;
    friend Your;
};

class Your
{
    public:
    My m;
    void Fun()
    {
        m.x = 10;
        m.y = 20;
        m.z = 30;
        cout << "X = " << m.x << endl;
        cout << "Y = " << m.y << endl;
        cout << "Z = " << m.z << endl;
    }
};

int main()
{
    Your obj;
    obj.Fun ();
    return 0;
}
Output:

Example to Understand Friend Classes in C++

Example:

Instead of writing the name before the My Class definition, we can also write friend class Your; inside the My class and it will work as expected. Following is the example.

#include <iostream>
using namespace std;

class My
{
    private:
    int x;
    protected:
    int y;
    public:
    int z;
    friend class Your;
};

class Your
{
    public:
    My m;
    void Fun()
    {
        m.x = 10;
        m.y = 20;
        m.z = 30;
        cout << "X = " << m.x << endl;
        cout << "Y = " << m.y << endl;
        cout << "Z = " << m.z << endl;
    }
};

int main()
{
    Your obj;
    obj.Fun ();
    return 0;
}
Output:

Friend Function and Classes in C++ with Examples

Key Points of Friend Functions and Friend Classes in C++:
  1. Friend functions are global functions
  2. They can access private, protected, and public members of a class upon their objects
  3. A class can be declared as a friend of another class
  4. All the functions of the friend class can access private and protected members of other classes.
  5. Friendship is not mutual. If class A is a friend of B, then B doesn’t become a friend of A automatically.
  6. Friendship is not inherited.
  7. Friends should be used only for a limited purpose. Too many functions or external classes are declared as friends of a class with protected or private data, it against the principle of encapsulation in object-oriented programming.

In the next article, I am going to discuss Static Members in C++ with Examples. Here, in this article, I try to explain Friend Function and Classes in C++ with Examples and I hope you enjoy this Friend Function and Classes in C++ with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.