How to access privately inherited member functions outside the class C++

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

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.