C++ examples for Class:Destructor
Making the Destructors Virtual
#include <iostream> using namespace std; class MyClass// ww w. j a va 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; }