Constructor/destructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deanm007
    New Member
    • Apr 2010
    • 37

    #1

    Constructor/destructor

    The code below does not show the output when the destructor is called. It shows when the constructor is called, then it shows the age. Then it finishes. Am I doing something wrong here? Thanks

    Code:
     #include <iostream>
     
     using namespace std;
     
     enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB}; //global enumeration
    
    
     class Mammal //This is the base class 
     {
        public:
               
     // constructors
     Mammal();
     ~Mammal();
    
     //accessor methods
     int GetAge()const {return itsAge;}
     void SetAge(int age){itsAge = age;}
     int GetWeight() const {return itsWeight;}
     void SetWeight(int weight){itsWeight = weight;}
    
     //Other methods
     void Speak() const {cout << "Talking" << endl;}
     void Sleep()const {cout << "Sleeping" << endl;}
    
    
     protected: 
                
     int itsAge;
     int itsWeight;
     };
    
     class Dog : public Mammal
     {
     public:
    
     // Constructors...dog class must have its own constructor //and destructor.
    
     Dog();
     ~Dog();
    
     // Accessor methods
     BREED GetBreed() const {return itsBreed;} 
     void SetBreed(BREED breed){itsBreed = breed;} 
     // Other methods
     void WagTail(){cout << "Tail wagging..." << endl;}
     void BegForFood(){cout << "Begging for food..." << endl;}
    
     protected:
     BREED itsBreed; 
     };
     
      Mammal::Mammal()
      {
      itsAge=1; 
      itsWeight=5;
      cout<<"Mammal constructor called" << endl; 
      } 
     
      Mammal::~Mammal()
      {
      cout<<"Mammal destructor called" << endl;
      }
      
      Dog::Dog()
      {
      itsBreed = YORKIE;
      cout << "Dog constructor called" << endl;
      
      }
      
      Dog::~Dog()
      {
      cout<<"Dog destructor called" << endl;
      }
      
     
     int main()
     {
         Dog Benny; 
         Benny.Speak();
         Benny.BegForFood();
         Benny.SetAge(56); 
         cout << "Benny is " << Benny.GetAge() << " years old." << endl;
         
         
         system("pause");
         return 0;
         
     }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    It wont show any output for the destructor because the destructor is not called until until main exits and the object is destroyed.

    Remove the system("pause") ;, which is bad practice anyway and run the program in a way doesn't involve the command prompt disappearing when the program exits (Ctrl-F5 in MSVC or find your IDEs method to run the program rather than debug it or just start a command prompt and run it).

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      output using gcc 3.4.4 (cygwin) :

      Mammal constructor called
      Dog constructor called
      Talking
      Begging for food...
      Benny is 56 years old.
      Dog destructor called
      Mammal destructor called

      as expected.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        the destructor is not called until until main exits and the object is destroyed.
        Maybe.

        The compiler may call the destructor any time after the object is marked for destruction (goes out of scope or you delete yourself). This may be right away or perhaps at the end of main. There's no way to know unless your destructor tells you it has been called.

        Comment

        • Deanm007
          New Member
          • Apr 2010
          • 37

          #5
          Banfa, as you mentioned, I got rid of system pause and did ctrl+f10 on dev c++ and the prompt just does not stay up. So I kept holding ctrl+f10 and I saw a glimpse of the destructor being called. Also, I tried it in MSVC (without system pause or cin.get()) with ctrl+f5 and it shows the destructor being called. So I understand that the destructor is called only when the object is destroyed or when main exits and main does not exit if you have system pause statement in there. Thank you very much.

          Comment

          Working...