Friend Class
A friend class is a class that can access the private and protected members of a class in which it is declared as a friend. This is needed when we want to allow a particular class to access the private and protected members of a class.
A class can be declared as a friend of another class. In such a case the friend class can access the private and protected data elements of the class.
A class is declared as a friend using the keyword “friend”.
Syntax:
class sample2;
class sample1
{
private:
data_elements;
member_functions;
protected:
data_elements;
member_functions;
public:
data_elements;
member_functions;
friend sample2;
};
class sample2
{
private:
data_elements;
member_functions;
protected:
data_elements;
member_functions;
public:
data_elements;
member_functions;
};
Class sample2 is declared as a friend of the class sample1. In this situation, the member functions of the class sample2 will be able to access private and protected data elements, and the member function of the class sample1