Page 1 of 1

Starting with v.1, write Routes.2.cpp, to design and test a class that represents a route between two cities along a pat

Posted: Fri Jul 01, 2022 5:52 am
by answerhappygod
Starting with v.1, write Routes.2.cpp, todesign and test a class that represents a route between two citiesalong a path made up of adjacent Leg objects. Here are thespecifications for the Route class:
Write two private data members: a bag to hold theLeg objects that make up the route, and a numeric member for thetotal distance -- the sum of all the Leg objects making up thepath. The distance should be constant.
Since the bag will hold only Leg objects, there's no need forgeneric pointers (that require type casting). So usea vector of read-only Leg pointers instead ofread-only generic pointers -- it saves a step.
Include two constructors -- one to create aRoute object consisting of only one Leg, and another to create anew Route object by appending a Leg object to the end of anexisting Route object. (Our solution in the next lab assignmentgenerates new routes by adding Leg objects one-at-a-time to agrowing collection of existing Route objects.)
Throw an exception from the second constructor if the startcity of the appended Leg object does not match the end city of thelast Leg in the bag. Use any data type you like for the exceptionhandling.
You'll have to figure out how to set the constant "totaldistance" data member in these constructors. Use the Q&Asection of this module to share ideas with classmates. (** add thedistance of new leg into the total distance of Route after thematch of the end city of the last Leg in the bag.)
Write two getters in class Route -- one toreturn the distance and another to produce nicely formattedoutput.
The output getter should have one parameter --an ostream reference, so that output can be directed toany stream-oriented source. The "nicely formatted output" shouldlook something like this: "Route: San Francisco to Sacramento toLas Vegas to Salt Lake City to Denver to Oakley to Kansas City toSpringfield to Columbus to Washington to New York City, 3502miles". You decide on the exact appearance, butdo not repeat any city name,and do include the start city of the first Legand the end city of the last Leg and every cityin between.
Hint: the Route class will need access to the Leg class'private data members, and no getter function was specified for citynames. There's another way to make this happen... Use the Q&Asection of this module to share ideas with classmates.
Route class and Leg class declarations:
In the main program declare an array of atleast 10 Route objects. At index zero,create a Route object using a Leg object of your choice. At indexone, create an object using the zeroth Route and a Leg that startswhere the zeroth Route ends. At index 2, create an object using theRoute object at index 1 and a Leg that starts where the index 1Route ends, and so on. Make sure to test the exception handling byadding the new leg not match the end city of last leg in thebag.
Hint: this is actually really easy, so don't overthink it.Each time you create a Route object at an index of the array, thatRoute becomes available for use in creating the Route object at thenext index. But do this before sortingthe Leg objects by distance.
If you correctly throw the exception and handled in the secondRoute constructor function with one existing Route and one new Leg,you may simply create the Route array by adding each Leg objectfrom the previous Route object as: (you may need tomodify the Leg objects array from Part 1 if no connections betweenany two Legs) The Leg array inPart 1 doesn't need to be identical as the Leg array in Part2.
In the main program write a nested-for-loop sorting code block,sorting the Route objects in their arrayfrom longest distance to shortest.This will require swapping of objects, which willrequire an assignment operator function in the Routeclass. Because there is a constant data member, you should write anassignment operator function -- not just because it's goodprogramming practice, but in this case it's required because of theswapping.
In a loop in the main program, call the output getter on eachobject in the Route array. Do so after sortingthe Route objects from longest to shortest. Refer to sample outputfrom Files, Assignment5Part2.pdf .
Actions
There are 40 Routes objects created in the sample output. Somethe Route objects are identical because of the start city of thenew Leg object different from the end city of the last Leg objectin the existing Route object. The exception been thrown andhandled.
Output should include the Routes sorted by distance (longest toshortest). You may optionally include the Legs sorted by distance(shortest to longest, appearing before or after the Routes per yourchoice. But if you do, think about it first!
If you do reorder your legs, do so AFTER you create your routeobject array!