Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- 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 cylinder {
- public: double radius, length;
- // Default constructor:
- cylinder ( ) { }
- // Other member functions
- // Argument-bearing constructor:
- double volume ( ) {return pi * radius * radius * length;}
- };
- class railroad_car {
- public: railroad_car ( ) { }
- virtual char* short_name ( ) {return "rrc";}
- virtual double capacity ( ) {return 0.0;}
- };
- class box_car : public railroad_car, public box {
- public: box_car ( ) : box (10.5, 9.5, 40.0) { }
- virtual char* short_name ( ) {return "box";}
- virtual double capacity ( ) {return volume ( );}
- };
- // Tank car definition goes here
- class tank_car : public railroad_car, public cylinder {
- public: tank_car ( ) : cylinder (3.5, 40.0) { }
- virtual char* short_name ( ) {return "tnk";}
- virtual double capacity ( ) {return volume ( );}
- };
- //..Engine &
- class engine : public railroad_car {
- public: engine ( ) { }
- virtual char* short_name ( ) {return "eng";}
- };
- // ..Caboose definition goes here
- class caboose : public railroad_car {
- public: caboose ( ) { }
- virtual char* short_name ( ) {return "cab";}
- };
- // 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;
- }
- //Display report.
- for (n = 0; n < car_count; ++n)
- // Display shortname & capacity & terminate the line:
- cout << train[n] -> short_name ( )
- << " "
- << train[n] -> capacity ( )
- << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement