242

What is the difference between a pure virtual function and a virtual function?

I know "Pure Virtual Function is a Virtual function with no body", but what does this mean and what is actually done by the line below:

virtual void virtualfunctioname() = 0
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Sachin
  • 20,805
  • 32
  • 86
  • 99

4 Answers4

298

A virtual function makes its class a polymorphic base class. Derived classes can override virtual functions. Virtual functions called through base class pointers/references will be resolved at run-time. That is, the dynamic type of the object is used instead of its static type:

 Derived d;
 Base& rb = d;
 // if Base::f() is virtual and Derived overrides it, Derived::f() will be called
 rb.f();  

A pure virtual function is a virtual function whose declaration ends in =0:

class Base {
  // ...
  virtual void f() = 0;
  // ...

A pure virtual function implicitly makes the class it is defined for abstract (unlike in Java where you have a keyword to explicitly declare the class abstract). Abstract classes cannot be instantiated. Derived classes need to override/implement all inherited pure virtual functions. If they do not, they too will become abstract.

An interesting 'feature' of C++ is that a class can define a pure virtual function that has an implementation. (What that's good for is debatable.)


Note that C++11 brought a new use for the delete and default keywords which looks similar to the syntax of pure virtual functions:

my_class(my_class const &) = delete;
my_class& operator=(const my_class&) = default;

See this question and this one for more info on this use of delete and default.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
  • 4
    Oh, so a pure virtual function is almost the same thing as a method in Java/C# interfaces. Neat. – Nicholas Miller Mar 03 '15 at 19:52
  • 6
    @Nick: Indeed. Only that C++ gives you more freedom about it. (You can implement pure virtual functions in C++ and you can mix pure, non-pure, and non-virtual functions in the same class. (Usually it's a good idea to not to do that, but if you need it, you can.) – sbi Mar 03 '15 at 21:50
  • @Sbi I think the surmising point of "use =0 for pure virtual functions" is worth leaving in. Perhaps even just adding to the end "In short, if you want pure virtual function, use `=0`" – thecoshman May 21 '15 at 09:42
  • hehe interesting "feature" ...or "bug" ? after all it can be one or the other its interchangeable – Xsmael Nov 19 '15 at 19:38
  • @Xsmael: Stroustrup declares it a feature that you can do this with any pure virtual function. (It is _necessary_ for pure virtual dtors, and Stroustrup has said on numerous occasions that he isn't fond of syntactical restrictions.) And, no, a function can be both pure virtual _and_ implemented. I'll add a link to a question where this is debated. – sbi Jan 08 '16 at 09:28
43

For a virtual function you need to provide implementation in the base class. However derived class can override this implementation with its own implementation. Normally , for pure virtual functions implementation is not provided. You can make a function pure virtual with =0 at the end of function declaration. Also, a class containing a pure virtual function is abstract i.e. you can not create a object of this class.

Naveen
  • 74,600
  • 47
  • 176
  • 233
25

A pure virtual function is usually not (but can be) implemented in a base class and must be implemented in a leaf subclass.

You denote that fact by appending the "= 0" to the declaration, like this:

class AbstractBase
{
    virtual void PureVirtualFunction() = 0;
}

Then you cannot declare and instantiate a subclass without it implementing the pure virtual function:

class Derived : public AbstractBase
{
    virtual void PureVirtualFunction() override { }
}

By adding the override keyword, the compiler will ensure that there is a base class virtual function with the same signature.

Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
10

You can actually provide implementations of pure virtual functions in C++. The only difference is all pure virtual functions must be implemented by derived classes before the class can be instantiated.

Community
  • 1
  • 1
AshleysBrain
  • 22,335
  • 15
  • 88
  • 124