What\'s the difference between the following two declarations?
virtual void calculateBase() = 0;
virtual void calculateBase();
I read the fir
The first one doesn't have to be implemented in the base class, but enforces it to be implemented in inherited classes.
You have to implement the second one in the base class and it can be implemented in inherited classes.
I think you are mixing terms...
virtual void x() = 0;
is a pure virtual function, or abstract function. It is a virtual function without implementation. You talk about a pure abstract class, which is the c++ equivalent of an interface, about a class having only abstract functions.
virtual void x();
is a virtual function, meaning it can be redefined in derived classes, but it's not abstract, so you must provide an implementation for it.
virtual void calculateBase() = 0;
The first function is Pure virtual function where the implementation can not be done in the class and it act as pure abstract class or Interface class. The concrete implementation should be done or overriden in subclass. It act as interface class to its derived classes.
virtual void calculateBase();
This function is virtual function where the implementation (default) can be done in the base class. But, the derived class must be overriden its own imeplementation.
The first one is a "pure virtual" - it will make the class abstract, attempting to instantiate it will result in compiler errors. It is meant to be used as a base class where the derived class implements the necessary behaviour by implementing the pure virtual function. You do not have to implement the function in the base class, though you can.
This is a pattern often used for two design patterns:
The second declaration is just an ordinary virtual member function declaration. You will get compiler errors if you fail to implement the member function in the base class. It is still virtual which implies that it may be useful to override the behaviour in a derived class.
The second function must have an implementation in the class that declare it (lack of '= 0'), and can be overriden by subClasses.
The first function may or not have an implementation in the class that declare it, and has to be implemented by subClasses
First one is called a pure virtual function. Normally pure virtual functions will not have any implementation and you can not create a instance of a class containing a pure virtual function.
Second one is a virtual function (i.e. a 'normal' virtual function). A class provides the implementation for this function, but its derived class can override this implementation by providing its own implementation for this method.