summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Bicycle.h83
-rw-r--r--PassengerException.h18
-rw-r--r--Vehicle.h203
3 files changed, 304 insertions, 0 deletions
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 <string>
+#include <iostream>
+using namespace std;
+
+
+template<class T>
+class Bicycle : public Vehicle<T>
+{
+ 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 <typename T>
+Bicycle<T>::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 <typename T>
+Bicycle<T>::Bicycle(int x, int y, string n, int c) : Vehicle<T>()
+{
+ 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 <typename T>
+Bicycle<T>::~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 <typename T>
+void Bicycle<T>::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 <typename T>
+void Bicycle<T>::set_capacity(int c)
+{
+ int n = c > 2 ? 2 : c<0?-c:c;
+ Vehicle<T>::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<stdexcept>
+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 <string>
+#include <iostream>
+using namespace std;
+
+
+//ADD LINE HERE TO MAKE IT A TEMPLATE CLASS
+template <class T>
+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<T> operator+(Vehicle<T> &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 <typename T>
+Vehicle<T>::Vehicle()
+{
+ x_pos = 0;
+ y_pos = 0;
+ name = "Default";
+ capacity = 2;
+ current_passenger = -1;
+ passengers = new T[capacity];
+}
+
+template <typename T>
+Vehicle<T>::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<typename T>
+Vehicle<T>::~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 <typename T>
+void Vehicle<T>::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<typename T>
+void Vehicle<T>::set_name(string n)
+{
+ name = n;
+}
+
+//Set capacity function
+template <typename T>
+void Vehicle<T>::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<typename T>
+string Vehicle<T>::get_name()
+{
+ return name;
+}
+
+
+//Here is the provided print funtion
+//DO NOT MODIFY
+template<typename T>
+void Vehicle<T>::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 <typename T>
+Vehicle<T> Vehicle<T>::operator+(Vehicle<T> &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<T>(newx, newy, newname, cap);
+}
+
+
+
+
+//********************************************************************
+
+/*
+
+Add
+
+get_passenger(int p) throw(PassengerException)
+add_passenger(T p) throw(PassengerException)
+remove_passenger() throw(PassengerException)
+
+functions here
+
+*/
+template <typename T>
+T Vehicle<T>::get_passenger(int p) throw(PassengerException)
+{
+ if (p < capacity && p >= 0)
+ return passengers[p];
+ else
+ throw PassengerException(p, capacity);
+}
+
+template <typename T>
+void Vehicle<T>::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 <typename T>
+void Vehicle<T>::remove_passenger() throw(PassengerException)
+{
+ if (current_passenger >= 0 && current_passenger < capacity) {
+ current_passenger--;
+ } else
+ throw PassengerException(current_passenger, capacity);
+}
+
+#endif
+