About Inheritance and its concepts

Discussion in 'C++' started by p_adib@encs.concordia.ca, Nov 11, 2006.

  1. Guest

    Hi all.
    In the context of inheritance in c++, I was taught in class that every
    derived class is "part base class content - part new content".
    Moreover, I was taught that the constructor of the base class is used
    to initialize base class content and that the derived class constructor
    is used to initialize the new contents.

    Ok, here is where I'm confused. If the derived class is part base
    content - part new content, then the keyword "protected" becomes
    unnecessary, since part of the base class is included in the derived
    class apart from the "protected" mechanism. This would be true unless
    public data members and functions are not included in the derived
    class. Then that would mean that the "part base class content" only
    contains protected data members and protected functions of the base
    class.

    If that were true though, it would make no sense to use the base
    class's constructor to initialize the "part base class content" since
    some of this content might not be public and would then not be
    inherited by the derived class and would therefore not require
    initialization.

    Can anyone clear me up on this?


    Also, regarding the "protected" keyword, in what case would I use
    protected functions rather than non-pure virtual functions?
     
    , Nov 11, 2006
    #1
    1. Advertising

  2. Guest

    note: replace all occurences of "public" by "public and private" in the
    post above.
     
    , Nov 11, 2006
    #2
    1. Advertising

  3. John Carson Guest

    "" <> wrote in message
    news:
    > Hi all.
    > In the context of inheritance in c++, I was taught in class that every
    > derived class is "part base class content - part new content".
    > Moreover, I was taught that the constructor of the base class is used
    > to initialize base class content and that the derived class
    > constructor is used to initialize the new contents.
    >
    > Ok, here is where I'm confused. If the derived class is part base
    > content - part new content, then the keyword "protected" becomes
    > unnecessary, since part of the base class is included in the derived
    > class apart from the "protected" mechanism. This would be true unless
    > public data members and functions are not included in the derived
    > class. Then that would mean that the "part base class content" only
    > contains protected data members and protected functions of the base
    > class.
    >
    > If that were true though, it would make no sense to use the base
    > class's constructor to initialize the "part base class content" since
    > some of this content might not be public and would then not be
    > inherited by the derived class and would therefore not require
    > initialization.
    >
    > Can anyone clear me up on this?
    >
    >
    > Also, regarding the "protected" keyword, in what case would I use
    > protected functions rather than non-pure virtual functions?


    *All* of the base class content becomes part of any derived class object.
    However, not all of the base class content may be accessible to derived
    class functions (or to functions outside the class that have access to a
    derived object). Public, private and protected govern what is accessible,
    not what is included.

    --
    John Carson
     
    John Carson, Nov 11, 2006
    #3
  4. Guest

    John, that makes sense.
    So, to clarify:
    1) My mistake was in assuming that the protected keyword indicates what
    is included in a derived class.
    2) All data members and functions of the base class are included in the
    derived class, no matter their visibility. They are included with their
    visibility.

    A new question arises:
    If a data member (class variable) is declared as private in the base
    class, when the base class content is copied into the subclass, then
    how do the visibilities apply?

    Using the below example,

    ******************************
    Person
    ******************************
    protected string lastName
    private string firstName
    public string nationality
    ******************************

    *************************************
    Professor (Generalizes Person)
    *************************************
    private string department
    *************************************

    Will a Professor object's "firstName" variable be private to him? Then
    what is the purpose of the protected keyword?

    John Carson wrote:
    > "" <> wrote in message
    > news:
    > > Hi all.
    > > In the context of inheritance in c++, I was taught in class that every
    > > derived class is "part base class content - part new content".
    > > Moreover, I was taught that the constructor of the base class is used
    > > to initialize base class content and that the derived class
    > > constructor is used to initialize the new contents.
    > >
    > > Ok, here is where I'm confused. If the derived class is part base
    > > content - part new content, then the keyword "protected" becomes
    > > unnecessary, since part of the base class is included in the derived
    > > class apart from the "protected" mechanism. This would be true unless
    > > public data members and functions are not included in the derived
    > > class. Then that would mean that the "part base class content" only
    > > contains protected data members and protected functions of the base
    > > class.
    > >
    > > If that were true though, it would make no sense to use the base
    > > class's constructor to initialize the "part base class content" since
    > > some of this content might not be public and would then not be
    > > inherited by the derived class and would therefore not require
    > > initialization.
    > >
    > > Can anyone clear me up on this?
    > >
    > >
    > > Also, regarding the "protected" keyword, in what case would I use
    > > protected functions rather than non-pure virtual functions?

    >
    > *All* of the base class content becomes part of any derived class object.
    > However, not all of the base class content may be accessible to derived
    > class functions (or to functions outside the class that have access to a
    > derived object). Public, private and protected govern what is accessible,
    > not what is included.
    >
    > --
    > John Carson
     
    , Nov 22, 2006
    #4
  5. * :
    > [top-posting]


    See FAQ item 5.4.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 22, 2006
    #5
  6. Guest

    Alf P. Steinbach wrote:
    > * :
    > > [top-posting]

    >
    > See FAQ item 5.4.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    Thank you M. Steinback for being clear.

    As for my post, I read something very clear about it in the FAQ so I
    will point to it.
    Item [24.6].

    To quote what is necessary:
    "None of the subclasses can access anything that is private in B [the
    base class]."

    Though I am still wondering. If you are using dynamic binding, which of
    the following two cases is legal?

    1) Base b = new SubC(...);
    2) SubC sc = new SubC(...);

    I would assume case 1. Am I right?
     
    , Nov 22, 2006
    #6
  7. * :
    >
    > As for my post, I read something very clear about it in the FAQ so I
    > will point to it.
    > Item [24.6].
    >
    > To quote what is necessary:
    > "None of the subclasses can access anything that is private in B [the
    > base class]."
    >
    > Though I am still wondering. If you are using dynamic binding, which of
    > the following two cases is legal?
    >
    > 1) Base b = new SubC(...);
    > 2) SubC sc = new SubC(...);
    >
    > I would assume case 1. Am I right?


    For (2), the object being declared is of type SubC, and the
    initialization expression, using 'new', produces a SubC* pointer
    (pointing to a dynamically allocated SubC object).

    That will only work if SubC has a constructor that accepts a SubC pointer.

    Presumably you meant to write

    Base b* = new SubC(...); // 1
    SubC sc* = new SubC(...); // 2

    where both are valid if Base is, at this place in the code, an
    /accessible/ base class of SubC.

    It would be the same for

    Base const& b = SubC( ... ); // 1
    SubC const& sc = SubC( ... ); // 2

    For example, if SubC is publicly derived from Base, then Base is always
    an accessible base class.

    There are two separate issues: implicit pointer (and reference) "upcast"
    to a base class, and accessibility of said base class.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 22, 2006
    #7
  8. * Alf P. Steinbach:
    >
    > Base b* = new SubC(...); // 1
    > SubC sc* = new SubC(...); // 2


    The inventor of "transpose back": step forth, show yourself, and make
    Mozilla implement this feature in Thunderbird. Now!

    Now I have to resque some things out of the oven.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 22, 2006
    #8
  9. Guest

    On 11 Nov, 06:57, "" <>
    wrote:
    > Also, regarding the "protected" keyword, in what case would I use
    > protected functions rather than non-pure virtual functions?


    You might have figured this out by now but anyway... The usage of pure
    virtual functions and protected (or private and public) are different
    things. First of, a class with a pure virtual function can not be
    instanciated, but can beuseful to assure that the derived classes have
    a certain interface, for instance. But sometimes there might exist a
    "default" bahaviour for a function, in which case it can be nice to
    implement this in the base-class, so if the derived class does not want
    to change the default behaviour it does not have to worry about that
    function.
    As you see there may be both public and protected pure virtual
    functions, but those two concepts does not affect each other much.
    (Note that you can also have a private pure virtual function but it's
    kind of pointless.)

    --
    Erik Wikström
     
    , Nov 22, 2006
    #9
  10. wrote:
    > (Note that you can also have a private pure virtual function but it's
    > kind of pointless.)


    I'm not so sure about that. It is saying that sub-classes must
    implement the method, but may not call it. This means that the method
    may only be called as part of the implementation of the abstract
    super-class that contains the private pure virtual.

    The problem here is of course that the protection from not being called
    is weak as any derived class can change the access specifier.

    class Base {
    public:
    void doSomething() {
    partOfProcess();
    std::cout << " world" << std::endl;
    }
    private:
    virtual void partOfProcess() = 0;
    };

    class Derived : public Base {
    private: // Can change this to public to make the call legal
    void partOfProcess() {
    std::cout << "Hello";
    }
    };

    int main() {
    Derived d;
    d.doSomething();
    d.partOfProcess(); // Illegal unless Derived puts it in its public
    space
    }

    You should certainly question anybody who implements a virtual method
    with a more liberal access specifier.


    K
     
    =?iso-8859-1?q?Kirit_S=E6lensminde?=, Nov 22, 2006
    #10
  11. Marcus Kwok Guest

    <> wrote:
    > (Note that you can also have a private pure virtual function but it's
    > kind of pointless.)


    This article actually gives the opposite advice: virtual functions
    (which may or may not be pure) should by default be made private:

    http://www.gotw.ca/publications/mill18.htm

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Nov 22, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    22
    Views:
    1,187
    Tim Roberts
    Mar 21, 2006
  2. Trans
    Replies:
    0
    Views:
    165
    Trans
    Apr 23, 2006
  3. thunk
    Replies:
    1
    Views:
    364
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    539
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    664
    thunk
    Apr 3, 2010
Loading...

Share This Page