#include <iostream>
#include <string>

// Declare the BaseClass.
class BaseClass {
      public:
             
             // Constructor.
             BaseClass();
             
             // Destructor.
             ~BaseClass();
             
             // Simple method.
             void doSomething();
             
             };
             
             // Declare a SubClass that
             // inherits BaseClass.
             class SubClass : public BaseClass {
                   public:
                          
             // Constructor.
             SubClass();
                          
             // Destructor.
             ~SubClass();
             
             };
             
             // Define the functions.
             BaseClass::BaseClass() {
             std::cout << "Entering BaseClass::BaseClass()\n";
             
             // Do some initialization here.
             
             std::cout << "Leaving BaseClass::BaseClass()\n\n";
             }
             
             BaseClass::~BaseClass() {
             std::cout << "Entering BaseClass::~BaseClass()\n";
                                     
             // Do some cleanup here.
                                     
             std::cout << "Leaving BaseClass::~BaseClass()\n\n";
             }
             
             void BaseClass::doSomething() {
             std::cout << "Entering BaseClass::doSomething()\n";
                  
             // Do something here.
                  
             std::cout << "Leaving BaseClass::doSomething()\n\n";
             }
                  
                  
                  SubClass::SubClass() {
                  std::cout << "Entering SubClass::SubClass()\n";
                                       
                  // Do some initialization here.
                                       
                  std::cout << "Leaving SubClass::SubClass()\n\n";
                  }
                                       
                  SubClass::~SubClass() {
                  std::cout << "Entering SubClass::~SubClass()\n";
                  
                  // Do some cleanup here.
                  
                  std::cout << "Leaving SubClass::~SubClass()\n\n";
                  }
                  
                  // Start the main() function.
                  int main() {
                      
                      // Indicate where we're at.
                      std::cout << "Creating an instance of class SubClass...\n";
                      
                      // Create the object.
                      SubClass subclass;
                      
                      // Call the method.
                      std::cout << "Calling subclass.doSomething()...\n";
                      subclass.doSomething();
                      
                      //End of the program.
                      std::cout << "This is the second-to-last statement in main().\n";
                      std::cout << "Press Enter or Return to continue.";
                      std::cin.get();
                      return 0;
                      }

I don't understand why it doesn't show the destructors being called in the output? In the book I'm using for the above code it shows the constructors, destructors and methods being called in the output. But mine isn't showing the destructors for some reason?

First of all, any good C++ book should tell you that the destructor in a base class has to be marked 'virtual' (unless you are an expert and intentionally leave it non-virtual in the context of some advanced programming technique, like scope-guards).

Anyways, on my computer, it does as expected. The issue here is that the program "stops" (as in, it reaches the end of main()) and then the destructors are called. I guess under some operating systems or under some terminal/console/command-prompt the stuff printed after the end of main() don't get printed. It's not an issue with the code.

Here is a modified version that is certain to show you what you are longing to see (also notice the fixed identation, this is a very important habit to develop):

#include <iostream>
#include <string>

// Declare the BaseClass.
class BaseClass {
  public:
    // Constructor.
    BaseClass();
             
    // Destructor.
    virtual ~BaseClass(); //notice 'virtual' here.
             
    // Simple method.
    void doSomething();
             
};
             
// Declare a SubClass that
// inherits BaseClass.
class SubClass : public BaseClass {
  public:
    // Constructor.
    SubClass();
                          
    // Destructor.
    ~SubClass();
             
};
             
// Define the functions.
BaseClass::BaseClass() {
  std::cout << "Entering BaseClass::BaseClass()\n";
  // Do some initialization here.
  std::cout << "Leaving BaseClass::BaseClass()\n\n";
}
             
BaseClass::~BaseClass() { //notice that 'virtual' is not needed here (only in declaration).
  std::cout << "Entering BaseClass::~BaseClass()\n";
  // Do some cleanup here.
  std::cout << "Leaving BaseClass::~BaseClass()\n\n";
}
             
void BaseClass::doSomething() {
  std::cout << "Entering BaseClass::doSomething()\n";
  // Do something here.
  std::cout << "Leaving BaseClass::doSomething()\n\n";
}
                  
SubClass::SubClass() {
  std::cout << "Entering SubClass::SubClass()\n";
  // Do some initialization here.
  std::cout << "Leaving SubClass::SubClass()\n\n";
}
                                       
SubClass::~SubClass() {
  std::cout << "Entering SubClass::~SubClass()\n";
  // Do some cleanup here.
  std::cout << "Leaving SubClass::~SubClass()\n\n";
}
                  
// Start the main() function.
int main() {

  {     //<-----notice here, a new scope is started.
    // Indicate where we're at.
    std::cout << "Creating an instance of class SubClass...\n";
                      
    // Create the object.
    SubClass subclass;
                      
    // Call the method.
    std::cout << "Calling subclass.doSomething()...\n";
    subclass.doSomething();
                      
    //End of the program.
    std::cout << "This is the last statement in the inner scope.\n";
  };
  std::cout << "At this point, the subClass should have been deleted now. Press Enter or Return.";
  std::cin.get();
  return 0;
}

Did you press enter after you ran your code?

Thanks. I'm using Dev-C++ by the way. Your example is better than the example in my book lol. Yours and the example in my book are slightly different but I guess they pretty much do the same thing. I Can't really understand why yours works though and mine doesn't (in my book) In my book in the terminal window it says:

This is the second-to-last statement in main().
Press Enter or Return to continue.
Entering SubClass::~SubClass()
Leaving SubClass::~SubClass()

Entering BaseClass::~BaseClass()
Leaving Baseclass::~Baseclass()

:

Thanks a lot for your help though it's really appreciated!!

Did you press enter after you ran your code?

yes. I pressed enter and it seemed like some writing flashed up really quick but I didn't have chance to read it. I tried clearing the extraneous input too with std::cin.ignore(100, '\n'); after "This is the second-to-last statement in main().\n"; thinking that might work but it didn't.

>>flashed up really quick
This is simply due to the fact that you are using a windows-based IDE that pops up a console terminal and then shuts it down as soon as the program finishes executing. You need to get into a command-prompt from outside your IDE, go to your program's directory and execute the program manually. This way, the command-prompt won't disappear after the end of the program and you will be able to see what was last printed.

OR

Use the program I posted. The destructor messages should appear before you have to press enter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.