What is Meant by Pure Virtual Function in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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 FunctionPure Virtual Function
In the virtual function, the derived class overrides the function of the base class; it is the case of function overridingIn 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 redefiningIn 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!

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more