Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In a series of three programming assignments we'll develop a shortest - route solution for a map of interconnected cities, where each city connects only
In a series of three programming assignments we'll develop a shortestroute solution for a map of interconnected cities, where each city connects only to its closest neighbors.
The solution will be complicated. It requires three classes: Leg to be developed in this lab Route to be developed in v and ShortestRoute to be developed in v The purpose of Leg is to represent the fact that two points in a network are connected, and to store the distance separating the two. The purpose of the Route class is to link adjacent Legs to form a route connecting any two points in the network.
The ShortestRoute class contains a recursive algorithm to find the shortest route between two selected points.
Do not use C strings or the string library anywhere in the Routes application, in this v or in any future versions. Let's get some experience using C strings. Also, the overhead is way less in this application using C strings.
Write Routescpp to design and test a class that represents a leg of a route between two adjacent cities. Here are the specifications for the Leg class:
Write three private data members: the starting city, the ending city, and the distance in miles separating the two.
Use constant, readonly pointers to store the city names as C strings. There will be no need to ever make copies of the names no strcpy Names will be declared and stored in the main program, so all that needs to be stored in the Leg object are the memory locations of those C strings.
For the distance, use any numeric data type of your choosing you decide on whole numbers vs floating point. It will never change, so it needs to be constant.
Include one constructor with three parameters the start and end cities and the distance separating them. Do not include a default constructor! There can be no "generic" or uninitialized Leg. Each Leg is very specific and constant and unchanging.
Write two getters one to return the distance and another to produce nicely formatted output.
The output getter would have one parameter an ostream reference, so that output can be directed to any streamoriented source. The "nicely formatted output" should look something like this: "Leg: San Francisco to San Jose, miles" but you decide on the exact appearance.
The declaration of the Leg class should be like,
class Leg
const char const startCity;
const char const endCity;
const double distance;
public:
Legconst char const, const char const, const double;
Leg& operatorconst Leg&; for swap
double getDistance const;
void outputostream& const;
;
In the main program declare an array of or more Leg objects, each connecting two cities of your choice. They can be any pairs of cities that you choose, but follow these rules:
Do not make legs where each new leg starts where the preceding leg ends. That is do not make one long list of legs following Interstate from San Francisco to New York City. Instead choose major cities across the United States, connecting adjacent cities.
Do include at least one set of legs that do form a "route" when taken endtoend. For this to happen you'll need to spell and case the end city of one leg exactly the same as the start city for some other leg so that they "connect".
Use sizeof to get the size of the Leg Objects array
It's okay to estimate the distances between cities exactness of your numbers will not be checked. But be reasonable!
In the main program write a nestedforloop sorting code block, sorting the Leg objects in the array from shortest distance to longest. This will require swapping of objects, which will require an assignment operator function in the Leg class. Because there are constant data members, you should write an assignment operator function just because it's good programming practice but in this case, it's required because of the swapping.
In a loop in the main program, call the output getter on each object in the array. The output should be arranged shortest to longest, because the sorting code block should be before the output code block in the main program. Refer to sample output from the image.
Do not add getters, setters, constructors, destructors, friends, or statics to a class, other than the ones specified. If a public function name is given in the specification, spell and case it exactly. If parameters are specified, write them exactly as specified do not add or remove parameters or change their type.
Do not add public data members unless specified. Public variables will never be specified, although public constants might be in a future lab assignment If that happens, name and case them exactly as specified.
Private members don't matter unless specifically told not to you may add private functions and data.
The output should list the legs, shortest to longest.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started