// inherit19.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 // inherit18.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* ptr[2]; ptr[0] = new A("greetings from A"); ptr[1] = new B("greetings from B"); 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 */