Question
I need this in c++ please Write a Map class that is composed of a Location class. Location class has three private variable string nameOfCity,
I need this in c++ please
Write a Map class that is composed of a Location class. Location class has three private variable string nameOfCity, double variables, latitude and longitude. It has a constructor to initialize these and also the usual getters. It has three functions: string toString() which prints :double getFlyingTime(Location l) which returns the flying time between this and location l. It uses the formula that time = distance/speed. speed = 673 miles/hour. No need to convert to minutes. : double getDistance(Location l). This method is used by getFlyingTime. The Map class has function double getFlyingTime(string l, string r); It returns -1 if the two cities are not found in its database. Map class has a vector of 4 location objects. The driver program creates the map by providing the locations to the Map. Once the Map is created, it asks the user for two cities. If the cities are found, it returns the flying distance otherwise it says cities not found. The cities have first letter capitalized and rest small.
I need help implementing the main.cpp
//Location.h
#pragma once
#include
class Location
{
std::string nameOfCity;
double latitude;
double longitude;
public:
Location() = default;
Location(double lat, double longat)
{
latitude = lat;
longitude = longat;
}
std::string getNameOfCity();
double getLatitude();
double getLongitude();
std::string toString();
double getFlyingTime(Location l);
double getDistance(Location l);
};
//Location.cpp
#include "Location.h"
#include
using namespace std;
double DegreeToRadians(double degree)
{
return (degree* 3.14 / 180); // We can use PI from math librabry as well for precise values
}
std::string Location::getNameOfCity()
{
return nameOfCity;
}
double Location::getLatitude()
{
return latitude;
}
double Location::getLongitude()
{
return longitude;
}
std::string Location::toString()
{
return "Latitude: " + to_string(latitude) + ", Longitude: " + to_string(longitude);
}
double Location::getFlyingTime(Location l)
{
return getDistance(l) / 673;
}
double Location::getDistance(Location l)
{
double lat1r = DegreeToRadians(latitude);
double lon1r = DegreeToRadians(longitude);
double lat2r = DegreeToRadians(l.getLatitude());
double lon2r = DegreeToRadians(l.getLongitude());
double u = sin((lat2r - lat1r) / 2);
double v = sin((lon2r - lon1r) / 2);
return 2.0 * 6371.0 * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v)); // 6371.0 is Earth's radius
}
//Map.h
#pragma once
#include
#include "Location.h"
class Map
{
std::vector
public:
void addLocation(Location l);
double getFlyingTime(std::string l, std::string r);
};
//Map.cpp
#include "Map.h"
using ::Map;
void Map::addLocation(Location l)
{
for (auto& c : locations)
{
if (c.getNameOfCity() == l.getNameOfCity())
return;
}
locations.emplace_back(l);
}
double Map::getFlyingTime(std::string l, std::string r)
{
bool city1Found = false;
bool city2Found = false;
Location loc1;
Location loc2;
for (auto& c : locations)
{
if (c.getNameOfCity() == l)
{
loc1 = c;
city1Found = true;
}
if (c.getNameOfCity() == r)
{
city2Found = true;
loc2 = c;
}
}
if (city1Found && city2Found)
{
return loc1.getFlyingTime(loc2);
}
return -1;
}
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