Inheiritance and derived classes

This is a discussion on Inheiritance and derived classes within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a question on derived classes and the inheiritance designation. In a documnet I have it says about ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    7

    Inheiritance and derived classes

    Hello,

    I have a question on derived classes and the inheiritance designation. In a documnet I have it says about derivation: "Access to inheirited members by derived classes and friends of
    the derived class is independent of the base-class inheiritance designation."

    I took this to mean that it does not matter if the base-class inheiritance designation is public, private, or protected as far as the ability of the derived class to access nheirited
    members.

    I was testing this out with some code. I made a BaseClass that had a public, protected, and private int member, along with methods to get and set each int member. I then established a
    publicly dervived class(DerivedPublic) of the base class. I added no more members to the derived class. When I instantiated an object of type DerivedPublic, I found that I could access each int member through the get method. This made sense.

    I then establishd a protected derivived class(DerivedProtected) of the base class. I added no more members to the derived class. When I instantiated an object of type DerivedProtected, I found that I could not access ANY int member through the get
    method. I don't understand why I could not do this. Based on what the document said, I thought I would be able to access the int data members.


    Thanks,
    Paul

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    with public inheritance this happens....

    base class access specifier public.....
    All public members of base class become public members of derived class.
    base class access specifier protected....
    All protected members of base class become protected members of derived class.
    base class access specifier private...
    All private members of base class become inaccessible in derived class except through public/protected functions ( i.e. get/set funcs)

    with private inheritance this happens....

    base class access specifier public.....
    All public members of base class become private members of derived class.
    base class access specifier protected....
    All protected members of base class become private in derived class.
    base class access specifier private....
    All private members of base class become inaccessible in derived class except through public/protected functions.

    I have never used protected inheritance and have never seen it used either.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest

    While on the topic...

    While on the topic, can you explain the purpose and usage of naming a function virtual. Sample code would be greatly appreciated.

Popular pages Recent additions subscribe to a feed

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