In reality, when we inherit privately we create a new class and the members of the base class(private, protected, public) can be accessed in the derived class but not outside it
Furthermore, when we inherit privately all the public members of the class will become private for the derived class
But, is it possible? To keep some member functions private and others accessible outside the class
The truth is, yes! You can
Here’s an example
#include <iostream>
using namespace std;
class base
{
public:
void display()
{
cout<<endl<<"in display";
}
void show()
{
cout<<"in show";
}
};
class derived:private base
{
public:
using base::display;
};
int main()
{
derived d;
d.display(); // WORKS
// d.show(); // ERROR
return 0;
}
As can be seen, from the above example that the privately inherited member base::display() can be accessed outside the class
Next, we can make the show() not be accessed outside the class
Leave a Reply