// inherit18.cpp By: Aiman Hanna - ©1993-2005 Aiman Hanna // This program illustrates virtual destructors. The program does not actually // use virtual destructors; rather it illustrates when they are needed. See // inherit19.cpp & inherit20.cpp. #include #include #include "inherit18.h" A::A(char* s) { cout << "Constructing A object \n\n"; strA = new char[100]; strcpy(strA, s); }; void A::Display() { cout << "Info of class A is: " << strA << endl; } A::~A() { cout << "Destructing A, memory deletion will occur now" << endl; cout << "Deleting \"" << strA << "\"" << endl; delete []strA; } B::B(char* s) : A("Hello") { cout << "Constructing B object \n\n"; strB = new char[100]; strcpy(strB, s); }; void B::Display() { cout << "Info of class B is: " << strB << endl << endl; } B::~B() { cout << "Destructing B, memory deletion will occur now" << endl; cout << "Deleting \"" << strB << "\"" << endl; delete []strB; } int main() { A a1("greetings from A"); B b1("greetings from B"); A* ptr[2]; ptr[0] = &a1; ptr[1] = &b1; ptr[0] -> Display(); ptr[1] -> Display(); return 0; } // Compilation Successful /* Output Constructing A object Constructing A object Constructing B object Info of class A is: greetings from A Info of class B is: greetings from B Destructing B, memory deletion will occur now Deleting "greetings from B" Destructing A, memory deletion will occur now Deleting "Hello" Destructing A, memory deletion will occur now Deleting "greetings from A" */