Advertisement
skb50bd

CSE107Final_Q1

Sep 9th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Vehicle {
  7. protected:
  8.     string vehicle_type;
  9.     Vehicle *next;
  10. public:
  11.     Vehicle(): vehicle_type("N/A"), next(NULL) {}
  12.     ~Vehicle() {}
  13.     virtual void read() {
  14.         cout << "Enter type: ";
  15.         cin >> vehicle_type;
  16.     }
  17.     virtual bool isLarge() = 0;
  18.  
  19.     void setNext(Vehicle *P) {
  20.         next = P;
  21.     }
  22.  
  23.     Vehicle * getNext() {
  24.         return next;
  25.     }
  26.  
  27.     string getType() {
  28.         return vehicle_type;
  29.     }
  30. };
  31.  
  32.  
  33. class PassengerVehicle: public Vehicle {
  34. private:
  35.     int passengerCapacity;
  36. public:
  37.     PassengerVehicle(): passengerCapacity(0) {}
  38.     ~PassengerVehicle() {}
  39.  
  40.     void read() {
  41.         Vehicle :: read();
  42.         cout << "Enter Passenger Capacity: " ;
  43.         cin >> passengerCapacity;
  44.     }
  45.  
  46.     bool isLarge() {
  47.         return (passengerCapacity >= 40) ? true : false;
  48.     }
  49. };
  50.  
  51.  
  52. class CargoVehicle: public Vehicle {
  53. private:
  54.     float loadCapacity;
  55. public:
  56.     CargoVehicle(): loadCapacity(0.0) {}
  57.     ~CargoVehicle() {}
  58.  
  59.     void read() {
  60.         Vehicle :: read();
  61.         cout << "Enter Load Capacity: " ;
  62.         cin >> loadCapacity;
  63.     }
  64.  
  65.     bool isLarge() {
  66.         return (loadCapacity >= 5.0) ? true : false;
  67.     }
  68. };
  69.  
  70.  
  71.  
  72.  
  73. int main() {
  74.     Vehicle *Head, *Current, *Last;
  75.     unsigned short choice;
  76.     bool first = true;
  77.  
  78.     cout << "Welcome!" << endl;
  79.  
  80.     while(true) {
  81.         cout << "Press 1 for creating a Passenger, 2 for Cargo, 0 to stop" << endl;
  82.         cin >> choice;
  83.  
  84.         if(choice == 1) {
  85.             if(first) {
  86.                 first = false;
  87.                 Head = new PassengerVehicle();
  88.                 Current = Last = Head;
  89.             }
  90.  
  91.             else {
  92.                 Current = new PassengerVehicle();
  93.                 Last -> setNext(Current);
  94.                 Last = Current;
  95.             }
  96.             Current -> read();
  97.         }
  98.  
  99.         else if(choice == 2) {
  100.             if(first) {
  101.                 first = false;
  102.                 Head = new CargoVehicle();
  103.                 Current = Last = Head;
  104.             }
  105.  
  106.             else {
  107.                 Current = new CargoVehicle();
  108.                 Last -> setNext(Current);
  109.                 Last = Current;
  110.             }
  111.             Current -> read();
  112.         }
  113.  
  114.         else if(choice == 0) break;
  115.     }
  116.  
  117.     cout << "Printing Results ..." << endl;
  118.     for(Current = Head; Current != NULL; Current = Current -> getNext()) {
  119.         if(Current -> isLarge())
  120.             cout << Current -> getType() << " is " <<  "LARGE." << endl;
  121.         else
  122.             cout << Current -> getType() << " is " <<  "NOT LARGE." << endl;
  123.     }
  124.  
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement