Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pure virtual destructor in C++
A pure virtual function is a function that has no implementation in the base class and must be overridden by any derived class. It is declared using = 0 in the base class.
Pure Virtual Destructor
When we want the base class to be abstract then we declare a pure virtual Destructor. A pure virtual Destructor can be declared in C++ after a destructor has been created as a pure virtual object.
One of the most important things is that if a class contains a pure virtual destructor, it must provide a function body for the pure virtual destructor.
Why Use a Pure Virtual Destructor?
- To make a class abstract (cannot create objects of it directly).
- To ensure proper cleanup (calling derived class destructors correctly when deleting via a base class pointer).
- It enforces polymorphic destruction safely.
Syntax
Following is the syntax of the pure virtual destructor ?
class Base {
public:
virtual ~Base() = 0; // Pure virtual destructor
};
Base::~Base() {
// Provide a definition for the pure virtual destructor
}
If the destructor is purely virtual, we must define it outside of the class. Otherwise, the linker will throw an error.
Example
The following code demonstrates the use of a pure virtual destructor in C++ ?
#include <iostream>
using namespace std;
class Base {
public:
// Pure virtual destructor
virtual ~Base() = 0;
};
Base::~Base() {
cout << "Base destructor called" << endl;
}
class Derived : public Base {
public:
~Derived() {
cout << "Derived destructor called" << endl;
}
};
int main() {
Base* obj = new Derived();
delete obj; // calls Derived's destructor first, then Base's
return 0;
}
The above code generated the following output ?
Derived destructor called Base destructor called
Example: Managing Employees
In this example, we create an employee management system. Suppose you're designing a system that manages different types of employees: full-time, part-time, contract workers, etc.
#include <iostream>
#include <string>
using namespace std;
class Employee {
// Pure virtual function
public: virtual void showDetails() const = 0;
// Pure virtual destructor
virtual~Employee() = 0;
};
Employee::~Employee() {
cout << "Employee destructor called" << endl;
}
class FullTimeEmployee: public Employee {
string name;
public:
FullTimeEmployee(const string & n): name(n) {}
void showDetails() const override {
cout << "Full-Time Employee: " << name << endl;
}
~FullTimeEmployee() {
cout << "FullTimeEmployee destructor called" << endl;
}
};
class PartTimeEmployee: public Employee {
string name;
public:
PartTimeEmployee(const string & n): name(n) {}
void showDetails() const override {
cout << "Part-Time Employee: " << name << endl;
}
~PartTimeEmployee() {
cout << "PartTimeEmployee destructor called" << endl;
}
};
int main() {
Employee * e1 = new FullTimeEmployee("Aman");
Employee * e2 = new PartTimeEmployee("Vivek");
e1 -> showDetails();
e2 -> showDetails();
delete e1;
delete e2;
return 0;
}
Following is the output of the above code ?
Full-Time Employee: Aman Part-Time Employee: Vivek FullTimeEmployee destructor called Employee destructor called PartTimeEmployee destructor called Employee destructor called