---
title: "Virtual destructor with example"
subject: "Object Oriented Programming"
chapter: 7
key: "semester_3/object_oriented_programming:virtual-destructor"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/virtual-destructor
site: BCT Engineering
---

# Virtual destructor with example

Object Oriented Programming · Chapter 7: Polymorphism and Dynamic Binding

- Asked in: 2072 Chaitra, 4 marks
- Video: [Virtual Destructor In C++](https://www.youtube.com/watch?v=kDHiOLC5sQo) — CppNuts
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/virtual-destructor
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-7

## Question

Why do you need Virtual Destructor? Explain with example.

## Short answer

If a derived object is deleted through a base pointer without a virtual destructor, only the base destructor runs, laking resources. A virtual destructor ensures the derived destructor executes first, then the base's, preventing memory leaks. Example: virtual ~Base(){}.

## Answer

A **virtual destructor** is a destructor declared with the keyword `virtual` in the base class.

#### Need
When a derived class object is deleted through a **base class pointer**, and the base destructor is **not** virtual, only the base destructor is called. The derived destructor is skipped, so resources acquired by the derived class (memory, files) are never released, causing a memory leak and undefined behaviour. Making the base destructor virtual ensures the **derived destructor runs first, then the base destructor**, in the correct order.

#### Without virtual destructor

```cpp
#include <iostream>
using namespace std;

class Base {
public:
    ~Base() { cout << "Base destructor" << endl; }
};
class Derived : public Base {
    int *p;
public:
    Derived() { p = new int[100]; }
    ~Derived() { delete[] p; cout << "Derived destructor" << endl; }
};

int main() {
    Base *b = new Derived;
    delete b;        // only Base destructor runs: memory leak
    return 0;
}
```
**Output:** `Base destructor`

#### With virtual destructor

```cpp
#include <iostream>
using namespace std;

class Base {
public:
    virtual ~Base() { cout << "Base destructor" << endl; }
};
class Derived : public Base {
    int *p;
public:
    Derived() { p = new int[100]; }
    ~Derived() { delete[] p; cout << "Derived destructor" << endl; }
};

int main() {
    Base *b = new Derived;
    delete b;        // both destructors run
    return 0;
}
```
**Output:**

```text
Derived destructor
Base destructor
```

**Rule:** any class meant to be used as a base class with virtual functions should have a virtual destructor. Constructors cannot be virtual.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What happens on delete b, where b is Base* to a Derived object and Base destructor is non-virtual?
   A. Both run, Base first
   B. A compile error occurs
   C. Only the Derived destructor runs
   D. Only the Base destructor runs, so Derived resources leak
   Answer: D. Without virtual, the call binds to the pointer type, skipping the derived destructor. (Key point: Problem)

2. With a virtual base destructor, in what order do destructors run?
   A. Base then Derived
   B. Derived then Base
   C. Only Derived
   D. Random order
   Answer: B. The derived destructor runs first, then the base destructor. (Key point: Order)

3. Which statement is true according to the answer?
   A. Constructors can be virtual
   B. Constructors cannot be virtual, but destructors can
   C. Neither can be virtual
   D. Only constructors can be virtual
   Answer: B. Constructors cannot be virtual; base classes with virtual functions should have virtual destructors. (Key point: Rule)

## Related in Object Oriented Programming

- [Need of virtual function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-function-need) — Object Oriented Programming, chapter 7, asked 4×
- [Need of virtual base class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-base-class) — Object Oriented Programming, chapter 6

## Related in other subjects

- [Virtual short circuit and inverting integrator](https://www.bctengineer.com/question.md?key=semester_2%2Fbasic_electronics_engineering%3Avirtual-short-integrator) — Basic Electronics Engineering, chapter 4
- [Virtual memory management; thrashing](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Avirtual-memory-thrashing) — Operating System, chapter 4
- [Human need and Herzberg's hygiene theory](https://www.bctengineer.com/question.md?key=semester_7%2Forganization_and_management%3Aherzberg-human-need) — Organization and Management, chapter 8, asked 2×
- [Election algorithm need and example](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Aelection-scenario) — Distributed System, chapter 6, asked 2×
