"Destructors for a derived class object are called in the reverse order of the constructors for the object. This is a general rule that always applies. Constructors are invoked starting with the base class constructor and then the derived class constructor, whereas the destructor for the derived class is called
first when an object is destroyed, followed by the base class destructor."
But why, or is it just because, so programmers know which one and modify their destructor accordingly??
I believe it is because in a class hierarchy, the derived classes may be using variables or data located i base classes. Is it not logical to initialize these first to ensure that the derived classes do not use invalid data? When we then destroy the objects; would it be logical to destroy "base" data whilst the derived destructor may still use this data in its destructor?
I believe this is the logical reasoning behind this rule. Think about it.
If the base class portion of a derived class were destroyed before the derived class version, the class instance would be in an undefined state. It would only be half-alive when the derived class destructor was called.
Maybe the base class has a container of some sort. The derived class writes the data in the container to a file when it's destructor is called. How does it write that data to file if the container no longer exists?