virtual destructor methods : destructor « Class « C++ Tutorial






#include <iostream>
 
 class Animal
 {
 public:
     Animal():itsAge(1) { std::cout << "Animal constructor...\n"; }
     virtual ~Animal() { std::cout << "Animal destructor...\n"; }
     virtual void Speak() const { std::cout << "Animal speak!\n"; }
 protected:
     int itsAge;
 };
 
 class Cat: public Animal
 {
 public:
     Cat() { std::cout << "Cat constructor...\n"; }
     ~Cat() { std::cout << "Cat destructor...\n"; }
     void Speak()const { std::cout << "Meow!\n"; }
 };
 
 int main()
 {
     Animal *pCat = new Cat;
     pCat->Speak();
     return 0;
 }
Animal constructor...
Cat constructor...
Meow!








9.3.destructor
9.3.1.A simple constructor and destructor.
9.3.2.Close stream in destructor
9.3.3.virtual destructor methods
9.3.4.Demonstrating the order in which constructors and destructors are called.