In: Computer Science
C++ Code (I just need the dieselLocomotive Class)
Vehicle Class
The vehicle class is the parent class of the derived class:
dieselLocomotive. Their
inheritance will be public inheritance so reflect that
appropriately in their .h files.
The description of the vehicle class is given in the simple UML diagram below:
vehicle
-map: char**
-name: string
-size:int
--------------------------
+vehicle()
+getSize():int
+setName(s:string):void
+getName():string
+getMap():char**
+setMap(s: string):void
+getMapAt(x:int, y:int):char
+~vehicle()
+operator--():void
+determineRouteStatistics()=0:void
The class variables are as follows:
map: A 2D array of chars, it will represent the map that each vehicle will have to
travel on.
name: The name of the vehicle. For example, "Frontier Express".
size: The size of the map as a square matrix.
The class methods are as follows:
vehicle: This is the constructor of the class. It is simply the default constructor
with no additional features.
getSize: This returns the size of the map as a square matrix.
setName: This will set the name of the vehicle as received.
getName: This will return the name of the vehicle.
getMap(): This will return the entire map variable.
setMap(): This method receives the name of a text le that contains an ASCII map.
The map will be a square, equal number of rows and columns. The rst line of the
map will have the number of rows. Every line after will contain a number of ASCII
characters that you must read into the map. This must allocate memory before
assigning the map.
getMapAt: This receives two coordinates, an x and y, and returns what character
is located at those coordinates. If the coordinates are out of bounds, return ':'.
~vehicle: The destructor for the class. It has been made virtual.
determineRouteStatistics: This function will be used to determine information from
the map based on requirements specic to the vehicle in question. As it stands, it
is made pure virtual.
operator--: The overload of this operator will deallocate the memory allocated for
the map.
2.2.2 dieselLocomotive Class
The description of the dieselLocomotive class is given by the simple UML diagram below:
dieselLocomotive
-passengerLimit: int
---------------------------------
+dieselLocomotive()
+~dieselLocomotive()
+getPassengerLimit():int
+setPassengerLimit(s:int):void
+determineRouteStatistics():void
The class variables are as follows:
passengerLimit: This is the limit, in terms of passengers, that a train can carry on
a single trip.
The class methods have the following behaviour:
dieselLocomotive: The constructor of the class. It has no features beyond the
default.
dieselLocomotive: This is the class destructor that will deallocate the memory
assigned by the class. It will also print out, "diesel locomotive removed", without
the quotation marks and ended by a new line.
getPassengerLimit: Getter for the class variables.
setPassengerLimit: Setters for the class variables.
determineRouteStatistics: This function needs to calculate the specic statistics for
the locomotive based on the map it is provided. The following key shows all the
specic elements that are pertinent to the locomotive:
1. M,N,P: Passenger Stations. The locomotive must pick up passengers from these
stations. Each station will produce a certain quantity of passengers. These are
(a) M: 50 passengers
(b) N: 25 passengers
(c) P: 10 passengers
2. O: Origin Points. This is where the trains will be expected to leave from.
3. E: Exit Points. This is where the train is expected to go towards.
4. #: Railroad. This is traversable tracks for the train. Locomotives can only
travel on the map where there is track laid.
The function will then determine a number of statistics and print them to the screen
in a neatly formatted way:
1. Distance: Distance from the origin to exit in units, where one "#" is one unit
so a track of "### " is a 3 unit long track. This does not include the origin
and exit points. It will include the passenger stations.
2. Journey Status: This will display: "Viable" or "Not Viable" depending on
whether the locomotive is capable of making the journey. To determine if the
journey will be viable, you need to determine if the train assigned to the map
can carry enough passengers for what the route will typically entail at most.
These stations are grouped into what they typically produce in volume so it is
a reasonably safe bet to use their volume numbers when calculating. Basically,
you must calculate how many passengers the locomotive will take on during its
journey and if that number is greater than its carrying capacity, the journey
is not viable. Otherwise it will be viable. You will need to display how many
passengers the train is expected to pick up on its journey as well.
Display the information as follows:
Name: Frontier Express
Origin Coordinates: 1,2
Exit Coordinates: 8,7
Distance: 16
Passengers Carried: 75
Status: Viable
Finally an example small map is provided below:
O#--
-M--
-P--
-##E
You will be allowed to use the following libraries: fstream, cstring, string, iostream.
Your submission must contain vehicle.h, vehicle.cpp, dieselLocomotive.h, dieselLocomotive.cpp, map1.txt,
main.cpp
///////////////////////////////////////////////////////////////////////////
// Driver Program to Run this
//////////////////////////////////////////////////////////////////////////
#include"Vehicle.h"
#include"DieselLocomotive.h"
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
using namespace std;
int main() {
string mapFile = "map.txt";
DieselLocomotive train;
cout<<"Train Created!!!"<<endl;
train.setName("Frontier Express");
train.setMap(mapFile);
train.determineRouteStatistics();
//train--;
return 0;
}
////////////////////////////////////////////////////////////
//Vehicle.h
#ifndef __VEHICLE__
#define __VEHICLE__
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
using namespace std;
class Vehicle {
public:
Vehicle(){}; //the default constructor of the
class
//~Vehicle(); // destructor for the class. It has been
made virtual
~Vehicle(){
for(int i = 0; i < getSize();
i++)
delete
map[i];
delete map;
cout<<"Diesel Locomotive
Removed"<<endl;
}
int getSize(); // returns the size of the map as a
square matrix
void setName(string s); //set the name of the vehicle
as received
string getName(); //return the name of the
vehicle
char** getMap(); //return the entire map
variable
void setMap(string s); // receives the name of a text
file that contains an ASCII map The map will be a square, equal
number of rows and columns. The first line of the map will have the
number of rows. Every line after will contain a number of ASCII
characters that you must read into the map. This must allocate
memory before assigning the map
char getMapAt(int x, int y); // receives
coordinates, (x, y), returns what character is located at those
coordinates. If the coordinates are out of bounds, return
':'.
virtual void determineRouteStatistics() = 0; // to
determine information from the map based on requirements specic to
the vehicle in question
// Overload --
// operator--(); // overload of this operator will
deallocate the memory allocated for the map
private:
char** map; // 2D array of chars, represents the map
that each vehicle will have to travel on
string name;// name of the vehicle. For example,
"Frontier Express"
int size; // Size of the map as a square
matrix
};
#endif /* __VEHICLE__ */
/////////////////////////////////////////////////////////////////
//Vehicle.cpp
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include"Vehicle.h"
using namespace std;
int Vehicle::getSize() {
return size;
}
void Vehicle::setName(string s) {
this->name = s;
}
string Vehicle::getName() {
return this->name;
}
char** Vehicle::getMap() {
return map;
}
void Vehicle::setMap(string fileName) {
ifstream in(fileName);
if(!in) {
cout<<"failed to open the
file!!!"<<endl;
exit(1);
} else {
cout<<"Reading Map
file"<<endl;
}
string line;
int size;
in>>size;
cout<<"size of map :
"<<size<<endl;
getline(in, line);
map = new char*[size];
for(int i = 0; i < size; i++) {
getline(in, line);
map[i] = new char[size];
strcpy(map[i], line.c_str());
}
}
char Vehicle::getMapAt(int x, int y) {
if(x >= size || y >= size || x < 0 || y <
0)
return ':';
else
return map[x][y];
}
///////////////////////////////////////////////////////////////////////////
// DieselLocomotive.h
#ifndef __DIESEL_LOCOMOTIVE__
#define __DIESEL_LOCOMOTIVE__
#include"Vehicle.h"
using namespace std;
class DieselLocomotive : public Vehicle {
public:
DieselLocomotive(){}; // constructor of the class. It
has no features beyond the default
~DieselLocomotive(){}; // destructor will deallocate
the memory assigned by the class. It will also print out, "diesel
locomotive removed", without the quotation marks and ended by a new
line.
// Getter/Setter for the class variables.
int getPassengerLimit();
void setPassengerLimit(int s);
void determineRouteStatistics();
private:
int passengerLimit; //the limit, in terms of
passengers, that a train can carry on a single trip
};
#endif /* __DIESEL_LOCOMOTIVE__ */
//////////////////////////////////////////////////////////////////////////////
// DieselLocomotive.cpp
/////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<string>
#include"DieselLocomotive.h"
using namespace std;
int DieselLocomotive::getPassengerLimit() {
return passengerLimit;
}
void DieselLocomotive::setPassengerLimit(int s) {
this->passengerLimit = s;
}
void DieselLocomotive::determineRouteStatistics() {
int oX, oY; // Origin Points
int eX, eY; // Exit Points
int passengers = 0; // number of passengers
int distanceTravelled = 0;
for(int x = 0; x < getSize(); x++) {
for(int y = 0; y < getSize();
y++ ) {
switch(getMapAt(x, y)) {
case 'O':
oX = x+1;
oY = y+1;
break;
case 'E':
eX = x+1;
eY = y+1;
break;
case 'M':
passengers += 50;
distanceTravelled++;
break;
case 'N':
passengers += 25;
distanceTravelled++;
break;
case 'P':
passengers += 10;
distanceTravelled++;
break;
case '#':
distanceTravelled++;
break;
default:
break;
}
}
}
//// PRINTING THE STATISTICS HERE ////
cout<<"Name\t\t\t\t:"<<this->getName()<<endl;
cout<<"Origin Coordinates\t:
("<<oX<<", "<<oY<<")"<<endl;
cout<<"Exit Coordinates\t:
("<<eX<<", "<<eY<<")"<<endl;
cout<<"Distance\t\t\t:
"<<distanceTravelled<<""<<endl;
cout<<"Passengers Carried\t:
"<<passengers<<endl;
cout<<"Status\t\t\t\t:
"<<(passengers>getPassengerLimit()?"Not
Viable":"Viable")<<endl;
}