Advertisement
txe

on-to-c++-seg-541

txe
Mar 29th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const double pi = 3.14159;
  5.  
  6. class box {
  7.   public: double height, width, length;
  8.     // Default constructor:
  9.     box ( ) { }
  10.     // Argument-bearing constructor:
  11.     box (double h, double w, double l) {
  12.       height = h; width = w; length = l;
  13.     }
  14.     // Volume member function:
  15.     double volume ( ) {return height * width * length;}
  16. };
  17.  
  18. // ...Cylinder definition goes here
  19. class cylinder {
  20.   public: double radius, length;
  21.   // Default constructor:
  22.   cylinder ( ) { }
  23.   // Other member functions
  24.   // Argument-bearing constructor:
  25.   double volume ( ) {return pi * radius * radius * length;}
  26.   };
  27.  
  28. class railroad_car {
  29.   public: railroad_car ( ) { }
  30.           virtual char* short_name ( ) {return "rrc";}
  31.           virtual double capacity ( ) {return 0.0;}
  32. };
  33.  
  34. class box_car : public railroad_car, public box {
  35.   public: box_car ( ) : box (10.5, 9.5, 40.0) { }
  36.           virtual char* short_name ( ) {return "box";}
  37.           virtual double capacity ( ) {return volume ( );}
  38. };
  39.  
  40. // Tank car definition goes here
  41. class tank_car : public railroad_car, public cylinder {
  42.   public: tank_car ( ) : cylinder (3.5, 40.0) { }
  43.   virtual char* short_name ( ) {return "tnk";}
  44.   virtual double capacity ( ) {return volume ( );}
  45.   };
  46.  
  47. //..Engine &
  48. class engine : public railroad_car {
  49.   public: engine ( ) { }
  50.   virtual char* short_name ( ) {return "eng";}
  51. };
  52.  
  53. // ..Caboose definition goes here
  54. class caboose : public railroad_car {
  55.   public: caboose ( ) { }
  56.   virtual char* short_name ( ) {return "cab";}
  57.   };
  58.  
  59. // Define railroad car pointer array:
  60. railroad_car *train[100];
  61.  
  62. // Declare enumeration constants, needed in switch statement:
  63. enum {eng_code, box_code, tnk_code, cab_code};
  64.  
  65. main ( ) {
  66.   int n, car_count, type_code;
  67.   for (car_count = 0; cin >> type_code; ++car_count)
  68.     switch (type_code) {
  69.       case eng_code: train[car_count] = new engine;   break;
  70.       case box_code: train[car_count] = new box_car;  break;
  71.       case tnk_code: train[car_count] = new tank_car; break;
  72.       case cab_code: train[car_count] = new caboose;  break;
  73.     }
  74.  
  75. //Display report.
  76.   for (n = 0; n < car_count; ++n)  
  77.  
  78. // Display shortname & capacity & terminate the line:
  79. cout << train[n] -> short_name ( )
  80.      << "     "
  81.      << train[n] -> capacity ( )
  82.      << endl;
  83.    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement