Similar additions to the other car classes enable you
to modify the class-objectcreating for
loop, as shown in the
following version of the analyze_train
program:
#include#include const double pi = 3.14159; // Box and cylinder class definitions go here 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;} }; 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) { } // Displayers: virtual char* short_name ( ) {return "box";} virtual double capacity ( ) {return volume ( );} }; // Other modified railroad car class definitions go here railroad_car *train[100]; char input_buffer[100]; enum {eng_code = 'E', box_code = 'B', tnk_code = 'T', cab_code = 'C'}; char extract_car_code (char *input_buffer) {return input_buffer[4];} main ( ) { int n, car_count; // Read serial number and create car class objects: for (car_count = 0; cin >> input_buffer; ++car_count) { // Construct new class object: switch (extract_car_code (input_buffer)) { case eng_code: train[car_count] = new engine (input_buffer); break; case box_code: train[car_count] = new box_car (input_buffer); break; case tnk_code: train[car_count] = new tank_car (input_buffer); break; case cab_code: train[car_count] = new caboose (input_buffer); break; } } // Display report: for (n = 0; n < car_count; ++n) { // Display short name and capacity and terminate the line: cout << train[n] -> short_name ( ) << " " << train[n] -> capacity ( ) << endl; } }