Topic: Virtual destructors

The article at http://www.programmerinterview.com/inde … structors/ asks the question, " In C++, what’s a virtual destructor and when is it needed?".  It goes on the answer it with the help of short example.  Following is a full working example:

// define and first 2 includes required for _CrtDumpMemoryLeaks()
#define _CRTDBG_MAP_ALLOC // this and the following 2 includes required for 
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
#include <string.h>
using namespace std;

class person
{
private:
    char* name;
public:
    person(char* nm) {
        cout << "person constructor called" << endl;
        name = (char*)malloc(100);
        strcpy_s(name, 100, nm);
    };
    ~person() {
    //virtual ~person() { // add virtual keyword to avoid memory leak
        cout << "person destructor called" << endl;
        free(name);
    }
    void print_name() {
        cout << name << endl;
    }
};
class employee: public person
{
private:
    char* title;
public:
    employee(char* nm, char* ti) :person(nm) {
        cout << "employee constructor called" << endl;
        title = (char*)malloc(50);
        strcpy_s(title, 50, ti);
    };
    ~employee() {
        cout << "employee destructor called" << endl;
        free(title);
    }
    void print_title_name() {
        cout << title << " " << endl;
    }
};
int testEmployee() {
    for (int i = 0; i < 1; i++) {
        employee* emp = new employee("John Smith", "programmer");
        person* pers = emp;
        delete(pers);
    }
    _CrtDumpMemoryLeaks(); // check for memory leaks
    return 0;
}
int main() {
    testEmployee();
}

As is, the program outputs the following to the console:

person constructor called
employee constructor called
person destructor called

Also, the following is in the output to the output window:

Detected memory leaks!
Dumping objects ->
c:\users\bdavi\onedrive\documents\visual studio 2017\projects\whiteboard\whiteboard\employee.cpp(35) : {150} normal block at 0x00184A80, 50 bytes long.
 Data: <programmer      > 70 72 6F 67 72 61 6D 6D 65 72 00 FE FE FE FE FE 
Object dump complete.

If the virtual keyword is added to the destructor for the base class (person), however, the following is output to the console:

person constructor called
employee constructor called
employee destructor called
person destructor called

In this case, no message regarding a memory leak is output to the output window.  Hence, without the virtual keyword, the employee destructor is not called and, as a result, the memory for the employee's title (programmer) is never freed.  This shows why the virtual destructor is required in the base class.