Filip_Markoski

Vehicle (Working)

Apr 19th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.56 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Vehicle {
  6. protected:
  7.     float mass;
  8.     int width;
  9.     int height;
  10. public:
  11.     /*Vehicle() {}
  12.     Vehicle(float m, int w, int h) {
  13.         mass = m;
  14.         width = w;
  15.         height = h;
  16.     }*/
  17.     Vehicle(float m = 0, int w = 0, int h = 0) {
  18.         mass = m;
  19.         width = w;
  20.         height = h;
  21.     }
  22.  
  23.     /* Because I don't want Vehicle to be an abstract class
  24.      I shall not make a pure virtual (=0) function */
  25.     virtual ~Vehicle() {
  26.         // cout << "Base Destructor\n";
  27.     }
  28.  
  29.     virtual int dailyPrice() = 0;
  30.  
  31.     const float getMass() {
  32.         return mass;
  33.     }
  34.  
  35.     const int getWidth() {
  36.         return width;
  37.     }
  38. };
  39.  
  40. class Automobile : public Vehicle {
  41. private:
  42.     int doors;
  43. public:
  44.     /*Automobile() {}
  45.     Automobile(float m, int w, int h, int d) : Vehicle(m, w, h) {
  46.         doors = d;
  47.     }*/
  48.     Automobile(float m = 0, int w = 0, int h = 0, int d = 0) : Vehicle(m, w, h) {
  49.         doors = d;
  50.     }
  51.  
  52.     // Virtual destructor in base class, therefore here also
  53.     ~Automobile() {}
  54.  
  55.     int dailyPrice() {
  56.         if (doors < 5) {
  57.             return 100;
  58.         }
  59.         return 130;
  60.     }
  61. };
  62.  
  63. class Bus : public Vehicle {
  64. private:
  65.     int passengers;
  66. public:
  67.     /*Bus() {}
  68.     Bus(float m, int w, int h, int p) : Vehicle(m, w, h) {
  69.         passengers = p;
  70.     }*/
  71.  
  72.     Bus(float m = 0, int w = 0, int h = 0, int p = 0) : Vehicle(m, w, h) {
  73.         passengers = p;
  74.     }
  75.  
  76.     ~Bus() {}
  77.  
  78.     int dailyPrice() {
  79.         return passengers * 5;
  80.     }
  81. };
  82.  
  83. class Truck : public Vehicle {
  84. private:
  85.     int maxWeight;
  86. public:
  87.     /*Truck() {}
  88.     Truck(float m, int w, int h, int mw) : Vehicle(m, w, h) {
  89.         maxWeight = mw;
  90.     }*/
  91.  
  92.     Truck(float m = 0, int w = 0, int h = 0, int mw = 0) : Vehicle(m, w, h) {
  93.         maxWeight = mw;
  94.     }
  95.  
  96.     ~Truck() {}
  97.  
  98.     int dailyPrice() {
  99.         return (mass + maxWeight) * 0.02;
  100.     }
  101.  
  102.     const int getMaxWeight() {
  103.         return maxWeight;
  104.     }
  105. };
  106.  
  107. class Parking {
  108. private:
  109.     /* This is a pointer to a Vehicle object */
  110.     //Vehicle *vehicles;
  111.  
  112.     /* This is a pointer to a Vehicle pointer or an array of Vehicle pointers */
  113.     Vehicle **vehicles;
  114.     int len;
  115. public:
  116.     Parking() {
  117.         vehicles = NULL;
  118.         len = 0;
  119.     }
  120.  
  121.     /* this would just destroy the array of pointers,
  122.      * but not actually the objects that the pointers
  123.      * contained in the array point to */
  124.     //~Parking() { delete[] vehicles; }
  125.  
  126.     ~Parking() {
  127.         /* Delete all Vehicle objects individually*/
  128.         for (int i = 0; i < len; ++i) {
  129.             delete vehicles[i];
  130.         }
  131.         /* Then delete the array itself*/
  132.         delete[] vehicles;
  133.     }
  134.  
  135.     Parking(Vehicle **v, int n) { // Regular Constructor
  136.         len = n;
  137.         vehicles = new Vehicle *[len];
  138.         for (int i = 0; i < len; ++i) {
  139.             vehicles[i] = v[i];
  140.         }
  141.     }
  142.  
  143.     Parking(const Parking &p) { // Copy Constructor
  144.         vehicles = new Vehicle *[p.len];
  145.         for (int i = 0; i < len; ++i) {
  146.             vehicles[i] = p.vehicles[i];
  147.         }
  148.         len = p.len;
  149.     }
  150.  
  151.     Parking &operator=(const Parking &p) {
  152.         if (this == &p) {
  153.             return *this;
  154.         }
  155.         for (int i = 0; i < len; ++i) {
  156.             delete vehicles[i];
  157.         }
  158.         delete[] vehicles;
  159.         vehicles = new Vehicle *[p.len];
  160.         for (int i = 0; i < len; ++i) {
  161.             vehicles[i] = p.vehicles[i];
  162.         }
  163.         len = p.len;
  164.         return *this;
  165.     }
  166.  
  167.     Parking &operator+=(Vehicle *v) {
  168.         Vehicle **old = vehicles;
  169.         vehicles = new Vehicle *[len + 1];
  170.         for (int i = 0; i < len; ++i) {
  171.             vehicles[i] = old[i];
  172.         }
  173.         vehicles[len++] = v;
  174.         /* Delete just the array old, not the object themselves*/
  175.         delete [] old;
  176.         return *this;
  177.     }
  178.  
  179.     float totalMass() {
  180.         float sum = 0;
  181.         for (int i = 0; i < len; ++i) {
  182.             sum += vehicles[i]->getMass();
  183.         }
  184.         return sum;
  185.     }
  186.  
  187.     int vehiclesWiderThan(int width) {
  188.         int widerThan = 0;
  189.         for (int i = 0; i < len; ++i) {
  190.             if (vehicles[i]->getWidth() > width) {
  191.                 widerThan++;
  192.             }
  193.         }
  194.         return widerThan;
  195.     }
  196.  
  197.     void print() {
  198.         int autos = 0, buses = 0, trucks = 0;
  199.         for (int i = 0; i < len; ++i) {
  200.             Automobile *a = dynamic_cast<Automobile *>(vehicles[i]);
  201.             if (a) {
  202.                 autos++;
  203.             }
  204.             Bus *b = dynamic_cast<Bus *>(vehicles[i]);
  205.             if (b) {
  206.                 buses++;
  207.             }
  208.             Truck *t = dynamic_cast<Truck *>(vehicles[i]);
  209.             if (t) {
  210.                 trucks++;
  211.             }
  212.         }
  213.         cout << "Automobiles: " << autos << endl;
  214.         cout << "Busses: " << buses << endl;
  215.         cout << "Trucks: " << trucks << endl;
  216.     }
  217.  
  218.     int greaterMaxWeightThan(Vehicle &v) {
  219.         int weightThan = 0;
  220.         for (int i = 0; i < len; ++i) {
  221.             Truck *t = dynamic_cast<Truck *>(vehicles[i]);
  222.             if (t) {
  223.                 if (t->getMaxWeight() > v.getMass()) {
  224.                     weightThan++;
  225.                 }
  226.             }
  227.         }
  228.         return weightThan;
  229.     }
  230.  
  231.     int dailyProfit() {
  232.         int profit = 0;
  233.         for (int i = 0; i < len; ++i) {
  234.             profit += vehicles[i]->dailyPrice();
  235.         }
  236.         return profit;
  237.     }
  238. };
  239.  
  240. int main() {
  241.     Parking p;
  242.     int n;
  243.     cin >> n;
  244.     int width, height, broj;
  245.     float mass, max_weight;
  246.     for (int i = 0; i < n; i++) {
  247.         int type;
  248.         cin >> type;
  249.         if (type == 1) {
  250.             cin >> mass >> width >> height >> broj;
  251.             Automobile *a = new Automobile(mass, width, height, broj);
  252.             p += a;
  253.         }
  254.         if (type == 2) {
  255.             cin >> mass >> width >> height >> broj;
  256.             p += new Bus(mass, width, height, broj);
  257.         }
  258.         if (type == 3) {
  259.             cin >> mass >> width >> height >> max_weight;
  260.             p += new Truck(mass, width, height, max_weight);
  261.         }
  262.     }
  263.  
  264.     p.print();
  265.     cout << "\nDaily profit: " << p.dailyProfit() << endl;
  266.     cout << "Total mass: " << p.totalMass() << endl;
  267.     cout << "Number wider then 5 is: " << p.vehiclesWiderThan(5) << endl;
  268.     Automobile a(1200, 4, 3, 5);
  269.     cout << "Number of trucks with max weight larger then the automobile is: " << p.greaterMaxWeightThan(a) << endl;
  270.     return 0;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment