Making the Destructors Virtual - C++ Class

C++ examples for Class:Destructor

Description

Making the Destructors Virtual

Demo Code

                                                                              
#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;
}

Result


Related Tutorials