1

I have the following sample code of two classes Programmer and CSstudent where

CSstudent:public Programmer

Destructors are defined for both the classes :

class Programmer{
           .........
            public:
               ~Programmer(){
                 cout<<"A Programmer obj destroyed";       
        }
      }

class CSstudent:public Programmer{
           .........
       public:
        ~CSstudent(){
          cout<<"CSstudent obj destroyed";
      }
         }

Now in the main() :

int main(){
    CSstudent cs1;

    /* call to CSstudent member functions by invoking the cs1 object
      ........... */

    cout<<cs1.getName()<<cs1.getUni()<<cs1.getLang()<<endl;
}

After the program runs I get the following: CSstudent obj destroyed A Programmer obj destroyed

I know that destructors are not inherited, and destructors are invoked when objects go out of scope. I initialized a CSstudent object then why do the destructor of the Programmer class invoked ?

I was hoping for this output: CSstudent obj destroyed

Abrar
  • 6,874
  • 9
  • 28
  • 41
  • The base class has to be destroyed as well. It's destructor is run so that it can handle any potential resources in the base class. – Bo Persson Oct 26 '15 at 18:55
  • You asked for this ... Student inherits from programmer, hence when you create a student you must also create a programmer. Leading on from this, once you destroy a student you must also destroy a programmer. Trying putting the same print statements in the constructor. – Fantastic Mr Fox Oct 26 '15 at 18:55
  • 1
    I strongly suggest reading a book on C++. – SergeyA Oct 26 '15 at 18:57
  • The destructor of the base class should also be declared `virtual` for deletion of a derived instance via a base class pointer to work. – James Adkison Oct 26 '15 at 19:01
  • Modification to @SergeyA 's comment: Make sure it is [a good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Far too many are written by folks who are a dab hand at C or Java and seem to assume a 1:1 correspondence. – user4581301 Oct 26 '15 at 19:14
  • @user4581301, in my honest opinion there are only 3 books (latest edutitions of them) on C++ worth reading: by Stroustroupp, by Meyers and by Sutter. Nothing else is needed. – SergeyA Oct 26 '15 at 19:16
  • @JamesAdkison: It should only be declared `virtual` if it's actually a base class in the OOP context. In all other cases, declaring it `virtual` is wrong. Since most custom types in C++ are not used with OOP in mind, most destructors should not be `virtual`. – Christian Hackl Oct 26 '15 at 20:23
  • @ChristianHackl My comment is specifically about the OP's code not an absolute statement that all destructors everywhere should be declared virtual (hence the very specific comment about deleting a derived instance via a base class pointer). – James Adkison Oct 26 '15 at 20:25

2 Answers2

5

A derived class essentially contains the base class within it. When the derived class is constructed, the base class is constructed first and the derived class is constructed next (which makes sense, in case your derived class requires the use of base class variables that it assumes have been properly initialized). The opposite is true on destruction, the derived class destructor is called first, then the base class destructor is called to cleanup base class information.

RyanP
  • 1,898
  • 15
  • 20
  • Recommend recommending that OP add to the constructors a debug print line similar to the ones in the destructors to see what is going on behind the scenes. – user4581301 Oct 26 '15 at 19:16
0

Because internally when you create a CSstudent object, a Programmer object is created. Thus, when you delete CSstudent the base object must be deleted too.

Gacci
  • 1,388
  • 1
  • 11
  • 23