The difference of virtual function and pure virtual function

A tough concept question I was faced in a revision C++ class was the question of the difference between a virtual function and pure virtual function. A virtual function is a function that can be overridden within an inheriting class by a function with the same name. A pure virtual function is required to be implemented by a derived class, that is not abstract.

The virtual keyword gives C++ it’s ability to support polymorphism. Pure virtual functions are used for

  • Abstract classes which these are base classes where you have to derive from them and then implement the pure virtual functions
  • Interfaces which consist of empty classes where all functions are pure virtual, hence you have to derive and then implement all of the functions

So in simple English – The virtual function can be overriden and a pure virtual function must be implemented.

Sample of a pure virtual function. The “= 0” portion of a pure virtual function is also known as the pure specifier, because it’s what makes a pure virtual function “pure”.

</pre>
<pre>class A {
public:
   virtual void pure_virtual() = 0;  // a pure virtual function
   // note that there is no function body	 
};</pre>
<pre>
Share this post