Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we actually need Private or Protected inheritance in C++?

In C++, I can't think of a case in which I would like to inherit private/protected from a base class:

class Base;
class Derived1 : private Base;
class Derived2 : protected Base;

Is it really useful?

like image 521
Gal Goldman Avatar asked Dec 17 '08 12:12

Gal Goldman


People also ask

Why do we need private inheritance?

The private inheritance can introduce unnecessary multiple inheritance. The private inheritance allows members of Car to convert a Car* to an Engine*. The private inheritance allows access to the protected members of the base class. The private inheritance allows Car to override Engine's virtual functions.

Why and when do we use protected instead of private?

- Private data members cannot be accessed outside the class. - When a class inherits a base class, all the data members except the private get inherited into it. So if we want data members to be accessible to only derived classes and not privately or publicly accessible, then we can use protected.

Why do we use protected class?

The protected keyword specifies access to class members in the member-list up to the next access specifier ( public or private ) or the end of the class definition. Class members declared as protected can be used only by the following: Member functions of the class that originally declared these members.

Why do we need inheritance in CPP?

Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.


3 Answers

It is useful when you want to have access to some members of the base class, but without exposing them in your class interface. Private inheritance can also be seen as some kind of composition: the C++ faq-lite gives the following example to illustrate this statement

class Engine {  public:    Engine(int numCylinders);    void start();                 // Starts this Engine };  class Car {   public:     Car() : e_(8) { }             // Initializes this Car with 8 cylinders     void start() { e_.start(); }  // Start this Car by starting its Engine   private:     Engine e_;                    // Car has-a Engine }; 

To obtain the same semantic, you could also write the car Class as follow:

class Car : private Engine {    // Car has-a Engine  public:    Car() : Engine(8) { }         // Initializes this Car with 8 cylinders    using Engine::start;          // Start this Car by starting its Engine };  

However, this way of doing has several disadvantages:

  • your intent is much less clear
  • it can lead to abusive multiple inheritance
  • it breaks the encapsulation of the Engine class since you can access its protected members
  • you're allowed to override Engine virtual methods, which is something you don't want if your aim is a simple composition
like image 193
Luc Touraille Avatar answered Oct 05 '22 10:10

Luc Touraille


Private can be useful in quite a few circumstances. Just one of them are policies:

Is partial class template specialization the answer to this design problem?.

Another occasion where it is useful is to forbid copying and assigning:

struct noncopyable {     private:     noncopyable(noncopyable const&);     noncopyable & operator=(noncopyable const&); };  class my_noncopyable_type : noncopyable {     // ... }; 

Because we don't want that the user has a pointer of type noncopyable* to our object, we derive privately. That counts not only for noncopyable, but many other such classes too (policies being the most common).

like image 22
Johannes Schaub - litb Avatar answered Oct 05 '22 09:10

Johannes Schaub - litb


Public inheritance models IS-A.
Non-public inheritance models IS-IMPLEMENTED-IN-TERMS-OF.
Containment models HAS-A, which is equivalent to IS-IMPLEMENTED-IN-TERMS-OF.

Sutter on the topic. He explains when you'd choose non-public inheritance over containment for implementation details.

like image 30
Greg Rogers Avatar answered Oct 05 '22 09:10

Greg Rogers