Question

What's the difference between the following two declarations?

virtual void calculateBase() = 0;  
virtual void calculateBase();

I read the first one (=0) is a "pure abstract function" but what does that make the second one?

Was it helpful?

Solution

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.

OTHER TIPS

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 "template method" design pattern, where the base class implements a structure around a function call, but the details of the function call must be filled in by the derived class, and
  • The "interface" design pattern, because C++ doesn't have the interface keyword. Abstract base classes, ideally with only pure virtual functions and no member data, are the C++ way of defining interfaces.

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

Basically, when inheriting, you are compelled to override the first one, and are allowed to override the second one.

Coming from Java, don't you?

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.

In Java

virtual void calculateBase() = 0;  

would be

abstract void calculateBase();

and

virtual void calculateBase();

would be

void calculateBase();

To be perfectly clear,

void calculateBase();

in C++, is "the same" as

final void calculateBase();

in Java. That is, final is "default" in C++. There is an exception to this rule however. When subclassing a class with a virtual method and reimplementing it without using the virtual keyword, the method in the subclass will not be final.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top