abstract class and pure virtual

Discussion in 'C++' started by cppsks, Nov 2, 2004.

  1. cppsks

    cppsks Guest

    Consider the following:

    class BaseInterface
    {
    public:
    virtual void something() = 0;
    };

    class AbstractSubclass : public BaseInterface
    {
    public:
    virtual void somethingElse() = 0;
    virtual void something() = 0; // is this declaration preferred or not?
    };

    First of all, I am "assuming" (since I don't know for sure) if the
    declaration of pure virtual "something" method in AbstractSubclass won't
    hide the one in BaseInterface. If it doesn't hide, then is this
    redeclaration preferred since the subclasses of AbstractSubclass can see
    this and implement it or is it better it leave it alone, meaning declare it
    in BaseInterface only?

    Thanks.
    cppsks, Nov 2, 2004
    #1
    1. Advertising

  2. cppsks

    JKop Guest

    cppsks posted:

    > Consider the following:
    >
    > class BaseInterface
    > {
    > public:
    > virtual void something() = 0;
    > };
    >
    > class AbstractSubclass : public BaseInterface
    > {
    > public:
    > virtual void somethingElse() = 0;
    > virtual void something() = 0; // is this declaration preferred or
    > not?
    > };
    >
    > First of all, I am "assuming" (since I don't know for sure) if the
    > declaration of pure virtual "something" method in AbstractSubclass
    > won't hide the one in BaseInterface.



    It doesn't hide it. Or then again you could say that it does. Either way it
    doesn't make a difference.

    > If it doesn't hide, then is this
    > redeclaration preferred since the subclasses of AbstractSubclass can
    > see this and implement it or is it better it leave it alone, meaning
    > declare it in BaseInterface only?



    I myself would only put it in "BaseInterface". One reason being that I
    couldn't be bothered typing it out. The second reason being:

    class UltimateBase
    {
    public:
    virtual int Monkey() const = 0;
    virtual int Ape() const = 0;
    };


    class DerivedAlpha : virtual public UltimateBase
    {
    public:
    virtual int Monkey() const
    {
    return 1;
    }
    };

    class DerivedBeta : virtual public UltimateBase
    {
    public:
    virtual void Ape() const
    {
    return 1;
    }
    };


    class Fullyfledged : virtual public DerivedAlpha, virtual public DerivedBeta
    {

    };



    int main()
    {
    Fullyfledged cheese;
    }



    -JKop
    JKop, Nov 2, 2004
    #2
    1. Advertising

  3. cppsks wrote:
    > Consider the following:
    >
    > class BaseInterface
    > {
    > public:
    > virtual void something() = 0;
    > };
    >
    > class AbstractSubclass : public BaseInterface
    > {
    > public:
    > virtual void somethingElse() = 0;
    > virtual void something() = 0; // is this declaration preferred or not?
    > };
    >
    > First of all, I am "assuming" (since I don't know for sure) if the
    > declaration of pure virtual "something" method in AbstractSubclass won't
    > hide the one in BaseInterface.


    It _overrides_ it.

    > If it doesn't hide, then is this
    > redeclaration preferred since the subclasses of AbstractSubclass can see
    > this and implement it or is it better it leave it alone, meaning declare it
    > in BaseInterface only?


    If you're going to use 'AbstractSubclass' definition as documentation,
    then, sure, every little bit helps. Otherwise, it shouldn't matter. The
    language doesn't require it. As to the style, everybody's got their own.

    V
    Victor Bazarov, Nov 2, 2004
    #3
  4. cppsks

    Ron Natalie Guest

    cppsks wrote:

    > First of all, I am "assuming" (since I don't know for sure) if the
    > declaration of pure virtual "something" method in AbstractSubclass won't
    > hide the one in BaseInterface.


    A name in defined in a derived class hides names in the base class. Doesn't
    matter if it's pure or virtual or not.

    The function AbstractSubclass::something() overrides the BaseInterface
    function of the same signature.

    > If it doesn't hide, then is this
    > redeclaration preferred since the subclasses of AbstractSubclass can see
    > this and implement it or is it better it leave it alone, meaning declare it
    > in BaseInterface only?


    It's not preferred one way or the other really. In this case there is no
    pracitcal affect to the second declaration. It's sort of like repeating
    virtual on derived overriders.
    Ron Natalie, Nov 2, 2004
    #4
  5. cppsks

    Cy Edmunds Guest

    "cppsks" <> wrote in message
    news:cm87c0$co1$...
    > Consider the following:
    >
    > class BaseInterface
    > {
    > public:
    > virtual void something() = 0;
    > };
    >
    > class AbstractSubclass : public BaseInterface
    > {
    > public:
    > virtual void somethingElse() = 0;
    > virtual void something() = 0; // is this declaration preferred or not?
    > };
    >
    > First of all, I am "assuming" (since I don't know for sure) if the
    > declaration of pure virtual "something" method in AbstractSubclass won't
    > hide the one in BaseInterface. If it doesn't hide, then is this
    > redeclaration preferred since the subclasses of AbstractSubclass can see
    > this and implement it or is it better it leave it alone, meaning declare
    > it
    > in BaseInterface only?
    >
    > Thanks.
    >
    >


    There is no standard answer, but I personally don't copy the definition of
    the function in a derived class. It is fundamental to good maintainability
    that everything be defined in exactly one place if at all possible.

    --
    Cy
    http://home.rochester.rr.com/cyhome/
    Cy Edmunds, Nov 3, 2004
    #5
  6. Ron Natalie wrote:

    <snip>

    > It's not preferred one way or the other really. In this case there
    > is no pracitcal affect to the second declaration. It's sort of like
    > repeating virtual on derived overriders.


    Greetings!

    Wouldn't this be necessary in the following situation?

    class TheBase
    {
    public:
    virtual void theFunc() = 0;
    };

    class TheDerived : public TheBase
    {
    public:
    virtual void theFunc(){ do stuff while(!done);return; }
    };

    class TheDoublyDerived : public TheDerived
    {
    virtual(?) void theFunc(){ do other_stuff while(!done);return; }
    };

    or is this invalid, or redundant? Is a function virtual through all
    generations if it's declared as such at some point in the inherital
    hierarchy? I'm (fairly) new at C++, so this is something that I'm kinda
    unsure about.

    -Henrik W Lund
    Henrik W Lund, Nov 5, 2004
    #6
  7. Henrik W Lund wrote:
    > Ron Natalie wrote:
    >
    > <snip>
    >
    >> It's not preferred one way or the other really. In this case there
    >> is no pracitcal affect to the second declaration. It's sort of like
    >> repeating virtual on derived overriders.

    >
    >
    > Greetings!
    >
    > Wouldn't this be necessary in the following situation?
    >
    > class TheBase
    > {
    > public:
    > virtual void theFunc() = 0;
    > };
    >
    > class TheDerived : public TheBase
    > {
    > public:
    > virtual void theFunc(){ do stuff while(!done);return; }


    'virtual' is redundant here too.

    > };
    >
    > class TheDoublyDerived : public TheDerived
    > {
    > virtual(?) void theFunc(){ do other_stuff while(!done);return; }
    > };
    >
    > or is this invalid, or redundant?


    It's valid and redundant. Good practice, however, suggests that you
    should put 'virtual' there to save the others the need to look for and
    at the original class. Redundancy is not always bad.

    There are cases, of course, where declaring the overrider 'virtual' can
    be seen as a mistake. Imagine that you change the interface of 'TheBase'
    and forget to change the same function in 'TheDerived'. If you don't
    have 'virtual' in the declaration of 'theFunc', the compiler is likely
    to warn you that "non-virtual TheDerived::theFunc hides TheBase::theFunc"
    (or something of that sort) and you'll know. If 'theFunc' is declared
    virtual in 'TheDerived' too, you are less likely to get any kind of
    a warning. Although I've seen compilers that don't emit any warnings
    in either case.

    > Is a function virtual through all
    > generations if it's declared as such at some point in the inherital
    > hierarchy?


    Yes.

    > I'm (fairly) new at C++, so this is something that I'm kinda
    > unsure about.


    Understandable.

    V
    Victor Bazarov, Nov 5, 2004
    #7
    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. Matthias Kaeppler
    Replies:
    1
    Views:
    426
    R.F. Pels
    May 22, 2005
  2. IK
    Replies:
    2
    Views:
    597
    hemraj
    Jul 23, 2004
  3. vsgdp
    Replies:
    7
    Views:
    396
    Ron Natalie
    Sep 25, 2005
  4. Replies:
    4
    Views:
    802
    Rolf Magnus
    May 17, 2006
  5. Arne Schmitz
    Replies:
    4
    Views:
    420
    Daniel Albuschat
    Jan 17, 2007
Loading...

Share This Page