What is the Difference Between Virtual Function and Pure Virtual Function

The main difference between Virtual Function and Pure Virtual Function is that the virtual function is a function in the base class that is declared using the virtual keyword while the pure virtual function is a virtual function in the base class without a function definition.

Polymorphism is an important OOP concept. It allows an object to take many forms. There are two types of polymorphism called early binding and late binding.  C++ is a programming language that supports OOP. Therefore, the programmer can implement polymorphism concepts using C++.

Key Areas Covered

1. What is Virtual Function
       – Definition,Functionality
2. What is Pure Virtual Function
       – Definition,Functionality
3. Difference Between Virtual Function and Pure Virtual Function
      – Comparison of Key Differences

Key Terms

OOP, Pure Virtual Function, Virtual Function

Difference Between Virtual Function and Pure Virtual Function - Comparison Summary

What is Virtual Function                 

A virtual function is a function in the base class with the virtual keyword. It helps to indicate the compiler that there should not be static linkage when the subclass also has the same method with some other implementation.  It is easier to understand this concept by referring the below program.

Difference Between Virtual Function and Pure Virtual Function

Figure 1 : C++ program

In the above program, Class A has a public method called display. Class B inherits class A. It has a method called display. Both methods have the same name, but they have different implementations. In the main method, ‘a’ is a pointer while ‘b’ is an object of type B. Then ‘a’ is assigned with the address of b. Finally, the programmer can call the display method using ‘a’.  Here, we can observe that the output shows the display method of class A.  Even though the programmer assigned the address of ‘b’ to ‘a’ (b = &a), it still prints the display method of class A. It happens due to static linkage. In other words, the function call is fixed before executing the program. The display function is set during the compilation. We also call this early binding.

In order to make the compiler identify the type of the object at run time and to bind the function call, the programmer can use the keyword “virtual” with the base class. To achieve that, he can do a small modification to the above program. That is to add the virtual keyword at the front of the base class function as follows.

Main Difference - Virtual Function  vs Pure Virtual Function

Figure 2: C++ program with virtual function

Now the display function in class A is a virtual function. We can observe that the output shows the display method of class B.  The preference is given to type B.  In other words, the function is called depending on the object. Hence, this is dynamic binding or late binding.

 What is Pure Virtual Function

A virtual function does not perform a task. A function without a definition or an implementation is a do nothing function. Thus, this kind of function is a pure virtual function.  Furthermore, it is not possible to declare an object of a class that has a pure virtual function.  Those classes are called an abstract base class.  

Difference Between Virtual Function and Pure Virtual Function_Figure 3

Figure 3: C++ program with pure virtual function

The subclass has to provide the definition to the virtual function of the base class. There is no need for a function definition for the display function in class A. Therefore, it has no function definition. The =0 indicates the compiler that the function has no definition. Therefore, the display function in class A is called a pure virtual function.  As class A has a pure virtual function, it is called an abstract base class.

Difference Between Virtual Function and Pure Virtual Function

Definition

A virtual function is a member function declared within the base class that can be redefined or overridden by a derived class while pure virtual function is a virtual function in the base class with no implementation. Thus, this is the main difference between virtual function and pure virtual function.

Function Definition

Moreover, the function definition is a major difference between virtual function and pure virtual function. In virtual functions, the base class has the function definition. However, in pure virtual functions, the base class does not have the function definition. It only has the function declaration. 

Derived Class

Also, another difference between virtual function and pure virtual function is that it is not necessary for all derived classes to override the virtual function of the base class.  However, the derived classes should override the pure virtual function of the base class.

Abstract Class

Furthermore, there is no abstract class concept in virtual functions. If there is at least one pure virtual function, that class is called an abstract class. 

Outcome

A derived class that does not override a virtual function of the base class does not cause any compilation errors. However, a derived class that does not override a pure virtual function of the base class cause compilation errors. Hence, this is also a difference between virtual function and pure virtual function.

Conclusion

A programmer can write virtual functions in C++. A pure virtual function is a special virtual function. The main difference between virtual function and pure virtual function is that the virtual function is a function in the base class that is declared using the virtual keyword while the pure virtual function is a virtual function in the base class without a function definition.

Reference:

1.Codearchery. Virtual Function in C -50, Codearchery, 22 Mar. 2017, Available here.
2.“C Virtual Function – Javatpoint.” Www.javatpoint.com, Available here.
3.“Virtual Function in C .” GeeksforGeeks, 17 Sept. 2018, Available here.
4.“Pure Virtual Functions and Abstract Classes in C .” GeeksforGeeks, 11 Sept. 2018, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply