The question's title is pretty clear. Here's what I mean by example:
class A
{
public:
virtual void f() = 0;
};
class B: public A
{
public:
virtual void f() = 0;
};
class C: public B
{
public:
virtual void f() {}
};
You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.
Yes, that's fine ... you only need to implement any pure virtual functions in order to instantiate a class derived from an abstract base class.
However, with pure virtuals, there is no base implementation... So isn't it functionally equivalent to declare a pure virtual as either private or protected? A protected pure virtual doesn't make sense because you can't ever invoke the base class's corresponding method.
Yes this is legal because there are not the same functions at all. The B::f()
function is an overriding of A::f()
. The fact that f()
is virtual in both cases is not entering into account.
Yes, it is perfectly legal.
In most situations the declaration on f() in B doesn't change the meaning of the program in any way, but there's nothing wrong with a little redundancy.
Recall that the "= 0" means only that the class may not be instantiated directly; that all pure virtual function must be overridden before an object can be instantiated.
You can even provide a definition for a pure virtual function, which can be called on an instance of a subclass. By explicitly declaring B::f(), you leave open the option of giving a definition for B::f().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With