My " Friend " in C++

You know you have a "Friend" in C++.
This friend can help you access the private member outside the class.

So lets meet this "Friend" of our.

How we generally access the private member ?

In my  discussed in detail about how to access the private member data




#include<iostream>
using namespace std;

class giveAll
{
private:
    
    int priV;
    
public:
    
    giveAll()
    {
        priV = 10;           // "priV"initialized using constructor
    }
    
    void print()
    {
        cout << "priV accessed using public fxn print , priV = " << priV;
    }
};

int main()
{
    giveAll obj;   //object of class giveAll created and constructor called
    obj.print();    // "print" function called 
}


OUTPUT :
priV accessed using public fxn print , priV = 10


How to access private member using friend function ?

A non-member function can access the private and protected members of a class if it is declared a friend of that class. That is done by including a declaration of this external function within the class, and preceding it with the keyword friend:

#include<iostream>
using namespace std;

class giveAll
{
private:

    int priV;

public:

    giveAll()
    {
        priV = 10;
    }

    void print()
    {
        cout <<"priV accessed using public fxn print , priV = " << priV;
    }

    friend void friendFxn(giveAll objrec);
};

// Friend function

void friendFxn(giveAll objrec)   
// defining Friend Function "friendFxn" , here "objrec" is the object "obj" passed as a parameter

{
    cout <<"\n\npriV accessed using Friend fxn friendFxn , priV= " << objrec.priV;
}


int main()
{
    giveAll obj;   //object of class giveAll created and constructor called
    obj.print();
    friendFxn(obj); // remember: friend fxn is called without the use of any object, because it is not a class member
}


How access private member using friend class ?

In this case we will use a friend class instead of friend function to access private member of some other class.
A friend class is a class whose members have access to the private or protected members of another class:

#include<iostream>
using namespace std;

class normalClass
{
private:
    int priV = 10;
public:
    friend class friendClass;       // Friend class "friendClass" inside nornalClass
};

class friendClass
{
public:
    void print(normalClass objNrec)            // objN received
    {
        cout <<"value of priV accessed using fxn of friend class , priV = " objNrec.priV;          //priV printed
    }
};

int main()
{
    normalClass objN;
    friendClass objF;
    objF.print(objN);  // "objN" passed to the function of friend class "print"

}

OUTPUT:
value of priV accessed using fxn of friend class , priV = 10


NOTE:
Friend class("friendClass") members have access to the private or protected members of another class("normalClass") , but "normalClass" does not have access to the private or protected member of the friendClass.





Comments

Popular Posts

Why to choose C# ? - for Beginners

NOT SO COMMON C++ : Access Specifiers

How to add/change Environment variable on your system ?