Fortunately, you know you can have derived constructors call base-class
constructors explicitly. The following is a suitable base-class
constructor, embedded in the railroad_car
class definition:
class railroad_car { public: char *serial_number; railroad_car ( ) { } // New constructor: 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); } virtual char* short_name ( ) {return "rrc";} virtual double capacity ( ) {return 0.0;} };
The following defines an engine
class constructor so as to make use
of the one-parameter railroad_car
constructor:
class engine : public railroad_car { public: engine ( ) { } // New constructor: engine (char *input_buffer) : railroad_car (input_buffer) { } virtual char* short_name ( ) {return "eng";} };