From 481468176e30c07872aea61ef65ba9156efd0fa6 Mon Sep 17 00:00:00 2001 From: Michael Abed Date: Tue, 27 Nov 2012 12:21:46 -0500 Subject: add lab 4 --- Bicycle.h | 83 +++++++++++++++++++++ PassengerException.h | 18 +++++ Vehicle.h | 203 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 304 insertions(+) create mode 100644 Bicycle.h create mode 100644 PassengerException.h create mode 100644 Vehicle.h diff --git a/Bicycle.h b/Bicycle.h new file mode 100644 index 0000000..59d1770 --- /dev/null +++ b/Bicycle.h @@ -0,0 +1,83 @@ +#ifndef BICYCLE_H +#define BICYCLE_H + +#include "Vehicle.h" + +#include +#include +using namespace std; + + +template +class Bicycle : public Vehicle +{ + public: + Bicycle(); + Bicycle(int, int, string, int); //set x, y, name and capacity (remember that capacity can be at most 2) + ~Bicycle(); + + //Redefined functions inherited from Vehicle + void move(int, int); // move to the requested x, y location divided by 2 + void set_capacity(int); // set the capacity value; can't be larger than 2 + +}; + +//Add default constructor +//Include: cout << "Creating new bicyle " << this->name << endl; +template +Bicycle::Bicycle() +{ + this->x_pos = 0; + this->y_pos = 0; + this->name = "Default"; + this->capacity = 1; + this->current_passenger = -1; + this->passengers = new T[this->capacity]; + cout << "Creating new bicyle " << this->name << endl; +} + + +//Add constructor +//Include: cout << "Creating new bicyle " << this->name << endl; +template +Bicycle::Bicycle(int x, int y, string n, int c) : Vehicle() +{ + this->x_pos = x; + this->y_pos = y; + this->name = n; + this->capacity = c > 2 ? 2 : c; + this->current_passenger = -1; + this->passengers = new T[this->capacity]; + cout << "Creating new bicyle " << this->name << endl; +} + +//Add destructor +//Include: cout << "Destroyed a bicycle named: " << this->name << endl; +template +Bicycle::~Bicycle() +{ + cout << "Destroyed a bicycle named: " << this->name << endl; +} + + +//Add move function +//Include: cout << "Moving to bicycle " << this->name << " to x: " << this->x_pos << " y: " << this->y_pos << endl; +template +void Bicycle::move(int x, int y) +{ + this->x_pos = x/2; + this->y_pos = y/2; + cout << "Moving to bicycle " << this->name << " to x: " << this->x_pos << " y: " << this->y_pos << endl; +} + +//Add capacity function +//Include: cout << "Setting bicycle " << this->name << " to " << this->capacity << endl; +template +void Bicycle::set_capacity(int c) +{ + int n = c > 2 ? 2 : c<0?-c:c; + Vehicle::set_capacity(n); + cout << "Setting bicycle " << this->name << " to " << this->capacity << endl; +} + +#endif diff --git a/PassengerException.h b/PassengerException.h new file mode 100644 index 0000000..8acb395 --- /dev/null +++ b/PassengerException.h @@ -0,0 +1,18 @@ +#ifndef PASSENGEREXCEPTION_H +#define PASSENGEREXCEPTION_H + +#include +using namespace std; + +// ALL YOU HAVE TO DO IS FIX #1 and #2. The rest comes down to the try and catch blocks you need to use in your main to test things out. + +class PassengerException: public logic_error +{ + public: + PassengerException(int request, int capacity): logic_error("Passenger Issues") + { + cout << "The requested int for passenger manipulation " << request << " is outside the capacity " << capacity << endl; + } +}; +#endif + diff --git a/Vehicle.h b/Vehicle.h new file mode 100644 index 0000000..8125835 --- /dev/null +++ b/Vehicle.h @@ -0,0 +1,203 @@ +#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 + -- cgit v1.2.3