/* */ Click to Join Live Class with Shankar sir Call 9798158723

What is Friend class ?


When the function is declared as a friend, then it can access the private and protected data members of the class.This is needed when we want to allow a particular class to access the private and protected members of a class.

Q1.) Example of Friends Class .
    
    #include < iostream>  
  
using namespace std;  
  
class A  
{  
    int x =5;  
    friend class B;           // friend class.  
};  
class B  
{  
  public:  
    void display(A &a)  
    {  
        cout<<"value of x is : "<< a.x;  
    }  
};  
int main()  
{  
    A a;  
    B b;  
    b.display(a);  
    return 0;  
}  
Output
value of x is : 5