private or public inheritance?

This is a discussion on private or public inheritance? within the C++ Programming forums, part of the General Programming Boards category; After reading an article on private inheritance Im wondering if I should use private inheritance in the following example instead ...

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

    private or public inheritance?

    After reading an article on private inheritance Im wondering if I should use private inheritance in the following example instead of public.


    Code:
    class CAnimation
    {
      explicit CAnimation(unsigned long Timer);
    
      virtual ~CAnimation();
    
    protected:
      virtual Status Init() = 0;
      virtual void Render() = 0;
    
      virtual Status Update(unsigned long TimeElapsed) = 0;
      virtual void Cleanup() = 0;
    
      virtual bool IsDone();
    };
    
    class CardDealAnimation : public CAnimation
    {
    public:
      CardDealAnimation(CSprite *HandSprites, int HandSize);
    
      virtual Status Init();
    
      virtual void Render() {}
      virtual Status Update(unsigned long Time) {}
      virtual void Cleanup() {}
      virtual bool IsDone() {}
    };
    I used public till now, but Im thinking its not the right choice. The CardDealAnimation does model the "is-a" relationship but it should not provide an interface to further subclasses. So whats better, public or private or is it both 100% correct from a modeling POV?

    Here's the article

    Cprogramming.com - Tutorials - Understanding Private Inheritance

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by KIBO View Post
    I used public till now, but Im thinking its not the right choice. The CardDealAnimation does model the "is-a" relationship but it should not provide an interface to further subclasses. So whats better, public or private or is it both 100% correct from a modeling POV?

    Here's the article

    Cprogramming.com - Tutorials - Understanding Private Inheritance
    In the case of CardDealAnimation and CAnimation it looks like you should use public or protected inheritance. It doesn't matter which of those you use since you override all of CAnimation's methods.

    Private inheritance should be used when the relationship is "has-a", not "is-a".
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    Quote Originally Posted by KIBO View Post
    I used public till now, but Im thinking its not the right choice. The CardDealAnimation does model the "is-a" relationship but it should not provide an interface to further subclasses. So whats better, public or private or is it both 100% correct from a modeling POV?
    There's very rarely any such thing as "100% correct from a modelling POV". Every alternative way of modelling a relationship has both a set of benefits and a set of associated trade-offs. The selection of a particular approach is therefore based on some preference for a set of benefits while accepting a few niggles that make that choice non-ideal.

    If CardDealAnimation does model an "is-a" relationship then, in practice, public inheritence is the way to go.

    Why do you consider that CardDealAnimation "should not provide an interface to further subclasses"? Every class provides some form of interface to any classes derived from it. If you mean that no classes should be allowed to be derived from CardDealAnimation, then there are options;

    1) Make CardDealAnimation's constructors (except its copy constructor) private, and provide public static members that create and return an object by value. Only those static members can create an object, so there is no way to create an instance of a derived class - as they cannot invoke private constructors of a base class.

    2) Have CardDealAnimation exploit private virtual inheritence from a helper class that has all its constructors private, and which declares CardDealAnimation as a friend.

    There are also non-technical options (eg documentation stating that the class is not to be derived from).
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 01:39 PM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  4. I need a code for Tic Tac Toe game ?
    By martyr in forum C++ Programming
    Replies: 11
    Last Post: 12-07-2003, 02:29 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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