Derived class function cannot access even the public members of the base class when the access specifier is private. But how is it that the function ‘xyz‘ of my derived class able to call ‘showofb‘?
I even tried it by calling the function ‘showofb‘ in the constructor of C. In both cases it works.
How is it able to call the function ‘showofb‘ ?
class B
{
public:
B()
{
cout<<":B:"<<endl;
}
void showofb()
{
cout<<"show of b"<<endl;
}
};
class C : private B
{
public:
C()
{
cout<<":C:"<<endl;
}
void xyz()
{
showofb();
}
};
int main()
{
C c1;
c1.xyz();
}
B::showofb()is a public function. So it can be called byC. If you modifyBto makeshowofbprivate,Cwill no longer be able to call it.The private inheritance means that all public and protected members of
Bare inherited as private byC. SoCcan still call public and protected members ofB, but any classes derived fromCwill not be able to call members ofB.