Home Segments Index Top Previous Next

651: Mainline

You may wish to use destructors that contain temporary output statements as an aid to debugging. Such destructors help you to ensure that they run when you think they do, thus helping you to prevent memory leaks. The following definition of the box_car class, for example, contains a destructor that only displays:

class box_car : public railroad_car, public box {
  public: // Constructors:
          box_car ( ) : box (10.5, 9.5, 40.0) { }
          box_car (char *input_buffer) : railroad_car (input_buffer),
                                   box (10.5, 9.5, 40.0)
            { }
          // Destructor:                                          
          ~box_car ( ) {cout << "Destroying a box car" << endl;}  
          // Members: 
          virtual char* short_name ( ) {return "box";} 
          virtual double capacity ( ) {return volume ( );} 
};