Thread: When to use private Inheritance?

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

    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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,318
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 09: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, 10:48 AM