Kind of a noob doubt here (another one from me 😛 )
Regarding a book I’m reading:
- One Abstract Base-Class(ABC) is so by the declaration of a pure virtual method.
- What makes a method a pure virtual method is the
= 0at the end of the declaration. - You can declare one as this:
virtual void f() = 0;This way you don’t need to define it in the ABC, but it’s mandatory for the derived classes to implement its own versions of it. - But you can also declare a virtual method like this:
void f() = 0;This way you’d need to define the method in the ABC, and the derived classes could use it like that. This approach is needed for ABCs whose methods are all commonly applied by its derived classes.
If all the points above are true, what I don’t understand is: What is exactly a virtual method?
Because void f() = 0; looks like a regular method to me. The ABC would define it, and the derived objects would use the ABC’s definition, just like any other public method. Is there a definition of virtual method that is compelling for these two instances?
A pure virtual function or pure virtual method is a virtual function
that is required to be implemented by a derived class that is not
abstract
This definition is picked from another question from this site. So my guess is the book I’m reading is wrong? void f() = 0; states the class is an ABC but it’s not a pure virtual method. Could somebody confirm this or explain it to me?
Is wrong unless a base class defines
f()asvirtual. For example:The above is legal because
f()is inherentlyvirtual. Otherwise, no.tl;dr you can’t declare a non-virtual method as pure, but a method can be virtual without explicitly specifying the
virtualkeyword (via inheritance). I hope this is what the book is talking about, otherwise I’m inclined to say it’s acrappybad book.