What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

What are Pure Virtual Functions in C++? (with Example)

  • May 15, 2023
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Anubhav Agarwal
What are Pure Virtual Functions in C++? (with Example)

Let's break our main topic into bits.  Look at this equation: Pure Virtual Functions = Pure + Virtual + functions. Following from right, we know about functions, and "Virtual" means virtual keyword which we use to achieve late binding as we implement it in virtual functions. In this article, we will look actually what are pure virtual functions in C++ with examples and advantages.

What is a Pure Virtual Function in C++?

A pure virtual function in C++ is a function that is declared in the base class but you cannot implement it, with a '0' assigned to make them pure. In this way, the base class becomes an abstract class, and it must be inherited by a derived class, which provides an implementation for it. 

Basically, they have the following characteristics:

  • These functions also must have a prefix before the function's name called 'virtual'.
  • In the base class, you can declare it, but you cannot implement it.
  • '0' must be assigned to the function to make them pure. 
  • The derived class must provide the implementation code for this function, else this derived class is also termed an 'abstract class'.

Note that classes having at least one pure virtual function are called Abstract classes. Now we know theoretically, what are important points to keep in mind about the pure virtual functions to define, then let's just quickly have a look at its syntax as well. 

Given below is the way to declare the pure virtual functions in C++ :

virtual function_type function_name() = 0;

 

The main use of pure virtual functions is to create an abstract class that defines an interface for its derived classes. An abstract class is like a base class for other classes to provide a common interface to implement. This helps us use the polymorphism feature of the programming language.

Pure Virtual Function C++ Example

Since we already understand the declaration part of a pure virtual function, now let's see how to code it in C++ and how to provide its implementation in the derived class. Given below can be the best example :

//Welcome to FavTutor
#include<iostream> using namespace std; // here is Abstract class class Shape { public: virtual int cal_Area() = 0; // cal_Area is a pure virtual function }; class Square : public Shape { int a; public: Square(int l) { a = l; } int cal_Area() { return a*a; // returns area of square } }; class Circle : public Shape { int r; public: Circle(int x) { r = x; } int cal_Area() { return 3.14*r*r ; } }; class Rectangle : public Shape { int l; int b; public: Rectangle(int x, int y) { l=x; b=y; } int cal_Area() { return l*b; // returns the product of length and breadth } }; int main() // main function { Shape *shape; Square s(3.4); Rectangle r(5,6); Circle c(7.8); shape =&s; int a1 =shape->cal_Area(); shape = &r; int a2 = shape->cal_Area(); shape = &c; int a3 = shape->cal_Area(); std::cout << "The area of square is: " <<a1<< std::endl; std::cout << "The area of rectangle is: " <<a2<< std::endl; std::cout << "The area of circle is: " <<a3<< std::endl; return 0; }

 

Output:

The area of square is: 11
The area of  rectangle is: 30
The area of  circle is: 191

 

After going through the above, you can observe that we define a shape class as a base, then we are deriving classes from it and using the member function of the base class to calculate the area, the important thing to note here is that we are efficiently using the pure virtual function cal_area.

But how? See we are not using multiple functions in every derived class to calculate the area rather we are using just the different implementations of it by defining it once in the base class, I hope this will put you in a state where you have found yourself more powerful as you have this craziest tool to use it in your C++ program now. 

Virtual Function vs Pure Virtual Function

The following are the differences between a virtual and a pure virtual function in C++. 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:

virtual<function_type><function_name>()
{
    // code
}

 

Syntax of pure virtual function:

virtual<function_type><function_name>()
    = 0;

 

Look at the below table for a complete list of differences:

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

 

Advantages of Pure Virtual Functions

Let's now look at the key advantages of pure virtual functions in C++ one by one:

  1. Abstraction: Pure virtual functions are a way to separate the interface from the implementation, to make the code easier to maintain.
  2. Polymorphism: A base class pointer is used to call functions of its derived classes, a key way to use polymorphism in C++.
  3. Reusability: Since we define a common interface, we reduce the amount of code and make it more reusable.

Overall, they are one way to use the object-oriented programming features of C++ and make the code more modular and maintainable.

Conclusion 

Hope this article clears your concepts of pure virtual functions in C++ and more specifically, why it is pure. Now you can also use it in your C++ program too instead of creating multiple different functions in every other derived class, define it only once in the base class and create your own implementations. Happy Learning :)

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Anubhav Agarwal
I'm a research-oriented engaged in various technologies and a technical content writer. Being a coder myself, going through the documentation to provide optimized solutions as technical content is what I always look for.