Inheritance

This is a discussion on Inheritance within the C++ Programming forums, part of the General Programming Boards category; i'm in the c++ inheritance lesson in cprogramming.com... In that the syntax for making a class(derived class) inherit all the ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Question Inheritance

    i'm in the c++ inheritance lesson in cprogramming.com... In that the syntax for making a class(derived class) inherit all the stuff from another class(base class) is this:

    Code:
    class base{
    
         //members...
    
    };
    class derived : public base{
    
         //members.
    
    };
    My Question is, that instead of writing:
    Code:
    class derived : public base{
    
         //members.
    
    };
    if i write:
    Code:
    class derived : protected base{
    
         //members.
    
    };
    or if i write:

    Code:
    class derived : private base{
    
         //members.
    
    };
    What would it mean?
    can anybody help me out please?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,629
    You need to understand principles of object-oriented design to grasp my answer. (Note that different OO methodologies use slightly different languages than I have here).

    There are various types of relationships between classes, including "is a" (for example, a Cat is an Animal), "implemented using" relationships (the Submarine class might be "implemented using" a Ship class, noting that a Submarine has a lot of characteristics in common with a Ship, but - depending on point of view - is not always viewed as a Ship), "containment" or "ownership" relationships (e.g. a Garage may contain zero or more Vehicles, a Person may own a Car).

    Public inheritance, conventionally, is used to represent "is a" relationships. A public, protected, and private members of base become (respectively) public, protected, and private members of derived.

    Protected and private inheritance are conventionally used for "implemented using" relationships. The difference between them is related to further derived classes (e.g. if you use your "derived" as a base class). With protected inheritance, the further derived classes have access to base's public and protected members (but never to private members). With private inheritance, the further derived classes have no access to base members (as they are effectively private members of derived).

    Sometimes any of the inheritance types are used to implement "containment" or "ownership" relationships. This is generally considered poor form (as it is better to represent a "A contains a B" relationship as "A has a member of type B", rather than using inheritance at all).
    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, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ inheritance
    By rahulsk1947 in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2009, 11:27 PM
  2. A Lil Help With Inheritance...
    By frassunit in forum C++ Programming
    Replies: 16
    Last Post: 03-04-2009, 03:54 AM
  3. GDB and inheritance
    By markcole in forum C++ Programming
    Replies: 10
    Last Post: 02-04-2009, 09:19 AM
  4. Inheritance
    By Verdagon in forum C++ Programming
    Replies: 8
    Last Post: 06-12-2005, 11:58 AM
  5. inheritance help...
    By tetra in forum C++ Programming
    Replies: 5
    Last Post: 05-12-2003, 08:56 PM

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