Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Vehicle {
- protected:
- float mass;
- int width;
- int height;
- public:
- /*Vehicle() {}
- Vehicle(float m, int w, int h) {
- mass = m;
- width = w;
- height = h;
- }*/
- Vehicle(float m = 0, int w = 0, int h = 0) {
- mass = m;
- width = w;
- height = h;
- }
- /* Because I don't want Vehicle to be an abstract class
- I shall not make a pure virtual (=0) function */
- virtual ~Vehicle() {
- // cout << "Base Destructor\n";
- }
- virtual int dailyPrice() = 0;
- const float getMass() {
- return mass;
- }
- const int getWidth() {
- return width;
- }
- };
- class Automobile : public Vehicle {
- private:
- int doors;
- public:
- /*Automobile() {}
- Automobile(float m, int w, int h, int d) : Vehicle(m, w, h) {
- doors = d;
- }*/
- Automobile(float m = 0, int w = 0, int h = 0, int d = 0) : Vehicle(m, w, h) {
- doors = d;
- }
- // Virtual destructor in base class, therefore here also
- ~Automobile() {}
- int dailyPrice() {
- if (doors < 5) {
- return 100;
- }
- return 130;
- }
- };
- class Bus : public Vehicle {
- private:
- int passengers;
- public:
- /*Bus() {}
- Bus(float m, int w, int h, int p) : Vehicle(m, w, h) {
- passengers = p;
- }*/
- Bus(float m = 0, int w = 0, int h = 0, int p = 0) : Vehicle(m, w, h) {
- passengers = p;
- }
- ~Bus() {}
- int dailyPrice() {
- return passengers * 5;
- }
- };
- class Truck : public Vehicle {
- private:
- int maxWeight;
- public:
- /*Truck() {}
- Truck(float m, int w, int h, int mw) : Vehicle(m, w, h) {
- maxWeight = mw;
- }*/
- Truck(float m = 0, int w = 0, int h = 0, int mw = 0) : Vehicle(m, w, h) {
- maxWeight = mw;
- }
- ~Truck() {}
- int dailyPrice() {
- return (mass + maxWeight) * 0.02;
- }
- const int getMaxWeight() {
- return maxWeight;
- }
- };
- class Parking {
- private:
- /* This is a pointer to a Vehicle object */
- //Vehicle *vehicles;
- /* This is a pointer to a Vehicle pointer or an array of Vehicle pointers */
- Vehicle **vehicles;
- int len;
- public:
- Parking() {
- vehicles = NULL;
- len = 0;
- }
- /* this would just destroy the array of pointers,
- * but not actually the objects that the pointers
- * contained in the array point to */
- //~Parking() { delete[] vehicles; }
- ~Parking() {
- /* Delete all Vehicle objects individually*/
- for (int i = 0; i < len; ++i) {
- delete vehicles[i];
- }
- /* Then delete the array itself*/
- delete[] vehicles;
- }
- Parking(Vehicle **v, int n) { // Regular Constructor
- len = n;
- vehicles = new Vehicle *[len];
- for (int i = 0; i < len; ++i) {
- vehicles[i] = v[i];
- }
- }
- Parking(const Parking &p) { // Copy Constructor
- vehicles = new Vehicle *[p.len];
- for (int i = 0; i < len; ++i) {
- vehicles[i] = p.vehicles[i];
- }
- len = p.len;
- }
- Parking &operator=(const Parking &p) {
- if (this == &p) {
- return *this;
- }
- for (int i = 0; i < len; ++i) {
- delete vehicles[i];
- }
- delete[] vehicles;
- vehicles = new Vehicle *[p.len];
- for (int i = 0; i < len; ++i) {
- vehicles[i] = p.vehicles[i];
- }
- len = p.len;
- return *this;
- }
- Parking &operator+=(Vehicle *v) {
- Vehicle **old = vehicles;
- vehicles = new Vehicle *[len + 1];
- for (int i = 0; i < len; ++i) {
- vehicles[i] = old[i];
- }
- vehicles[len++] = v;
- /* Delete just the array old, not the object themselves*/
- delete [] old;
- return *this;
- }
- float totalMass() {
- float sum = 0;
- for (int i = 0; i < len; ++i) {
- sum += vehicles[i]->getMass();
- }
- return sum;
- }
- int vehiclesWiderThan(int width) {
- int widerThan = 0;
- for (int i = 0; i < len; ++i) {
- if (vehicles[i]->getWidth() > width) {
- widerThan++;
- }
- }
- return widerThan;
- }
- void print() {
- int autos = 0, buses = 0, trucks = 0;
- for (int i = 0; i < len; ++i) {
- Automobile *a = dynamic_cast<Automobile *>(vehicles[i]);
- if (a) {
- autos++;
- }
- Bus *b = dynamic_cast<Bus *>(vehicles[i]);
- if (b) {
- buses++;
- }
- Truck *t = dynamic_cast<Truck *>(vehicles[i]);
- if (t) {
- trucks++;
- }
- }
- cout << "Automobiles: " << autos << endl;
- cout << "Busses: " << buses << endl;
- cout << "Trucks: " << trucks << endl;
- }
- int greaterMaxWeightThan(Vehicle &v) {
- int weightThan = 0;
- for (int i = 0; i < len; ++i) {
- Truck *t = dynamic_cast<Truck *>(vehicles[i]);
- if (t) {
- if (t->getMaxWeight() > v.getMass()) {
- weightThan++;
- }
- }
- }
- return weightThan;
- }
- int dailyProfit() {
- int profit = 0;
- for (int i = 0; i < len; ++i) {
- profit += vehicles[i]->dailyPrice();
- }
- return profit;
- }
- };
- int main() {
- Parking p;
- int n;
- cin >> n;
- int width, height, broj;
- float mass, max_weight;
- for (int i = 0; i < n; i++) {
- int type;
- cin >> type;
- if (type == 1) {
- cin >> mass >> width >> height >> broj;
- Automobile *a = new Automobile(mass, width, height, broj);
- p += a;
- }
- if (type == 2) {
- cin >> mass >> width >> height >> broj;
- p += new Bus(mass, width, height, broj);
- }
- if (type == 3) {
- cin >> mass >> width >> height >> max_weight;
- p += new Truck(mass, width, height, max_weight);
- }
- }
- p.print();
- cout << "\nDaily profit: " << p.dailyProfit() << endl;
- cout << "Total mass: " << p.totalMass() << endl;
- cout << "Number wider then 5 is: " << p.vehiclesWiderThan(5) << endl;
- Automobile a(1200, 4, 3, 5);
- cout << "Number of trucks with max weight larger then the automobile is: " << p.greaterMaxWeightThan(a) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment