Base Class
class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance); const string & getName() const; double getDistance() const; void setDistance(double); // Passes in the departure time (as minute) and returns arrival time (as minute) // For example: // 8 am will be passed in as 480 minutes (8 * 60) // 2:30 pm will be passed in as 870 minutes (14.5 * 60) virtual unsigned computeArrivalTime(unsigned minute) const = 0;};#endif
Derived Classes
Define and implement RoadSegment.h and RoadSegment.cpp
Define a RoadSegment class that inherits from the baseTransportationClass from above. This class will have avector hourlySpeeds data field which will hold thespeed for all 24 hours of the day (each index is 1 hour). Thevector's default speed for all hours will be passed in to theconstructor. You will be able to set/change a speed for a specifichour, or all 24 speeds at once using setHourSpeed(unsigned hour,double speed) or setAllHourSpeeds(const vector &)respectively.
The length of time on the road segment is calculated by takingit's distance and dividing by the speed at the time of departure.This length of time is then added to the departureTime parameter toget the arrivalTime.
Define and implement RiverSegment.h and RiverSegment.cpp
Define a RiverSegment class that inherits from the baseTransportationClass from above. This class will have avector scheduledDepartureTimes data field that willkeep track of the departure time (e.g. 10.5 represents the time10:30) of all ferries on the river. You can assume these aresorted. Additionally, there is a _speed value that is the speed ofthe ferry at all times (ferries tend to be consistently slow).There is a setSpeed function to change the _speed value.Additionally, there is an addDepartureTime(double hour) function toadd a departure time to the vector. Remember, these timesneed to be sorted.
Computing the arrival time for the RiverSegment is a little morecomplicated since you need to wait for the next available departuretime. Once you find the next available departure time (after thedeparture time passed in), you need to add the length of time onthe river segment. This is done by dividing the _distance by the_speed. Add the time on the river to the departure time (from thevector of departure times) and this will give you thearrival time.
Note: If there is no departure time scheduledfor after your planned departure, you need to take the firstavailable departure the following day. Our solution assumes thedeparture times are the same every day.
\*TransportationLink.cpp*\
#include \"TransportationLink.h\"
#include \"TransportationLink.h\"
#include
using namespace std;
TransportationLink::TransportationLink(const string &name,double distance)
: _name(name), _distance(distance)
{}
const string & TransportationLink::getName() const {
return _name;
}
double TransportationLink::getDistance() const {
return _distance;
}
void TransportationLink::setDistance(double distance) {
_distance = distance;
}
\*main.cpp*\
#include \"RoadSegment.h\"
#include \"RiverSegment.h\"
#include
using namespace std;
void constructRoute(vector&);
int routeArrivalTime(const vector&, unsigned);
void addRoad(vector &);
void addRiver(vector &);
int main() {
vector route1;
constructRoute(route1);
unsigned departureTime;
unsigned arrivalTime;
cout << \"Enter route departure time (in minutes): \";
cin >> departureTime;
cout << endl;
arrivalTime = routeArrivalTime(route1, departureTime);
cout << \"Arrival Time: \" << arrivalTime <Â Â
return 0;
}
void constructRoute(vector&route) {
char option = 'q';
do {
cout << endl;
cout << \"Menu\" << endl;
cout << \"- Add Road ('D' or 'd')\" << endl;
cout << \"- Add River ('R' or 'r')\" << endl;
cout << \"- Quit ('Q' or 'q')\" << endl;
cout << \"Choose an action: \";
cin >> option;
cout << endl;
cin.ignore(); // get rid of whitespace character
 Â
if (option == 'd' || option == 'D') {
  addRoad(route);
}
else if (option == 'r' || option == 'R') {
  addRiver(route);
}
} while (option != 'q' && option != 'Q');
}
int routeArrivalTime(const vector&route, unsigned departureTime) {
unsigned arrivalTime = departureTime;
for (unsigned i = 0; i < route.size(); ++i) {
arrivalTime =route.at(i)->computeArrivalTime(arrivalTime);
}
return arrivalTime;
}
void addRoad(vector &route){
string name;
double speed;
double distance;
char answer = 'n';
unsigned hour;
cout << \"Enter name of road: \";
getline(cin, name);
cout << endl;
cout << \"Enter road's default speed: \";
cin >> speed;
cout << endl;
cout << \"Enter road's distance: \";
cin >> distance;
cout << endl;
RoadSegment *road = new RoadSegment(name, speed, distance);
cout << \"Enter non-default speeds by hour (if any):\" <cout << \"Enter new speed? (y or n): \";
cin >> answer;
cout << endl;
while (answer != 'n' && answer != 'N') {
cout << \"Which hour? (0 to 23): \";
cin >> hour;
cout << endl;
if (hour > 23) {
cout << \"Invalid hour. Speed not updated.\" <} else {
cout << \"Enter speed for hour \" << hour << \":\";
cin >> speed;
cout << endl;
road->setHourSpeed(hour, speed);
}
cout << \"Enter new speed? (y or n): \";
cin >> answer;
cout << endl;
}
route.push_back(road);
}
void addRiver(vector &route){
string name;
double speed;
double distance;
char answer = 'n';
double hour;
cout << \"Enter name of river: \";
getline(cin, name);
cout << endl;
cout << \"Enter river's speed: \";
cin >> speed;
cout << endl;
cout << \"Enter river's distance: \";
cin >> distance;
cout << endl;
RiverSegment *river = new RiverSegment(name, speed, distance);
cout << \"Enter departure times:\" << endl;
do {
cout << \"Enter time as an hour: (0 to 23.99): \";
cin >> hour;
cout << endl;
if (hour >= 24) {
cout << \"Invalid hour. Departure time not added.\" <} else {
river->addDepartureTime(hour);
}
cout << \"Enter new departure time? (y or n): \";
cin >> answer;
cout << endl;
} while (answer != 'n' && answer != 'N');
route.push_back(river);
}
\*TransportationLink.h*\
#include
using namespace std;
#ifndef __TRANSPORTATIONLINK_H__
#define __TRANSPORTATIONLINK_H__
const int HOURS_PER_DAY = 24;
const int MINS_PER_HOUR = 60;
const int MINS_PER_DAY = MINS_PER_HOUR * HOURS_PER_DAY; //24 *60
class TransportationLink {
protected:
string _name;
double _distance;
public:
TransportationLink(const string &, double);
const string & getName() const;
double getDistance() const;
void setDistance(double);
// Passes in the departure time (as minute) and returns arrivaltime (as minute)
// For example:
// 8 am will be passed in as 480 (8 * 60)
// 2:30 pm will be passed in as 870 (14.5 * 60)
virtual unsigned computeArrivalTime(unsigned minute) const =0;
};
#endif