abstract class and pure virtual

C

cppsks

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.
 
J

JKop

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
 
V

Victor Bazarov

cppsks said:
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
 
R

Ron Natalie

cppsks said:
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.
 
C

Cy Edmunds

cppsks said:
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.
 
H

Henrik W Lund

Ron Natalie wrote:

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
 
V

Victor Bazarov

Henrik said:
Ron Natalie wrote:




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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top