C++ Class Destructor Making Destructors Virtual

Description

C++ Class Destructor Making Destructors Virtual

 
#include <iostream>
 
using namespace std;
 
class MyClass//from   www  .  jav a 2s  . c  o m
{
public:
    int Number;
    virtual ~MyClass();
};
 
class Derived : public MyClass
{
public:
    virtual ~Derived();
};
 
MyClass::~MyClass()
{
    cout << "MyClass::~MyClass()" << endl;
}
 
Derived::~Derived()
{
    cout << "Derived::~Derived()" << endl;
}
 
void ProcessAndDelete(MyClass *inst)
{
    cout << inst->Number << endl;
    delete inst;
}
 
int main()
{
    MyClass *MyObject = new(Derived);
    MyObject->Number = 10;
    ProcessAndDelete(MyObject);
    return 0;
}



PreviousNext

Related