Accessing private members of base class in the derived class by using friend class

Accessing
private members of base class in the derived class by using friend class
A public member function of a base class can
access the private member of the derived clas irrespective of the friend class
has been derived privately or publicly.
Example: A friend class derived privately.
Class base
{
friend class derived;
private:
int count;
base( ):count(0)
{ }
};
class
derived : private base
{
public:
void show()
{ cout<<”count=”<<++count; //valid
}
};
void
main()
{ derived d1;
d1.show();
getch();
}
note: if we derive
another class from base and try to access the private data member count then
there
will be an
error.
class
der2:public base
{ public:
void
show()
{
cout<<”count=”<<++count;
//error , can not access the private data member.
}
};