Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- You are to write a complete City class in separate header (.h) and implementation (.cpp) files. Method implementations longer than a single line should be written in the City.cpp file.
- Attributes (all of which should be made private)
- string name - the name of the city
- double latitude - the latitude of the city in degrees. Lines of latitude north of the equator are to be expressed as positive numbers between 0 and 90 while lines of latitude south of the equator are to be expressed as negative numbers between 0 and 90.
- double longitude - the longitude of the city in degrees. Lines of longitude west of the Prime Meridian are to be expressed as negative numbers between 0 and 180 while lines of latitude east of the Prime Meridian are to be expressed as positive numbers between 0 and 180.
- Public Methods
- City() - default constructor, sets name to the empty string (""), and latitude and longitude to 0.0
- City(string nm, double lati, double longi) - constructor that sets the class attributes to the value of the parameters. An invalid_argument error should be thrown if either latitude or longitude has an invalid value.
- setters - one for each attribute (e.g. setName(string nm) changes the name of a city). An invalid_argument error should be thrown by the setters for latitude and longitude if the parameter is passed has an invalid value. Note that you will not use these setters in the assignment - but you still need to write them!
- getters - one for each attribute (e.g. getName() returns name)
- double distance(const City & c) const - returns the distance between the calling object and the City parameter. You should calculate the distance using the spherical law of cosines, the formula for which is given at the end of this section.
- Overloaded Operator
- You must overload the << operator so that a city is displayed like this:
- Vancouver 49.2 N, 123.1 W //i.e. <name> <latitude> N/S, <longitude> W/E
- Distance and the Spherical Law of Cosines
- As the Earth is roughly (well not all that roughly actually) a sphere it is not useful to calculate the straight line distance between two locations as that would suggest that a traveler was burrowing from city to city! It is therefore necessary to calculate the length of the arc between the locations. This is a somewhat complex formula but fortunately it is not too difficult to apply without really understanding it (as I can tell you from personal experience).
- The distance between two locations with latitude and longitude {lat1, long1}, and {lat2, long2} can be calculated as follows.
- distance = acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(long2βlong1) ) * earth radius
- Notes on Implementation
- You can make use of the C++ standard math library (cmath) which (amongst other things) includes these functions:
- double cos(double angle) - returns the cosine of the angle
- double sin(double angle) - returns the sine of the angle
- double acos(double angle) - returns the arc cosine of the angle
- All these functions expect their input to be in radians, whereas our latitude and longitude values are in degrees. To convert from degrees to radians multiply by pi/180. Finally, you will need to know that the radius of the earth is 6,371 kilometres. I would suggest recording both pi and the radius of the earth as constants in the city header file.
Advertisement
Add Comment
Please, Sign In to add comment