Javatpoint Logo
Javatpoint Logo

Private Inheritance in C++

Private Inheritance

Private inheritance is a type of different method in which we can implement the has-a relationship. With the help of private inheritance, we can convert the protected, and public members of the class became private members of the class. This means after implementing the private member class, all the bases of the member class become a private class. However, we can implement these private classes inside the member function of the derived class.

Let's briefly understand private inheritance by taking a program.

In the above program, in the public inheritance, if the relationship between two classes is set to be a private class, then the compiler cannot convert the derived class into the base class. That's why the function eat() can not be called by the object S.

In the case of public inheritance, the public method of the base class becomes the public method of the derived class. In other words, we can say that the derived class inherits the property of the base class with the help of inheritance. This is a type of IS-A relationship. But in the case of private inheritance, the public method of the base class becomes the private method for the derived class, even if the base class is protected or public. Here the derived class does not inherit the property of the base class.

But we should be very careful in the case of private inheritance. Private inheritance is very confusing. Here the, inherit does not mean own. Suppose the parent gave the secret recipe of the special candy to the child with a condition to keep this secret. The child is allowed to give different varieties of candy to the people but is not allowed to share the recipe of that candy. With the help of private inheritance, the derived class can be implemented by the base class but not owned by it. Therefore the derived class does not show its interface to the outside world. The only thing that is shown to the outside world is the product.

With the help of private inheritance, a class can be inherited by the implementation. The class declaration can be directly accessed by the user who is a part of the interface. The user can also access the implementation class indirectly through the class declaration.

Example

Output:

Private Inheritance in C++

Explanation:

As we see from the example, the Car class winds up with an inherited Engine component such as a cylinder. The Car method can use the Engine method, getCylinder(), internally to access the Engine component, cylinder.

Conclusion

  • Private inheritance is most likely to be a legitimate design strategy when you're dealing with two classes notrelated by is-a where one either needs access to the protected members of another or needs to redefine one or more of its virtual functions."
  • "Private inheritance means is-implemented-in-terms-of. It's usually inferior to composition."
  • "If you make class Dprivately inherit from a class B, you do so because you are interested in taking advantage of some of the features available in class B, not because there is any conceptual relationship between objects of types B and D."
  • "Private inheritance means nothing during software design, only during software implementation."






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA