TITLE: overview of private inheritance (Newsgroups: comp.lang.c++, 11 Jun 97) LIANG: Mike Liang > > What does it mean to derive a class 'private' from a > > base class vs. 'public' from a base class? [ Jesse Liberty is author of "Teach Yourself C++ In 21 Days". -adc ] LIBERTY: "Jesse Liberty" [snip] you didn't say what the semantics are of private inheritance, but for the sake of completeness let me add that private inheritance is used very differently from public. Public inheritance establishes an "is a" relationship. Private inheritance is one of two principal ways of establishing "implemented in terms of," (the other is aggregation -- or "has a"). Typically you use private inheritance when you have a working, debugged object and you'd like to implement a new object in terms of the established object, but an is-a relationship is inappropriate. In my book I develop a linked list. I then implement a parts-catalogue in terms of that linked list. Since the semantics of a parts-catalogue are different from that of a linked list, but nonetheless much of the functionality of the linked list can be reused, I do not want an is-a relation but I do want to implement the parts catalogue in terms of the linked list. There are two ways to do so. One is to inherit partsCatalogue from linked list privately. This gives the new class access to the functionality without exposing the interface of linked list. The second is to create a private member variable of type linked list (aggregation) and then to delegate responsibility for implementing certain functionality to the linked list. Finally, for completion, let me say that aggregation is required when you need more than one instance of the base class -- that is if my parts catalogue needed more than one linked list I would have been forced to use aggregation. When this is not required, then private inheritance can make for cleaner code and is certainly required when you want access to the protected members of the base class.