Question

In: Computer Science

C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive....

C++ Code

Vehicle Class

The vehicle class is the parent class of a derived class: locomotive. 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()

+setName(s:string):void

+getName():string

+getMap():char**

+getSize():int

+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.

• 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 thememory allocated for

the map.

2.2.2 Locomotive Class

The description of the locomotive class is given by the simple UML diagram below:

locomotive

-supplyRange:int

---------------------------------

+locomotive()

+~locomotive()

+getSupplyRange():int

+setSupplyRange(s:int):void

+determineRouteStatistics():void

The class variables are as follows:

• supplyRange: This is the range, in terms of units (a unit being the distance between

one map coordinate and another) that the locomotive can travel on one trip. The

locomotive will require both coal and water in order to function and can only get it

at supply locations on the map which are marked by a ”*”. A supply location will

always be directly next to or adjacent to a railway, if one exists.

The class methods have the following behaviour:

• locomotive: The constructor of the class. It has no features beyond the default.

• ~locomotive: This is the class destructor that will deallocate the memory assigned

by the class. It will also print out, ”locomotive removed”, without the quotation

marks and ended by a new line.

• getSupplyRange: Getters for the class variables.

• setSupplyRange: Setters for the class variables.

• 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. *: Supply Stations. These supply the locomotive with coal and water.

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.

2. Number of Supply Stations: Determine the number of supply stations that

support the railway.

3. Journey Status: This will display: ”Viable” or ”Not Viable” depending on

whether the locomotive is capable of making the journey. To determine this,

calculate the distance from the origin to the exit as units. If the train can

reach this distance with its current supply distance, then the trip is viable. If

there is at least one supply station, then the trip will be viable. If not, it will

not be viable.

Display the information as follows:

Name: Frontier Express

Supply Range: 12 units

Origin Coordinates: 1,2

Exit Coordinates: 8,7

Distance: 16

Number of Stations: 2

Status: Viable

Finally an example small map is provided below:

O#--

-#*-

-#--

-##E

You will be allowed to use the following libraries: sstream,fstream, cstring, string,

iostream. Your submission must contain vehicle.h, vehicle.cpp, locomotive.h, locomotive.cpp, map1.txt, main.cpp

Solutions

Expert Solution

#######################################
      locomotive.cpp
#######################################
#include "locomotive.h"
#include<fstream>
#include<cstring>

Locomotive::Locomotive() {
        supplyRange = 0;
}
Locomotive::~Locomotive() {
        cout << "Locomotive removed" << endl;
}
int Locomotive::getSupplyRange() {
        return supplyRange;
}
void Locomotive::setSupplyRange(int s) {
        supplyRange = s;
}

// position(r, c) is where we start, We need to reach till
// we get cell 'E'
// Also, We keep a boolean array to mark the cells which
// are already visited
int findDistance(char **map, int size, int r, int c, bool **visited) {
        // if current cell is invalid or visited, Then return -1 to show error
        if(r < 0 || r >= size || c < 0 || c >= size 
                || visited[r][c] || map[r][c] == '-') {
                return -1;
        }

        visited[r][c] = true;
        if(map[r][c] == 'E') {
                return 0;
        }

        // Try visiting to neighbors.
        int dis = findDistance(map, size, r+1, c, visited);
        if(dis != -1) {
                return 1 + dis;
        }
        dis = findDistance(map, size, r-1, c, visited);
        if(dis != -1) {
                return 1 + dis;
        }
        dis = findDistance(map, size, r, c+1, visited);
        if(dis != -1) {
                return 1 + dis;
        }
        dis = findDistance(map, size, r, c-1, visited);
        if(dis != -1) {
                return 1 + dis;
        }

        return -1; // Not possible to reach
}

void Locomotive::determineRouteStatisitcs() {
        char **map = Vehicle::getMap();
        int size = Vehicle::getSize();

        int originRow, originCol;
        int exitRow, exitCol;
        int supplyStations = 0;
        for(int i=0; i<size; i++) {
                for(int j=0; j<size; j++) {
                        if(map[i][j] == '*') {
                                supplyStations++;
                        }
                        if(map[i][j] == 'O') {
                                originRow = i;
                                originCol = j;
                        }
                        if(map[i][j] == 'E') {
                                exitRow = i;
                                exitCol = j;
                        }
                }
        }

        // TODO, Find the logic for reaching to the destination
        // And it the trip is viable
        bool **visited = new bool*[size];
        for(int i=0; i<size; i++) {
                visited[i] = new bool[size];
                for(int j=0; j<size; j++) {
                        visited[i][j] = false;
                }
        }

        cout << "Name: " << Vehicle::getName() << endl;
        cout << "Supply Range: " << supplyRange << endl;
        cout << "Origin Coordinate: " << originRow << "," << originCol << endl;
        cout << "Exit Coordinate: " << exitRow << "," << exitCol << endl;

        int distance = findDistance(map, size, originRow, originCol, visited);
        if(distance == -1) {
                cout << "End location can not be reached from origin." << endl;
        } else {
                cout << "Distance: " << distance << endl;
                cout << "Number of Stations: " << supplyStations << endl;
                if(distance <= supplyRange || supplyStations > 0) {
                        cout << "Status: Viable" << endl;
                } else {
                        cout << "Status: Not Viable" << endl;
                }
        }
        
        // free the visited array we created for tracking.
        for(int i=0; i<size; i++) {
                delete [] visited[i];
        }
        delete [] visited;
}




#######################################
        locomotive.h
#######################################
#ifndef LOCOMOTIVE_H
#define LOCOMOTIVE_H

#include <iostream>
#include "vehicle.h"

using namespace std;

class Locomotive: public Vehicle {
        
        int supplyRange;

        public:
        Locomotive();
        ~Locomotive();
        int getSupplyRange();
        void setSupplyRange(int s);
        void determineRouteStatisitcs();
};


#endif



#######################################
            main.cpp
#######################################
#include "locomotive.h"

int main() {
        Locomotive l;

        l.setSupplyRange(12);
        l.setName("Frontier Express");
        l.setMap("map.txt");

        l.determineRouteStatisitcs();

        return 0;
}



#######################################
             map.txt
#######################################
4
O#--
-#*-
-#--
-##E



#######################################
         vehicle.cpp
#######################################
#include "vehicle.h"
#include<fstream>
#include<cstring>

Vehicle::Vehicle() {
        size = 0;
        map = NULL;
        name = "";
}

void Vehicle::setName(string s) {
        name = s;
}
string Vehicle::getName() {
        return name;
}
char** Vehicle::getMap() {
        return map;
}
int Vehicle::getSize() {
        return size;
}
void Vehicle::setMap(string s) {
        ifstream f(s.c_str());
        
        if(!f.fail()) {
                f >> size;
                map = new char*[size];

                for(int i=0; i<size; i++) {
                        string x;
                        f >> x;
                        map[i] = new char[size];
                        strcpy(map[i], x.c_str());
                }
        }
}
char Vehicle::getMapAt(int x, int y) {
        return map[x][y];
}
Vehicle::~Vehicle() {
        for(int i=0; i<size; i++) {
                delete [] map[i];
        }
        delete [] map;
}

void Vehicle::operator--() {
        for(int i=0; i<size; i++) {
                delete [] map[i];
        }
        delete [] map;
}




#######################################
           vehicle.h
#######################################
#ifndef VEHICLE_H
#define VEHICLE_H

#include <iostream>

using namespace std;

class Vehicle {
        char **map;
        string name;
        int size;

        public:
        Vehicle();
        void setName(string s);
        string getName();
        char **getMap();
        int getSize();
        void setMap(string s);
        char getMapAt(int x, int y);
        ~Vehicle();
        virtual void determineRouteStatisitcs() = 0;
        void operator--();
};


#endif


**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive....
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reect that appropriately in their .h les. 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...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a...
Write a c++ code: 2.2.1 Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so react that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +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...
C++ Code (I just need the dieselLocomotive Class) Vehicle Class The vehicle class is the parent...
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...
C++ Code Required to Show The constructor of parent class executes before child class
C++ Code Required to Show The constructor of parent class executes before child class
If a class A implements interface I1 and class C and D are derived from class...
If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct 1. A, C, and D 2. A and D
Here I'm using "person" as an abstract superclass or parent class, and "Student" as a derived/child...
Here I'm using "person" as an abstract superclass or parent class, and "Student" as a derived/child class. // File name: Person.h // Person is the base, or parent for chapter11 #pragma once #include <iostream> #include <string> using namespace std; class Person { private:    string fName;    string lName;    int areaCode;    int phone; public:    Person();    Person(string, string);    void setFirst(string);    void setLast(string);    void setPhoneNumber(int, int);    string getFirstlast();    string getLastFirst();    string getPhoneNumber();...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the opportunity to overload or override an inherited member function. What is the difference? and which one is the better?
Class object in C++ programming language description about lesson base class and derived class example.
Class object in C++ programming language description about lesson base class and derived class example.
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an...
C++ The following is a specification of three classes: Class Vehicle:       Attributes:       Age, an integer à The age of the vehicle       Price, a float à The price of the vehicle       Behaviors: Vehicle() à default constructor sets age=0, and price=0.0 setAge()   à Takes an integer parameter, returns nothing setPrice() à Takes a float parameter, returns nothing getAge()   à Takes no parameters, returns the vehicle’s age getPrice() à Takes no parameters, returns the vehicle’s price End Class Vehicle...
How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This...
How to code the following function in C? (NOT C++) int vehicleInsert(HashFile *pHashFile, Vehicle *pVehicle) This function inserts a vehicle into the specified file. • Determine the RBN using the driver's hash function. • Use readRec to read the record at that RBN. • If that location doesn't exist or the record at that location has a szVehicleId[0] == '\0': o Write this new vehicle record at that location using writeRec. • If that record exists and that vehicle's szVehicleId...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT