Table of contents
1.
Introduction
2.
Destructor
2.1.
Syntax
2.2.
Example
3.
Pure Virtual Destructor 
3.1.
Syntax
3.2.
Example
4.
Need for Pure virtual destructor
5.
Need of function body for pure virtual function
5.1.
Example
5.2.
Output
6.
Sample Code
7.
Output
8.
Frequently Asked Questions
8.1.
What are Constructors in C++?
8.2.
What is Destructor in C++?
8.3.
Why is a function body necessary for a pure virtual function?
9.
Conclusion
Last Updated: Mar 27, 2024

Pure virtual destructor

Author Muskan Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We all are very aware of the concept of Virtual Destructors in c++. But can we have pure virtual destructors in C++? The answer is yes; You can have a pure virtual destructor. Pure virtual destructors are acceptable in standard C++, and one of the most crucial things to keep in mind is that a class must include a function body for any pure virtual destructor it contains.

Now let's learn about Pure Virtual Destructor more briefly.

Destructor

When an object leaves the scope of its member function or is destroyed explicitly by a call to delete, the destructor is automatically called. A tilde (~) is added before the class name in a destructor's name.

Syntax

~class_name ();

Example

class Ninjas {
public:
  // Constructor for class X
  Ninjas();
  // Destructor for class X
  ~Ninjas();
};

Pure Virtual Destructor 

A Pure Virtual Destructor is defined in a base class with a =0; statement and its body is declared outside of the class. Although there are several use cases for virtual destructors, the C++ standard prefers pure virtual destructors, typically used in libraries (static or dynamic DLL).

Syntax

virtual ~class_name ()=0

Example

class MyNinjas{
   public:
   virtual ~MyNinjas() // Virtual Destructor
   {
   };
 
};
 

Need for Pure virtual destructor

The inclusion of a pure virtual destructor in a C++ program has no negative consequences. Pure virtual destructors must have a function body because their destructors are called before those of base classes; if one is absent, object destruction will fail since there won't be anything to call when the object is destroyed. Making a pure virtual destructor with its definition allows us to create an abstract class easily.

Need of function body for pure virtual function

Destructors are always called in the opposite order of the class derivation, as opposed to other functions, for this reason: they are not genuinely "overridden" like other functions. The base class destructor will then be called once a derived class destructor has been called, according to this. What function body will be contacted during object destruction if the definition of the pure virtual destructor is not given? The existence of a function body for pure virtual destructors is thereby enforced by the compiler and linker.

Example

// A pure virtual destructor is demonstrated in a C++ program.
#include <iostream>
using namespace std;
 
// CodingNinjas class initialization
class CodingNinjas {
public:
    virtual ~CodingNinjas() = 0;
    // Pure virtual destructor
};
 
// Ninjas class initialization
class Ninjas : public CodingNinjas {
public:
    ~Ninjas() { cout << "~Ninjas() is executed"; }
};
 
// Driver Code
int main()
{
    // a base class pointer that provides derived function objects with new storage
    CodingNinjas* b = new Ninjas();
    delete b;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code


So, this error will be generated because of the linker.

Output

CodingNinjas.cpp:(.text._ZN6NinjasD2Ev[_ZN6NinjasD5Ev]+0x39): undefined reference to `CodingNinjas::~CodingNinjas()'
collect2: error: ld returned 1 exit status

The program now compiles and executes without any issues if the definition for the pure virtual destructor is given.

Sample Code

// C++ program to show that the program compiles
//and runs without error when the value of a pure virtual destructor is provided.
#include <iostream>
 
//  CodingNinjas class Initialization
class CodingNinjas {
public:
    virtual ~CodingNinjas() = 0; // Pure virtual destructor
};
CodingNinjas::~ CodingNinjas() // Explicit destructor call
{
    std::cout << "CodingNinjas is called";
}
 
 
//  Ninjas class Initialization
class Ninjas : public CodingNinjas {
public:
    ~Ninjas() { std::cout << "Ninjas() is executed\n"; }
};
 
int main()
{
    // Ninjas member function called
    CodingNinjas* b = new Ninjas();
    delete b;
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

~Ninjas() is executed
CodingNinjas is called //pure virtual destructor is called.

Frequently Asked Questions

What are Constructors in C++?

When an object of a class is created in C++, a unique method called a function is automatically invoked. This is called a Constructor. 

What is Destructor in C++?

When an object leaves the scope of its member function or is destroyed explicitly by a call to delete, the destructor is automatically called. 

Why is a function body necessary for a pure virtual function?

The reason is that destructors are always called in the opposite order of the class derivation, unlike other functions, which are genuinely "overridden." This indicates that a derived class destructor will be called before the base class destructor.

Conclusion

This blog has extensively discussed Pure Virtual Destructor in C++, Its need, and the sample code. We hope this blog has helped you learn about pure Virtual Destructor in C++If you want to learn more, check out the excellent content on the Coding Ninjas Website:

Virtual Copy Constructor, Constructors and Destructors, CPP Abstract ClassClasses and Objects in C++Virtual Functions.

Explore our Library on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsMachine LearningDeep LearningCloud Computing, and many more! If you want to test your competency in coding, check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

 

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass