When to use private Inheritance?

This is a discussion on When to use private Inheritance? within the C++ Programming forums, part of the General Programming Boards category; Hi all I am learning about the private inheritance and found a little bit confusing. In all other type of ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    When to use private Inheritance?

    Hi all

    I am learning about the private inheritance and found a little bit confusing.
    In all other type of inheritance we can reuse the code in derived class but not in case of private( unless we apply some technique to use them).

    My question is that, in what circumtances we should only use private inheritance?

    I will be very thankful if someone explain with help of layman example ?

    Thanks

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Bargi View Post
    My question is that, in what circumtances we should only use private inheritance?
    Pretty much never. It's a sign that you're trying to implement a "has-a" relationship via an "is-a" relationship which is almost never a correct design.

    There is an example on the Cprogramming article board which ends with the statement "If you need this kind of functionality, then private inheritance is the only solution to the problem!" Well, that's nonsense. There is no problem which can only be solved by private inheritance.

    Using private inheritance might save you from writing a certain amount of code, in some cases, but that's hardly a good basis for a design.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I rarely see anything except public inheritance. I've never personally used private inheritance and I think I've only used protected inheritance once.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    I've never had a reason to even think about using it in the last 10 years of C++ programming.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by brewbuck View Post
    Pretty much never. It's a sign that you're trying to implement a "has-a" relationship via an "is-a" relationship which is almost never a correct design.
    Private inheritence is used more often for "implemented using" relationships than for "has a" relationships.

    In terms of the original question, I agree there are no cases where one should use private inheritence. Private inheritence is, invariably, one option among several that can address some need. The choice of which option to use should generally be one that represents a rational trade-off for the intended application.

    If the original question concerned when one might use private inheritence, the answers would probably show some more variation.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Bubba View Post
    I rarely see anything except public inheritance. I've never personally used private inheritance and I think I've only used protected inheritance once.
    Well, I've used protected inheritance quite often. Of course, not remotely as often as public inheritance. But yeah - I've never used private at all.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by Bargi
    I am learning about the private inheritance and found a little bit confusing.
    In all other type of inheritance we can reuse the code in derived class but not in case of private( unless we apply some technique to use them).
    The way that I see it, you can reuse the code in the derived class with private inheritance. In fact, Scott Meyers' opinion is that "private inheritance means is-implemented-in-terms-of", and is "purely an implementation technique", although it is a technique that can pretty much always be replaced by composition.

    This is in contrast to public inheritance ("is-a"), in which one would inherit not only to reuse code (which might not happen at all if the base class is a pure interface), but so that the derived class can be used by existing code that is written for the base class and its derived classes, with no or minimal modification to existing code.

    Quote Originally Posted by EVOEx
    Well, I've used protected inheritance quite often. Of course, not remotely as often as public inheritance. But yeah - I've never used private at all.
    Out of curiosity, but why did you use protected inheritance? Did you intentionally choose it over private inheritance and composition, and if so, what were your reasons?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Currency Calculator
    By desiamerican in forum C# Programming
    Replies: 1
    Last Post: 01-21-2010, 08:53 PM
  2. private or public inheritance?
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2009, 12:57 AM
  3. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 12:34 PM
  4. webBrowser problem
    By algi in forum C# Programming
    Replies: 7
    Last Post: 08-19-2005, 09:35 AM
  5. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21