Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include Distance.h #include #include Distance::Distance ( ) { feet = 0 ; inches = 0 . 0 ; } Distance::Distance ( unsigned ft ,

#include "Distance.h"
#include
#include
Distance::Distance(){
feet =0;
inches =0.0;
}
Distance::Distance(unsigned ft, double in) : feet(ft), inches(in){
init();
}
Distance::Distance(double in) : feet(0), inches(in){
init();
}
unsigned Distance::getFeet() const {
return feet;
}
double Distance::getInches() const {
return inches;
}
double Distance::distanceInInches() const {
return (feet *12)+ inches;
}
double Distance::distanceInFeet() const {
return feet +(inches /12);
}
double Distance::distanceInMeters() const {
return distanceInInches()*0.0254;
}
const Distance Distance::operator+(const Distance &rhs) const {
double totalInches = distanceInInches()+ rhs.distanceInInches();
return Distance(totalInches);
}
const Distance Distance::operator-(const Distance &rhs) const {
double diffInches = distanceInInches()- rhs.distanceInInches();
if(diffInches <0){
diffInches =-diffInches; // Ensure that Distance is always positive
}
return Distance(diffInches);
}
ostream& operator<<(ostream &out, const Distance &rhs){
out << rhs.getFeet()<<"'"<< rhs.getInches()<<"\"";
return out;
}
void Distance::init(){
if (inches >=12){
feet += static_cast(inches)/12;
inches = fmod(inches,12);
}
}#include
using namespace std;
class Distance {
public:
/* Constructs a default Distance of 0(0 feet and 0.0 inches)
*/
Distance();
/* Constructs a distance of ft feet and in inches,
unless in >=12, in which case the values of feet and inches
are adjusted accordingly. A Distance will always be positive.
Just convert negative inches to positive. Don't subtract from feet.
*/
Distance(unsigned ft, double in);
/* Constructs a distance of 0 ft and in inches,
unless in >=12, in which case the values of feet and inches
are adjusted accordingly. A Distance will always be positive.
Just convert negative inches to positive. Don't subtract from feet.
*/
Distance(double in);
/* Returns just the feet portion of the Distance
*/
unsigned getFeet() const;
/* Returns just the inches portion of the Distance
*/
double getInches() const;
/* Returns the entire distance as the equivalent amount of inches.
(e.g.,4 feet 3.5 inches would be returned as 51.5 inches)
*/
double distanceInInches() const;
/* Returns the entire distance as the equivalent amount of feet.
(e.g.,3 feet 6 inches would be returned as 3.5 feet)
*/
double distanceInFeet() const;
/* Returns the entire distance as the equivalent amount of meters.
1 inch equals 0.0254 meters.
(e.g.,2 feet 8.12 inches would be returned as 0.815848 meters)
*/
double distanceInMeters() const;
/* Returns the sum of 2 Distances.
*/
Distance operator+(const Distance &rhs) const;
/* Returns the difference between 2 Distances.
*/
Distance operator-(const Distance &rhs) const;
/* Outputs to the stream out the Distance in the format:
feet' inches''(i.e.3'3.41'')
*/
friend ostream & operator<<(ostream &out, const Distance &rhs);
private:
/* Used by the 2 parameterized constructors to convert any negative values to positive and
inches >=12 to the appropriate number of feet and inches.
*/
void init();
unsigned feet;
double inches;
};
#include
using namespace std;
#include "Distance.h"
int main(){
Distance d1;
cout <<"d1: "<< d1<< endl;
Distance d2= Distance(2,5.9);
Distance d3= Distance(3.75);
cout <<"d2: "<< d2<< endl;
cout <<"d3: "<< d3<< endl;
//test init helper function
Distance d4= Distance(5,19.34);
Distance d5= Distance(100);
cout <<"d4: "<< d4<< endl;
cout <<"d5: "<< d5<< endl;
//test add (<12 inches)
cout <<"d4+ d5: "<<(d4+ d5)<< endl;
//test add (>12 inches)
cout <<"d2+ d4: "<<(d2+ d4)<< endl;
//test sub (0 ft)
cout <<"d3- d1: "<<(d3- d1)<< endl;
//test sub (0 ft, negative conversion)
cout <<"d1- d3: "<<(d1- d3)<< endl;
//test sub (positive ft & inch)
cout <<"d4- d2: "<<(d4- d2)<< endl;
//test sub (negative ft & inch)
cout <<"d2- d4: "<<(d2- d4)<< endl;
//test sub (negative ft, positive inch)
cout <<"d4- d5: "<<(d4- d5)<< endl;
//test sub (positive ft, negative inch)
cout <<"d5- d2: "<<(d5- d2)<< endl;
return 0;
mistake:Distance.cpp:38:16: error: no declaration matches const Distance Distance::operator+(const Distance&) const
38| const Distance Distance::operator+(const Distance &rhs) const {
|^~~~~~~~
In file included from Distance.cpp:1:
Distance.h:60:13: note: candidate is: Distance Distance::operator+(const Distance&) const
60| Distance operator+

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Oracle Fusion Applications Installation And Administration

Authors: Tushar Thakker

1st Edition

1484209834, 9781484209837

More Books

Students also viewed these Databases questions