C++ | Friend Class

In this article, we will be learning about the Friend class and Friend Members in C++. We will understand these concepts with relevant examples and also look into the merits and demerits of using a friend function.

Friend Class A friend class can access private and protected members of other classes in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other classes. For example, a LinkedList class may be allowed to access private members of Node.

A friend class can access both private and protected members of the class in which it has been declared as friend.

General Syntax for declaration of a friend class is as follows:

class Node {
private:
    int key;
    Node* next;
    /* Other members of Node Class */
 
    // Now class  LinkedList can
    // access private members of Node
    friend class LinkedList;
};

Now let’s see an example to understand the friend class better.

Example:

#include<iostream>
using namespace std;
 
class A
{
    int x;
          public:
             
    A()
    {
        x=10;
    }
    friend class B;    //friend class
};
 
class B
{
    public:
        void display(A &t)
        {
            cout<<endl<<"The value of x="<<t.x;
        }   
};
 
main()
{
    A _a;
    B _b;
    _b.display(_a);
    return 0;
}

Output:

The value of x=10

In the above example, class B is declared as a friend class inside class A. So, class B can now access all the private and protected members of class A.

So, the value of x inside the object of class A was accessed by the object of class B.

Now, that we know about the friend class let’s now see what a friend function is .

Friend Function:

Similar to the friend class, a friend function can be given a special privilege of accessing the private and protected members of a class.

See also  Types of Pointers (Continuation)

A friend function can be of two types:

  1. A member of another class
  2. A Global Function

A friend function is a special function in C++, which in spite of not being a member of a class can access the private and protected data of that class.

A friend function is a non class member function or any other ordinary function of a class, which is declared as a friend using the keyword “friend” inside the class for declaring the function. By doing this the function that is declared as a friend can access all the private and protected data of that class.

The Keyword friend is only used for the declaration of the function but not for the definition of the function.

The friend function does not need the name of the object or the “.” Operator to be accessed. But the object is accepted as an argument through which the private and protected data is accessed.

There is no specific access modifier in which a friend function has to be declared, that is private, public, or protected. It can be declared under any of the access modifiers.

Syntax:

class <class_name>    
{    
    friend  <return_type>  <function_name>(argument/s);  
};

Let’s Understand this with an example.

Example:

// Find the largest of two numbers using Friend Function
 
#include<iostream>
using namespace std;
 
class Largest
{
    int a,b,m;
    public:
        void set_data();
        friend void find_max(Largest);       
};
 
void Largest::set_data()
{
    cout<<"Enter the First No:";
    cin>>a;
    cout<<"Enter the Second No:";
    cin>>b;
}
 
 
 
void find_max(Largest t)
{
    if(t.a>t.b)
        t.m=t.a;
    else
        t.m=t.b;
         
        cout<<"Maximum Number is\t"<<t.m;
}
 
main()
{
    Largest l;
    l.set_data();
    find_max(l);
    return 0;
}

Output:

Enter the First No: 21
Enter the Second No:32
Maximum Number is    32

Merits and Demerits of the Friend Function:

Merits: 

  • A friend function is able to access members without the need of inheriting the class.
  • Friend function acts as a bridge between two classes by accessing their private data.
  • It can be used to increase the versatility of overloaded operators.
  • It can be declared either in the public or private or protected part of class.
See also     C++ CLASSES AND OBJECTS

Demerits: 

  • Friend functions have access to private members of a class from outside the class which violates the law of the data hiding.
  • Friend functions cannot do any run time polymorphism in its members.

That is all for this article. Happy Coding!

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh