Difference between Virtual function and Pure virtual function in C++

SHARE

What is a virtual function?

A virtual function is a member function that is declared within a base class and redefined by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function. Virtual functions ensure that the correct function is called for an object, regardless of the expression used to make the function call.

Virtual functions are resolved ‘late’. If the function in question is ‘virtual’ in the base class, the most-derived class’s implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. If it is not ‘virtual’, the method is resolved ‘early’ and selected according to the declared type of the pointer or reference.

This concept is an important part of the (runtime) polymorphism portion of object-oriented programming (OOP). In short, a virtual function defines a target function to be executed, but the target might not be known at compile time.

What you need to know about virtual functional

  • A virtual function is a member function in a base class that can be redefined in a derived class.
  • Virtual function’ has their definition in the base class.
  • All derived classes may or may not override the virtual function of the base class.
  • If the derived class fails to redefine (override) the virtual function it can use the virtual function of the base class.
  • Base class containing the virtual function can be instantiated i.e. its object can be created.
  • Classes having virtual functions are not abstract.
  • The resolving of function call is done at runtime.
  • It is used to tell the compiler to perform dynamic linkage or late binding on the function.

What is a pure virtual function?

A pure virtual function is a virtual function that has no definition within the class. Its definition lies only in the derived class i.e it is compulsory for the derived class to provide definition of a pure virtual function. In other words, it can be considered as an empty function meaning that the pure virtual function does not have any definition relative to the base class.

A class having pure virtual function cannot be used to create direct objects of its own. It means that the class is containing any pure virtual function then we cannot create the object of that class. This type of class is known as an abstract class. Programmers need to redefine the pure virtual function in the derived class as it has no definition in the base class.

What you need to know about pure virtual function

  • A pure virtual function is a member function in a base class whose declaration is provided in a base class and implemented in a derived class.
  • Pure Virtual Function has no definition in the base class.
  • All derived classes must override the virtual function of the base class.
  • If the derived class does not define the pure virtual function; it will not throw any error but the derived class becomes an abstract class.
  • The base class containing pure virtual function i.e. an abstract class can not be instantiated as an abstract class is not fully defined.
  • Base class containing pure virtual function becomes abstract.
  • Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface.