a dd f u n c t i on a l i t y t o DegMinSec cla s s f or a dd i ti o n ( + ) , s
ID: 3549513 • Letter: A
Question
add functionality to DegMinSec class for addition (+), subtraction (-), conversion to double, assignment from a double, and output << operations. Then come up with two derived classes Latitude and Longitude that offer enhanced toString() and output functionality. In particular latitudes will be suffixed with "N" and "S" rather than being written as positive or negative numbers, and longitudes will be suffixed with "E" and "W" rather than being written as positive or negative numbers.
-------------------------------------------------------------------------
#include <string>
#include <sstream>
#include <cmath>
using namespace std;
class DegMinSec
{
private:
int degree, minute;
double second;
bool negative;
void convert(double d)
{
negative = false;
if (d < 0.0)
{
negative = true;
d *= -1.0;
}
double base = floor(d);
double frac = d - floor(d);
degree = int(floor(d))%360;
double dmin = floor(60.0*frac);
minute = int(dmin);
double fracMin = 60.0*frac - dmin;
double dsec = round(60.0*fracMin);
second = dsec;
}
double toDouble() const
{
double result = (double)degree;
result += (double)minute/60.0;
result += (double)second/3600.0;
if (negative)
{
result *= -1.0;
}
return result;
}
public:
//TODO other member function, access and/or virtual qualifiers
DegMinSec(int d, int m, double s)
{
negative = false;
if (d < 0)
{
negative = true;
d *= -1;
}
degree = d % 360;
minute = m % 60;
second = fmod(s, 60.0);
}
DegMinSec(double d)
{
convert(d);
}
string toString()
{
stringstream ss;
if (negative)
{
ss << "-";
}
ss << degree << "deg " << minute << "' " << second << """;
return ss.str();
}
};
// TODO other operators... +,<<, -
// Hint
ostream& operator<< (ostream& os, DegMinSec& dms)
{
os << dms.toString();
return os;
}
class Longitude: public DegMinSec
{
public:
static bool negNotWest; //Used to indicate whether "-" or "W" is used in toString() formatting.
//TODO constructor(s), toString
};
class Latitude: public DegMinSec
{
public:
static bool negNotSouth;
//TODO constructors, toString
};
bool Longitude::negNotWest = false;
bool Latitude:: negNotSouth = false;
int main()
{
cout << "Hello Degrees, Minutes, Seconds from Your Name Here!" << endl;
//Test construction
DegMinSec c1(37, 34, 19), c2(37.571944);
double decC1 = c1;
// Test conversion and overloading
cout << c1 << " in decimal is: " << (double) decC1 << endl;
cout << c2 << endl;
// Test assignment overloading
c1 = 39.873421;
cout << c1.toString() << " as decimal: " << c1 << endl;
// Test addition and subtraction
DegMinSec c3 = c1 + c2;
cout << "Sum: " << c3 << endl;
DegMinSec c4 = c1 - c2;
DegMinSec c5 = c2 - c1;
cout << "Difference: " << c4 << endl;
// Test inheritance with North East tip of Treasure Island coordinates.
Longitude TILong(-122.371789 );
Latitude TILat(37.834142);
cout << "TI lat: " << TILat << " TI long: " << TILong << " as decimal: " << (double)TILong << endl;
return 0;
}
Explanation / Answer
Below is the updated Code:
#include <string>
#include <sstream>
#include <cmath>
using namespace std;
class DegMinSec
{
private:
int degree, minute;
double second;
bool negative;
void convert(double d)
{
negative = false;
if (d < 0.0)
{
negative = true;
d *= -1.0;
}
double base = floor(d);
double frac = d - floor(d);
degree = int(floor(d))%360;
double dmin = floor(60.0*frac);
minute = int(dmin);
double fracMin = 60.0*frac - dmin;
double dsec = round(60.0*fracMin);
second = dsec;
}
double toDouble() const
{
double result = (double)degree;
result += (double)minute/60.0;
result += (double)second/3600.0;
if (negative)
{
result *= -1.0;
}
return result;
}
public:
//other member function, access and/or virtual qualifiers ---ADDED: See below
void setDegree(int degree)
{
this.degree=degree;
}
void setMinute(int minute)
{
this.minute=minute;
}
void setSecond(double second)
{
this.second=second;
}
int getMinute()
{
return minute;
}
int getDegree()
{
return degree;
}
double getSecond()
{
return second;
}
DegMinSec(int d, int m, double s)
{
negative = false;
if (d < 0)
{
negative = true;
d *= -1;
}
degree = d % 360;
minute = m % 60;
second = fmod(s, 60.0);
}
DegMinSec(double d)
{
convert(d);
}
string toString()
{
stringstream ss;
if (negative)
{
ss << "-";
}
ss << degree << "deg " << minute << "' " << second << """;
return ss.str();
}
};
// other operators... +,<<, -
// Hint ---See below
DegMinSec operator+(const DegMinSec& other)
{
double result_second = second + other.second;
int result_min = minute + other.minute;
int result_degree=degree + other.degree;
return DegMinSec( result_degree, result_min, result_second );
}
DegMinSec operator-(const DegMinSec& other)
{
double result_second = second - other.second;
int result_min = minute - other.minute;
int result_degree=degree - other.degree;
return DegMinSec( result_degree, result_min, result_second );
}
DegMinSec operator=(const DegMinSec& other)
{
this.second = other.second;
this.minute = other.minute;
this.degree =other.degree;
return *this;
}
ostream& operator<< (ostream& os, DegMinSec& dms)
{
os << dms.toString();
return os;
}
class Longitude: public DegMinSec
{
public:
static bool negNotWest; //Used to indicate whether "-" or "W" is used in toString() formatting.
//constructor(s), toString --See below
Longitude(double d))
{
super(d);
}
string toString()
{
stringstream ss;
if (negNotWest)
{
ss << "W";
}
else ss<< "E";
}
};
class Latitude: public DegMinSec
{
public:
static bool negNotSouth;
//constructors, toString --See below
Latitude(double d)
{
super(d);
}
string toString()
{
stringstream ss;
if (negNotSouth)
{
ss << "S";
}
else
ss<< "S";
}
};
bool Longitude::negNotWest = false;
bool Latitude:: negNotSouth = false;
int main()
{
cout << "Hello Degrees, Minutes, Seconds from Your Name Here!" << endl;
//Test construction
DegMinSec c1(37, 34, 19), c2(37.571944);
double decC1 = c1;
// Test conversion and overloading
cout << c1 << " in decimal is: " << (double) decC1 << endl;
cout << c2 << endl;
// Test assignment overloading
c1 = 39.873421;
cout << c1.toString() << " as decimal: " << c1 << endl;
// Test addition and subtraction
DegMinSec c3 = c1 + c2;
cout << "Sum: " << c3 << endl;
DegMinSec c4 = c1 - c2;
DegMinSec c5 = c2 - c1;
cout << "Difference: " << c4 << endl;
// Test inheritance with North East tip of Treasure Island coordinates.
Longitude TILong(-122.371789 );
Latitude TILat(37.834142);
cout << "TI lat: " << TILat << " TI long: " << TILong << " as decimal: " << (double)TILong << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.