Virtual Destructors

Hard Virtual Functions & Abstract Classes PRO
🔒 Login to Unlock

Problem Statement

Create a `Base` class with a virtual destructor printing ‘Base ~’. Create a `Derived` class overriding the destructor to print ‘Derived ~’. In `main()`, dynamically allocate `Derived` using a `Base*` pointer (`Base* b = new Derived();`), then `delete b;` to trigger both destructors cleanly.

Input Format

None

Output Format

Print the cascade of destructor calls.

Constraints

None

Sample Output

Derived ~
Base ~

Explanation

If a base class pointer deletes a derived object, the base destructor MUST be virtual. Otherwise, the derived destructor never runs, causing massive memory leaks.

Starter Code

#include <iostream>

class Base {
public:
    // Implement virtual destructor
};

class Derived : public Base {
public:
    // Implement destructor
};

int main() {
    // Allocate and delete pointer
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used