What is Meant by Pure Virtual Function in C++?
Introduction
A pure virtual function in c++ is a virtual function for which we do not have an implementation. We do not write any functionality in it. Instead, we only declare this function. A pure virtual function does not carry any definition related to its base class. A pure virtual function is declared by assigning a zero (0) in its declaration. Any class containing one or more pure virtual functions can not be used to define any object. For this reason, these classes are known as abstract classes. Classes derived from abstract classes need to implement the pure virtual functions of these classes.
Syntax
The syntax of the pure virtual function is as follows:
We need to write "virtual" to create a virtual function. After that, we declare the type of function, and then the function's name is written. Since it is a pure virtual function, we must end this function by assigning the value '0' zero to the function.
Characteristics of Pure Virtual Function in C++
The following are the characteristics of a pure virtual function in c++:
- A pure virtual function does not do anything, which means it is used to resemble the template, and the derived classes implement the function.
- It is an empty function because it does not contain any definition of the functionality of its base class in it.
- Derived class can call a member or pure virtual function in c++.
- The user in the derived class redefines a pure virtual function.
- Any class in c++ that contains a pure virtual function does not allow the user to create the object of that class.
Examples
Let us see an example to understand how to implement a pure virtual function in c++:
Output:
Explanation:
In the above program, we have first defined a class Shape ( an abstract class ). This class contains a pure virtual function cal_area() that calculates the area of any shape ( class ) which will inherit this base class. Three classes, Square, Circle, and Rectangle, have inherited Shape as a base class. So, all these three classes need to implement the pure virtual function cal_area() defined in the base class. So, when we created the class ( Square / Circle / Rectangle ), the class implemented the pure virtual function and defined the function's behavior according to its properties and shape. So, the Circle class calculated and returned the area by defining its own formula; on the other hand, the Square class calculated and returned its area according to the formula of the area of the square.
Similarities between Virtual Function and Pure Virtual Function in C++
Although. Virtual and pure virtual functions in c++ are two different things, but there are some similarities between them. Let us have a look into this.
- Both virtual function and pure virtual function belong to Run-time polymorphism in C++.
- The declaration of both types of functions does not change for the whole life of the program.
- These functions do not belong to a global or static function.
Differences between Virtual Function and Pure Virtual Function in C++
The following are the differences between a virtual and a pure virtual function in c++. Let us have a look into this.
- A virtual function and a pure virtual function are both declared with the word "virtual" at the beginning of the code declaration, but their syntax is different.
syntax of virtual function:
syntax of pure virtual function:
Virtual Function | Pure Virtual Function |
---|---|
In the virtual function, the derived class overrides the function of the base class; it is the case of function overriding | In a pure virtual function, the derived call would not call the base class function as it has not defined instead it calls the derived function which implements that same pure virtual function in the derived call. |
Class containing virtual function may or may not be an Abstract class. | If there is any pure virtual function in a class, then it becomes an "Abstract class". |
Virtual function in the base does not enforce to derived for defining or redefining | In pure virtual function, the derived class must redefine the pure virtual class of the base class. Otherwise, that derived class will become abstract as well. |
Learn More
You can learn more about the given topics that are related to pure virtual functions in c++:
Conclusion
- A pure virtual function in c++ is defined as a function that is only initialized but not defined.
- A pure virtual function in c++ must end with " =0 " when declared.
- This type of function is a concept of Run-time Polymorphism.
- This type of function needs to be redefined in the derived class.
- A pure virtual function class had to be an Abstract class.
Ready to amplify your coding skills? Join our Free C++ course tailored by industry experts and become a certified C++ pro!