#ifndef VEHICLE_H #define VEHICLE_H #include "PassengerException.h" #include #include using namespace std; //ADD LINE HERE TO MAKE IT A TEMPLATE CLASS template class Vehicle { public: Vehicle(); //default contstructor Vehicle(int, int, string, int); // set the x, y, name, and capacity virtual ~Vehicle(); //destructor; should this be virtual or not??? //Inheritance - question #1; create these functions here and in Bicycle class string get_name(); // get the name of the vehicle void set_name(string); //set the name of the vehicle void print(); // std print funtion (GIVEN TO YOU) //Polymorphism - question #2 virtual void move(int, int); // move to the requested x, y location virtual void set_capacity(int); // set the capacity value //Operator overloading - question #3 Vehicle operator+(Vehicle &secondVehicle) const; //Exceptions - question #4 T get_passenger(int) throw(PassengerException); // get the passenger at the specified index void add_passenger(T) throw(PassengerException); // add passenger and the current passenger index void remove_passenger() throw(PassengerException); // remove a passenger using current passenger index protected: int x_pos; int y_pos; string name; int capacity; T *passengers; int current_passenger; }; //************************Add your functions below //Add constructors /* x_pos and y_pos = 0 name = "Default" capacity is two current passenger = -1 create a new array of type T with size capacity */ template Vehicle::Vehicle() { x_pos = 0; y_pos = 0; name = "Default"; capacity = 2; current_passenger = -1; passengers = new T[capacity]; } template Vehicle::Vehicle(int x, int y, string n, int cap) { x_pos = x; y_pos = y; name = n; capacity = cap; current_passenger = -1; passengers = new T[capacity]; } //Add destructor //What do you need to destroy //Put this line in: cout << "Destroyed a vehicle named: " << this->name << endl; template Vehicle::~Vehicle() { cout << "Destroyed a vehicle named: " << this->name << endl; } //Move function //Put this line in before you return: cout << "Moving Vehicle to x: " << this->x_pos << " y: " << this->y_pos << endl; template void Vehicle::move(int x, int y) { x_pos = x; y_pos = y; cout << "Moving Vehicle to x: " << this->x_pos << " y: " << this->y_pos << endl; } //Set name function template void Vehicle::set_name(string n) { name = n; } //Set capacity function template void Vehicle::set_capacity(int n) { int c = n < 0 ? -n : n; T *p = new T[c]; for (int i = 0; i < capacity; i++) { if (i < c) p[i] = passengers[i]; } capacity = c; T *old = passengers; passengers = p; } //Get name function template string Vehicle::get_name() { return name; } //Here is the provided print funtion //DO NOT MODIFY template void Vehicle::print() { cout << "Object " << name << "\n at x: " << x_pos << "\n and y: " << y_pos << "\n with capacity: " << capacity << endl; return; } //*************************************************************************** //Add operator overloading function here.... template Vehicle Vehicle::operator+(Vehicle &secondVehicle) const { int newx = x_pos + secondVehicle.x_pos; int newy = y_pos + secondVehicle.y_pos; int cap = capacity > secondVehicle.capacity ? capacity : secondVehicle.capacity; string newname = name + secondVehicle.name; return Vehicle(newx, newy, newname, cap); } //******************************************************************** /* Add get_passenger(int p) throw(PassengerException) add_passenger(T p) throw(PassengerException) remove_passenger() throw(PassengerException) functions here */ template T Vehicle::get_passenger(int p) throw(PassengerException) { if (p < capacity && p >= 0) return passengers[p]; else throw PassengerException(p, capacity); } template void Vehicle::add_passenger(T p) throw(PassengerException) { current_passenger++; if (current_passenger >= 0 && current_passenger < capacity) { passengers[current_passenger] = p; } else { current_passenger--; throw PassengerException(current_passenger, capacity); } } template void Vehicle::remove_passenger() throw(PassengerException) { if (current_passenger >= 0 && current_passenger < capacity) { current_passenger--; } else throw PassengerException(current_passenger, capacity); } #endif