Re: virtual function declaration
Richard Powell wrote:
On May 1, 9:26 pm, pmouse <pmo...@cogeco.ca> wrote:
Virtual is only used on base classes, i.e., classes that might be
inherited by others. There is a huge difference in this case, but
none in yours. (Suppose BoxB is inherited from BoxA, then yes,
virtual should be declared on A, otherwise it's just a good manner
to keep the class extend-able)
So if a function is virtual in the base class, and an inherited class
has the same function, then the inherited function is virtual too.
But what if the function overloads the arguments, is it still virtual:
class AbstractBox {
public:
virtual double area() const = 0;
};
class BoxA : public AbstractBox {
virtual double area() { return 0.1; }
This function does not override 'area' in the base class. It hides
the other function since the signature is different. And since the
'AbstractBox::area' is pure, this class (and others) are still
abstract.
};
class BoxB : public AbstractBox {
double area() { return 0.2; } /* this is virtual too */
No, it isn't virtual. The signature of this function is not the
same as 'area' in the base class 'AbstractBox'. 'const' is missing.
};
class BoxC : public AbstractBox {
double area(int a) { return a*0.2; } /* is this virtual too? */
No, it's not, for the same reason: the signature is different.
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask