In: Computer Science
Imagine that you are working for a logistics company
operating out in the frontiers of
civilisation. Your boss wants to be able to run some basic
analytics on some of the
routes that you currently support and wants you to implement some
systems to help
her. However the current limitations of the legacy system prevent
you from making
fundamental changes. Instead, you will need to use inheritance in
order to achieve your
goal.
Vehicle Class
The vehicle class is the parent class of the derived class: maglev.
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 vehicle as set.
• getMap(): This will return the entire map variable.
• setMap(): This method 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. The textfile that you will receive in input has
no delimiters.
On each line, all the characters are written out with no spaces or
commas or other
delimiters separating them. For example:
-#--
• 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 specific 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.
Maglev Class
The description of the maglev class is given by the simple UML
diagram below:
maglev
-perUnitCost: double
---------------------------------
+maglev()
+~maglev()
+getUnitCost():double
+setUnitCost(s:double):void
+calculateStraightDistance():double
+determineRouteStatistics():void
The class variables are as follows:
• perUnitCost: This is the cost of moving the maglev from one part
of the track to
the other a distance of one unit of rail.
The class methods have the following behaviour:
• maglev: The constructor of the class. It has no features beyond
the default.
• ∼maglev: This is the class destructor that will deallocate the
memory assigned by
the class. It will also print out, ”maglev removed”, without the
quotation marks
and ended by a new line.
• getUnitCost: This returns the per unit cost.
• setUnitCost: This sets the per unit cost.
• determineRouteStatistics: This function needs to calculate the
specific statistics for
the locomotive based on the map it is provided. The following key
shows all the
specific elements that are pertinent to the locomotive:
1. O: Origin Points. This is where the trains will be expected to
leave from.
2. E: Exit Points. This is where the train is expected to go
towards.
3. #: 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. calculateStraightDistance: This will calculate a straight line
distance, using
the Euclidean distance metric, between the origin and exit
components in the
map. It will return this distance.
2. 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.
3. Journey Cost: This is the cost of operating the maglev on the
track. It is the
perUnitCost of the maglev multiplied against the number of tracks
the maglev
has to move over in the map.
4. Straight Distance: This is a straight line distance calculation
from the origin
to the exit position on the map. This would represent the path of a
flying
vehicle, such as a plane, moving directly between the two
stations.
Display the information as follows:
Name: Frontier Maglev
Origin Coordinates: 1,2
Exit Coordinates: 8,7
Straight Distance: 13.5
Distance: 16
Journey Cost: 3000000
Finally an example small map is provided below:
O#--
-#--
-#--
-##E
You will be allowed to use the following libraries: cmath, fstream,
cstring, string,
iostream. You will have a maximum of 10 uploads for this task. Your
submission must
contain vehicle.h, vehicle.cpp, maglev.h, maglev.h,map1.txt,
main.cpp and a
makefile.
Please specify anything I have missed while submitting answer. If you facing any issue.
Vehicle.h
//==========================================
#ifndef VEHICLE_H
#define VEHICLE_H
#include<iostream>
#include<string>
using namespace std;
class vehicle{
public:
char ** map;
string name;
int size;
vehicle();
int getSize();
void setName(string s);
string getName();
char** getMap();
void setMap(string s);
char getMapAt(int x, int y);
virtual ~vehicle();
void operator−−();
virtual void determineRouteStatistics()=0;
};
#endif
//==========================================
vehicle.cpp
//=========================================
#ifndef VEHICLE_H
#include "vehicle.h"
#endif
#include<fstream>
vehicle::vehicle()
{
}
int vehicle::getSize()
{
return size;
}
void vehicle::setName(string s)
{
name = s;
}
string vehicle::getName()
{
return name;
}
char** vehicle::getMap()
{
return map;
}
void vehicle::setMap(string s)
{
ifstream in(s.c_str());
int x;
in >> x;
size = x;
map = new char *[size];
for (int i = 0; i < size; i++)
{
map[i] = new char[size];
for (int j = 0; j < size;
j++)
{
in >>
map[i][j];
}
}
}
char vehicle::getMapAt(int x, int y) //x is horizontal so assuming
column.
{
if (x >= getSize() || y >= getSize() || x < 0
|| y < 0) return ':';
return map[y][x];
}
vehicle::~vehicle()
{
}
void vehicle::operator−−()
{
for (int i = 0; i < size; i++)
{
delete[] map[i];
}
delete[] map;
size = 0; // memory allocated is deleted so size
assigned to zero
}
//==========================================
maglev.h
//============================================
#ifndef MAGLEV_H
#define MAGLEV_H
#include<iostream>
#include<cmath>
using namespace std;
#include "vehicle.h"
class maglev:public vehicle
{
private:
double perUnitCost;
public:
maglev();
~maglev();
double getUnitCost();
void setUnitCost(double cost);
double calculateStraightDistance();
void determineRouteStatistics();
};
#endif
//===========================================
maglev.cpp
//==========================================
#ifndef MAGLEV_H
#include"Maglev.h"
#endif
maglev::maglev()
{
}
maglev::~maglev()
{
cout << "maglev removed" << endl;
}
double maglev::getUnitCost()
{
return perUnitCost;
}
void maglev::setUnitCost(double cost)
{
perUnitCost = cost;
}
double maglev::calculateStraightDistance()
{
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size;
j++)
{
if (map[i][j] ==
'O') { y1 = i; x1 = j; break; }
}
if (x1 != -1) break;
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size;
j++)
{
if (map[i][j] ==
'E') { y2 = i; x2 = j; break; }
}
if (x2 != -1) break;
}
int square_distance = (x1 - x2)*(x1 - x2) + (y1 -
y2)*(y1 - y2);
return (double)sqrtl((long
double)square_distance);
}
void maglev::determineRouteStatistics()
{
int x1=-1, y1=-1, x2=-1, y2=-1;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size;
j++)
{
if (map[i][j] ==
'O') { y1 = i; x1 = j; break; }
}
if (x1 != -1) break;
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size;
j++)
{
if (map[i][j] ==
'E') { y2 = i; x2 = j; break; }
}
if (x2 != -1) break;
}
double str_distance =
calculateStraightDistance();
int distance=0;
bool ** visited=new bool *[size];
for (int i = 0; i < size; i++)
{
visited[i] = new bool[];
for (int j = 0; j < size;
j++)
visited[i][j] =
0;
}
int k = y1; int j = x1;
while (getMapAt( k+ 1, j) != 'E' &&
getMapAt(k, j+1) != 'E' && getMapAt(k , j-1) != 'E'
&& getMapAt(k - 1, j) != 'E')
{
if(k+1<getSize()) if (getMapAt(k
+ 1, j) == '#' && visited[k][j] == 0) { visited[k][j] = 1;
distance++; k++; continue; }
if(k-1>=0) if
(getMapAt(k - 1, j) == '#' && visited[k][j] == 0) {
visited[k][j] = 1; distance++; k--; continue; }
if(j+1 <getSize())if
(getMapAt(k, j + 1) == '#' && visited[k][j] == 0) {
visited[k][j] = 1; distance++; j++; continue; }
if(j-1>=0) if
(getMapAt(k, j - 1) == '#' && visited[k][j] == 0) {
visited[k][j] = 1; distance++; j--; continue; }
}
double journey_cost = distance*getUnitCost();
cout << "Name: " << getName() <<
endl;
cout << "Origin co-ordinates: " << x1
<< "," << y1 << endl;
cout << "Exit Coordinates: " << x2
<< "," << y2 << endl;
cout << "Straight Distance: " <<
str_distance << endl;
cout << "Distance: " << distance <<
endl;
cout << "Journey Cost: " << journey_cost
<< endl;
}
//==========================================
main.cpp
//=======================================
#include<iostream>
#include<string>
#ifndef MAGLEV_H
#include"Maglev.h"
#endif
#ifndef VEHICLE_H
#include"vehicle.h"
#endif
int main()
{
maglev *locomotive = new maglev();
locomotive->setName("Steam");
cout << "Please provide input map file: ";
string file_name;
cin >> file_name;
locomotive->setMap(file_name);
locomotive->setUnitCost((double)2.01);
locomotive->determineRouteStatistics();
char** map = locomotive->getMap();
cout << "input map:" << endl;
for (int i = 0; i < locomotive->getSize();
i++)
{
for (int j = 0; j <
locomotive->getSize(); j++)
cout <<
map[i][j];
cout << endl;
}
locomotive--;
return 0;
}
//========================================
map1.txt
4
O#--
-#--
-#--
-##E
Makefile
all:
g++ main.cpp maglev.cpp vehicle.cpp -o main