Home Segments Index Top Previous Next

666: Mainline

A problem emerges if you happen to define a destructor for the railroad_car class that reclaims the chunk of memory reserved for the serial number. In Chapter 43, you learned that you can define such a destructor in the railroad_car class definition as follows:

class railroad_car {
  public: char *serial_number;
          // Constructors:
          railroad_car ( ) { }
          railroad_car (char *input_buffer) {
            // Create new array just long enough:
            serial_number = new char[strlen(input_buffer) + 1];
            // Copy string into new array:
            strcpy (serial_number, input_buffer);
          }
          // Destructor:               
          virtual ~railroad_car ( ) {  
            delete [ ] serial_number;  
          }                            
          // Other: 
          virtual char* short_name ( ) {return "rrc";} 
          virtual double capacity ( ) {return 0.0;} 
};