lundi 26 octobre 2015

Destructor execution of object of an inherited class

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

Aucun commentaire:

Enregistrer un commentaire