Home Segments Index Top Previous Next

509: Mainline

The following is the analyze_train program, revised to include the volume-displaying augmentations:

#include   
const double pi = 3.14159; 
class box { 
  public: double height, width, length; 
    // Default constructor: 
    box ( ) { } 
    // Argument-bearing constructor: 
    box (double h, double w, double l) { 
      height = h; width = w; length = l; 
    } 
    // Volume member function: 
    double volume ( ) {return height * width * length;} 
}; 
// Cylinder definition goes here 
class railroad_car { 
  public: railroad_car ( ) { } 
          virtual void display_short_name ( ) { } 
          virtual void display_capacity ( ) { } 
}; 
class box_car : public railroad_car, public box { 
  public: box_car ( ) : box (10.5, 9.5, 40.0) { } 
          virtual void display_short_name ( ) {cout << "box";} 
          virtual void display_capacity ( ) {cout << volume ( );} 
}; 
// Tank car definition goes here 
class engine : public railroad_car { 
  public: engine ( ) { } 
          virtual void display_short_name ( ) {cout << "eng";} 
}; 
// Caboose definition goes here 
// Define railroad car pointer array: 
railroad_car *train[100]; 
// Declare enumeration constants, needed in switch statement: 
enum {eng_code, box_code, tnk_code, cab_code}; 
main ( ) { 
  int n, car_count, type_code; 
  for (car_count = 0; cin >> type_code; ++car_count) 
    switch (type_code) { 
      case eng_code: train[car_count] = new engine;   break; 
      case box_code: train[car_count] = new box_car;  break; 
      case tnk_code: train[car_count] = new tank_car; break; 
      case cab_code: train[car_count] = new caboose;  break; 
    } 
  for (n = 0; n < car_count; ++n) { 
    train[n] -> display_short_name ( ); 
    cout << "     "; 
    train[n] -> display_capacity ( ); 
    cout << endl; 
  } 
} 
--- Data ---
0 1 1 2 3 
--- Result --- 
eng      
box     3990 
box     3990 
tnk     1539.38 
cab