views:

629

answers:

8

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?

+4  A: 

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.

PoweRoy
+3  A: 

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?

Vinzz
+7  A: 

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.

Naveen
A: 

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

Clement Herreman
Wrong. The second one MUST have an implementation in the declaring (base) class. And the first one MAY or MAY NOT have an implementation in the declaring class. Your statements about subclasses are correct however.
Ben Voigt
You're right, I edit =)
Clement Herreman
+1  A: 

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.

Paolo Tedesco
pure virtual functions CAN be implemented.
Ben Voigt
+8  A: 
MadKeithV
+1 for a very nice and complete answer which also sets straight the terminology.
sbi
+1  A: 

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.

Red
A: 

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.

larsm