Question
Starting with v.1, writeRoutes.2.cpp, to design and test a class that represents a route between two cities along a path made up of adjacent Leg
- Two Private Data Members
Write twoprivate data members: a bag to hold the Leg objects that make up the route, and a numeric member for the total distance -- the sum of all the Leg objects making up the path. The distance should beconstant.
Since the bag will hold only Leg objects, there's no need for generic pointers (that require type casting). So use avector of read-only Leg pointersinstead of read-only generic pointers -- it saves a step.
- Two Route Constructor Func!ons
Include twoconstructors-- one to create a Route object consisting of only one Leg, and another to create a new Route object by appending a Leg object to the end of an existing Route object. (Our solution in the next lab assignment generates new routes by adding Leg objects one-at-a-time to a growing collection of existing Route objects.)
Throw an exception from the second constructor if the start city of the appended Leg object does not match the end city of the last Leg in the bag. Use any data type you like for the exception handling.
You'll have to figure out how to set the constant "total distance" data member in these constructors. Use the Q&A section of this module to share ideas with classmates.
- Two Getter Functions
Writetwogetters in class Route -- one to return the distance and another to produce nicely formatted output.
The output getter should have one parameter -- an ostream reference, so that output can be directed to any stream-oriented source. The "nicely formatted output" should look something like this: "Route: San Francisco to Sacramento to Las Vegas to Salt Lake City to Denver to Oakley to Kansas City to Springfield to Columbus to Washington to New York City, 3502 miles". You decide on the exact appearance, but donotrepeat any city name, anddoinclude the start city of the first Leg and the end city of the last Leg andeverycity in between.
Hint: the Route class will need access to the Leg class' private data members, and no getter function was specified for city names. There's another way to make this happen... Use the Q&A section of this module to share ideas with classmates.
Route class and Leg class declarations:
- An Array Of Route Objects
In the main program declare an array of at least 10 Route objects. At index zero, create a Route object using a Leg object of your choice. At index one, create an object using the zeroth Route and a Leg that starts where the zeroth Route ends. At index 2, create an object using the Route object at index 1 and a Leg that starts where the index 1 Route ends, and so on.
Hint: this is actually really easy, so don't overthink it. Each time you create a Route object at an index of the array, that Route becomes available for use in creating the Route object at the next index. But do thisbeforesorting the Leg objects by distance.
- Sort By Distance
In the main program write a nested-for-loop sorting code block, sorting the Route objects in their array fromlongestdistance toshortest. This will require swapping of objects, whichwill requirean assignment operator function in the Route class. Because there is a constant data member, you should write an assignment operator function -- not just because it's good programming practice, but in this case it's required because of the swapping.
- Output
In a loop in the main program, call the output getter on each object in the Route array. Do soaftersorting the Route objects from longest to shortest. Refer to sample output from Files, Assignment5Part2.pdf
//My code:
#include
#include
#include
usingnamespacestd;
classLeg{
constchar*conststartCity;
constchar*constendCity;
constdoubledistance;
public:
Leg(constchar*const,constchar*const,constdouble);
Leg&operator=(constLeg&);
doublegetDistance()const;
voidoutput(ostream&)const;
};
intmain() {
Leg a[]={
Leg("San Fransisco", "Concord", 31),
Leg("Walnut Creek", "Pleasant Hill", 4),
Leg("Irvine", "Long Beach", 31),
Leg("Chico", "Corona", 514),
Leg("Acme", "Albion", 2343),
Leg("Detroit", "Watervliet", 1966),
Leg("Coloma", "Saginaw", 2301),
Leg("Richland", "Glenn", 719),
Leg("Midland", "Brooklyn", 1887),
Leg("Aberdeen", "Abilene", 4642),
Leg("Akron", "Albany", 2470),
Leg("Albuquerque", "Alexandria", 1074),
Leg("Allentown", "Amarillo", 1626),
Leg("Anaheim", "Anchorage", 3520),
Leg("Ann Arbor", "Antioch", 2335),
Leg("Apple Valley", "Appleton", 2208),
Leg("Arlington", "Arvada", 1453),
Leg("Asheville", "Athens", 163),
Leg("Atlanta", "Atlantic City", 828),
Leg("Augusta", "Aurora", 934),
Leg("Austin", "Bakersfield", 1745),
Leg("Baltimore", "Barnstable", 967),
Leg("Baton Rouge", "Beaumont", 567),
Leg("Bel Air", "Bellevue", 2131),
Leg("Berkeley", "Bethlehem", 865),
Leg("Billings", "Birmingham", 1923),
Leg("Bloomington", "Boise", 380),
Leg("Boise City", "Bonita Springs", 996),
Leg("Boston", "Boulder", 1988),
Leg("Bradenton", "Bremerton", 786),
Leg("Bridgeport", "Brighton", 2879),
Leg("Brownsville", "Bryan", 1995),
Leg("Buffalo", "Burbank", 2538),
Leg("Burlington", "Cambridge", 725),
Leg("Canton", "Cape Coral", 1156),
Leg("Carrollton", "Cary", 2897),
Leg("San Diego", "Santa Cruz", 465),
Leg("Santa Cruz", "San Fransisco", 73),
Leg("San Fransisco", "Davis", 74),
Leg("Davis", "Chico", 92),
Leg("Chico", "Seattle", 660)
};
constintSIZE =sizeof(a) /sizeof(a[0]);
for(inti = 0; i
for(intj = i + 1; j
if(a[j].getDistance()
swap(a[i], a[j]);
}
}
}
for(inti=0;i
a[i].output(cout);
return0;
}
Leg::Leg(constchar* startCity,constchar* endCity,constdoubledistance): startCity(startCity), endCity(endCity), distance(distance)
{}
doubleLeg::getDistance()const{
returndistance;
}
voidLeg:: output(ostream& out)const
{
out"leg:>
}
Leg& Leg::operator=(constLeg& copyThis) {
if(this!= This) {
const_cast(this->distance) = copyThis.distance;&>
const_cast(this->startCity) = copyThis.startCity;char*>
const_cast(this->endCity) = copyThis.endCity;char*>
}
return*this;
}
//Output
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