Cover Image for Private Inheritance in C++
113 views

Private Inheritance in C++

Private inheritance is one of the three types of class inheritance in C++, along with public and protected inheritance. With private inheritance, a derived class inherits the members of a base class, but these inherited members become private members in the derived class, effectively hiding them from the outside world. This means that the derived class can access the inherited members, but they are not visible to external code.

Here’s how private inheritance works in C++:

C++
class Base {
public:
    void publicFunction() {
        std::cout << "Public function in Base" << std::endl;
    }

    void sharedFunction() {
        std::cout << "Shared function in Base" << std::endl;
    }
};

class Derived : private Base {
public:
    void callBaseFunctions() {
        publicFunction(); // Accesses publicFunction from Base
        sharedFunction(); // Accesses sharedFunction from Base
    }

    void derivedFunction() {
        std::cout << "Derived function" << std::endl;
    }
};

int main() {
    Derived derived;

    // The following line will result in a compilation error
    // derived.publicFunction();

    // The following line will result in a compilation error
    // derived.sharedFunction();

    derived.callBaseFunctions(); // Calls the Base class functions through Derived
    derived.derivedFunction();   // Calls the Derived class function

    return 0;
}

In this example:

  • The Base class has two public member functions: publicFunction() and sharedFunction().
  • The Derived class privately inherits from Base using the syntax class Derived : private Base. This means that all public and protected members of Base become private members of Derived.
  • The Derived class can still access the inherited members of Base internally. In the callBaseFunctions() member function, it demonstrates how the private members of Base can be used within Derived.
  • Outside of the Derived class, attempts to access publicFunction() and sharedFunction() directly result in compilation errors because these functions are treated as private members of Derived.

Private inheritance is useful in situations where you want to reuse the implementation of a base class but don’t want to expose its interface directly to the outside world. It allows you to encapsulate and control access to the base class’s functionality while still benefiting from code reuse.

Common use cases for private inheritance include:

  1. Implementation Inheritance: When you want to reuse the implementation details of a base class without exposing its interface.
  2. Implementing Interfaces: When you want to implement an interface but don’t want to expose all of the interface’s methods.
  3. Enhancing Encapsulation: When you want to tightly control access to the base class’s members and only expose selected functionality to the outside world.

However, private inheritance should be used with caution because it can make the code less intuitive and may require more careful design and documentation. In most cases, composition (using an instance of the base class as a member of the derived class) or public/protected inheritance is preferred.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS