public/private inheritance

This is a discussion on public/private inheritance within the C++ Programming forums, part of the General Programming Boards category; my book explains that Code: class C: public A //able to access public member functions class B: private A //un-able ...

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    public/private inheritance

    my book explains that
    Code:
    class C: public A   //able to access public member functions
    class B: private A   //un-able to access public member functions
    isnt the point of inheritance is to add features to a class without modifying it? but if class B cant access anything from base class A then whats the whole point? am i missing something here?
    whats the point of the private keyword in inheritance?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Think of it as a "has-a" relationship, or "is implemented in terms of". Think about if you had a stack that you implemented through a linked-list. You could derive the stack privately from the list class. Then you wouldn't be able to access a public method that was once available for list. Maybe the method was "Next()" to get the next node. Well that doesn't make much sense for a stack. We want to write our own Push and Pop methods and not allow the user to call the old list methods. That is just one of the many examples in which you could use private inheritance. I hope that helps you a little bit.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Class B can access the same stuff from class A as class C can. The difference is what users of class B and C can access.

    What I mean by that is, inside the code for class B, you can call all the public and protected functions that are part of class A. You are using these functions to help implement class B. If somewhere else you have an object of type B, then that is where the interface of A cannot be accessed. It is very similar to making a private member variable of type A inside of class B. When you have a private member variable, you can use that variable inside your code for that class, but other code that uses that class cannot access that private member.

    In general, it is better to use containment (a private member variable of type A) over inheritance (inheriting from A privately) when making an is-implemented-in-terms-of relationship. There are times when private inheritance is required, though. Some examples include when you want class B to have access to some protected member functions in class A, or you want class B to override some virtual functions in class A.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    One example is when you are using a notification scheme. For instance, you have

    Code:
    class GfxWindowListener
    {
    public:
           virtual void OnMove(int x, int y, int w, int h) = 0;
           virtual void OnSelect() = 0;
           virtual void OnDelete() = 0;
           // ... //
    };
    
    class GfxWindowSelectAdapter : public GfxWindowListener
    {
    public:
           virtual void OnMove(int x, int y, int w, int h) { } 
           virtual void OnSelect() = 0;
           virtual void OnDelete() { }
    };
    
    class GfxWindow
    {
          // ... //
    public:
           RegisterListener(GfxWindowListener* list);
           UnRegisterListener(GfxWindowListener* list)
    };
    When you write a class that implements GfxWindowSelectAdapter it will not typically want its overriden methods public. So you can do something like this

    Code:
    class Shape : private GfxWindowSelectAdapter
    {
          virtual void OnSelect() { cout << "OnSelect\n"; }
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Forms of Inheritance
    By kolucoms6 in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2008, 03:09 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 03:41 AM

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