Question

Your task will be to create a class called Distance, in the files distance.h and distance.cpp, which will involve a variety of operator overloads. A Distance object will store a quantity of distance in terms of miles, yards, feet, and inches. You will overload some basic operators for use with these objects including arithmetic, comparison, and insertion/extraction operators. These operators must work even for large distances (e.g. thousands of miles) and should not overflow the capacity of the storage variables (int) unless the number of miles is very close to the upper limit of int storage. Program Details and Requirements The Distance class must allow for storage of a non-negative quantity of distance in terms of miles, yards, feet, and inches using integer precision. All values should be non-negative. The data should always be maintained in a simplified form. For example, if you have 14 inches, this should be expressed as 1 foot and 2 inches. You should create appropriate member data in your class, all of which must be private. Remember that there are 12 inches in a foot, 3 feet in a yard, and 1760 yards in a mile. The only limit on the number of miles is that imposed by int storage (e.g. ~2 billion for 32 bit int) Public Interface 1. Constructors The class should have a default constructor (no parameters) which should initialize the object so that it represents the distance 0. The class should have a constructor with a single integer parameter which represents a quantity of inches. This should be translated into the appropriate notation for a Distance object. Note: this will be a conversion constructor that allows automatic type conversions from int to Distance. If the parameter is negative, default the Distance object to represent 0. The class should have a constructor that takes 4 parameters representing the miles, yards, feet, and inches to use for initializing the object. If any of the provided values are negative, default the Distance object to represent 0. If any of the provided values are too high (e.g. 13 inches), simplify the object to the appropriate representation. Examples: Distance t; Distance s (1234); // creates an object of 0 miles, 0 yards, 0 feet, 0 inches // creates an object of 0 miles, 34 yards, 0 feet, 10 inchesSo far I have this code but I'm not sure if it's right or how to finish it.

#include "distance.h"

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

Distance::Distance()

{

feet = 0;

miles = 0;

yards = 0;

inches = 0;

}

Distance::Distance(int inch)

{

  

}

Distance::Distance(int m, int y, int f, double i)

{

if(m < 0 || y < 0 || f < 0)

{

feet = 0;

miles = 0;

yards = 0;

}

else if (i < 0.0)

{

inches = 0;

}

if(i >= 12)

{

inches = 0;

feet++;

}

if(f >= 3)

{

feet = 0;

yards++;

}

if(y >= 1760)

{

yards = 0;

miles++;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below are the needed files. Output is shown. Please do rate the answer if it helped. Thank you.

compile: g++ distance.cpp main.cpp

distance.h


#ifndef distance_h
#define distance_h
#include <iostream>
using namespace std;
class Distance
{
private:
int miles;
int yards;
int feet;
int inches;
int toInches() const;
void setFromInches(int inches);
void set(int m, int y, int f, int i);
public:
Distance();
Distance(int inches);
Distance(int m, int y, int f, int i);
int getMiles();
int getYards();
int getFeet();
int getInches();
friend ostream& operator << (ostream &out, const Distance &d);
friend istream& operator >> (istream &in, Distance &d);
bool operator == (const Distance &other);
bool operator != (const Distance &other);
bool operator < (const Distance &other);
bool operator > (const Distance &other);
bool operator <= (const Distance &other);
bool operator >= (const Distance &other);
Distance operator +(const Distance &other);
Distance operator -(const Distance &other);
  
  
};
#endif /* distance_h */

distance.cpp


#include "distance.h"
Distance::Distance()
{
miles = 0;
yards = 0;
feet = 0;
inches = 0;
}
Distance::Distance(int inches)
{
if(inches < 0)
inches = 0;
  
setFromInches(inches);
}

Distance::Distance(int m, int y, int f, int i)
{
set(m, y, f, i);
}

int Distance::getMiles()
{
return miles;
}
int Distance::getYards()
{
return yards;
}
int Distance::getFeet()
{
return feet;
}
int Distance::getInches()
{
return inches;
}
ostream& operator << (ostream &out,const Distance &d)
{
out << "Miles: " << d.miles << ", Yards: " << d.yards << ", Feet: " << d.feet << ", Inches: " << d.inches ;
return out;
}
istream& operator >> (istream &in, Distance &d)
{
int m, y, f, i;
in >> m >> y >> f >> i;
d.set(m, y, f, i);
return in;
}
bool Distance::operator == (const Distance &other)
{
return toInches() == other.toInches();
}
bool Distance::operator != (const Distance &other)
{
return toInches() != other.toInches();
}
bool Distance::operator < (const Distance &other)
{
return toInches() < other.toInches();
}
bool Distance::operator > (const Distance &other)
{
return toInches() > other.toInches();
}
bool Distance::operator <= (const Distance &other)
{
return toInches() <= other.toInches();
}
bool Distance::operator >= (const Distance &other)
{
return toInches() >= other.toInches();
}
Distance Distance::operator +(const Distance &other)
{
int d = toInches() + other.toInches();
return Distance(d);
}
Distance Distance::operator -(const Distance &other)
{
int d1 = toInches();
int d2 = other.toInches();
int diff = d1 - d2;
if(diff < 0)
diff = -diff;
return Distance(diff);
}

void Distance::set(int m, int y, int f, int i)
{
int totalInches = 0;
if(m < 0 || y < 0 || f < 0 || i < 0)
totalInches = 0;
else
{
//getting the total inches ensures that if y > 1760 or f > 12 , we handle those cases correctly
totalInches = i;
totalInches += f * 12;
totalInches += y * 3 * 12;
totalInches += m * 1760 * 3 * 12;
}
setFromInches(totalInches);
}

void Distance::setFromInches(int n)
{
//get miles , yards, feet in terms of inches
int onefeet = 12;
int oneyard = 3 * onefeet;
int onemile = 1760 * oneyard;
  
miles = n / onemile;
n = n % onemile;
  
yards = n / oneyard;
n = n % oneyard;
  
feet = n / onefeet;
inches = n % onefeet;
}

int Distance::toInches() const
{
int totalInches = 0;
totalInches = inches;
totalInches += feet * 12;
totalInches += yards * 3 * 12;
totalInches += miles * 1760 * 3 * 12;
return totalInches;
}

main.cpp

#include "distance.h"
#include <iostream>
using namespace std;
int main()
{
Distance d1;
Distance d2(1234);
Distance d3;
  
cout << "Using << to display distances " << endl;
cout << "d1 is " << d1 << endl;
cout << "d2 is " << d2 << endl;
  
cout << "Using >> to input distance d3 , input miles yards feet inches (separated by spaces)" << endl;
cin >> d3;
cout << "d3 is " << d3 << endl;
  
cout << "d2 + d3 = " << d2 + d3 << endl;
cout << "d2 - d3 = " << d2 - d3 << endl;
  
cout << boolalpha ; //settting to show boolean values as true or false
  
cout << "d2 < d3 = " << (d2 < d3) << endl;
cout << "d2 > d3 = " << (d2 > d3 )<< endl;
cout << "d2 <= d3 = " << (d2 <= d3) << endl;
cout << "d2 >= d3 = " << (d2 >= d3) << endl;
cout << "d2 == d3 = " << (d2 == d3) << endl;
cout << "d2 != d3 = " << (d2 != d3) << endl;
  
  
}

output

Jsing << to display distances di is Miles: 0, Yards: 0, Feet: 0, Inches: 0 2 is Miles: 0, Yards: 34, Feet: 0, Inches: 10 sing

Add a comment
Know the answer?
Add Answer to:
So far I have this code but I'm not sure if it's right or how to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that contains the function measure() that is to accept a long integer number...

    Write a program that contains the function measure() that is to accept a long integer number total and the addresses of the integer variables inches, feet, yards, and miles. The passed long integer represents the total number of inches, and the function is to determine the number of miles, yards, feet, and inches in the passed value, writing these values directly into the respective variables declared in the calling function. This function will be called from the main program and...

  • For this Java program I have to be implementing a Pedometer class. The driver file will...

    For this Java program I have to be implementing a Pedometer class. The driver file will be provided for you ( PedometerDriver.java ) and can be downloaded in Canvas. Write a stand alone class. Name your class Pedometer and source file Pedometer.java . Program 6 Overview Goals Class and File Naming Here is the Unified Modeling Language (UML) diagram for the Pedometer class. See the end of this document for information on how to read a UML document. Pedometer -...

  • Here is the code I have so far. I'm trying to figure out how to implement...

    Here is the code I have so far. I'm trying to figure out how to implement a boolean and use precedence. The 2nd expression should be 14 but it comes out as 28 so I'm definitely not understanding. #include <stack> #include <iostream> #include <string> using namespace std; // Function to find precedence of // operators. int precedence(char op) {    if (op == '+' || op == '-')        return 1;    if (op == '*' || op ==...

  • ////****what am i doing wrong? im trying to have this program with constructors convert inches to...

    ////****what am i doing wrong? im trying to have this program with constructors convert inches to feet too I don't know what im doing wrong. (the program is suppose to take to sets of numbers feet and inches and compare them to see if they are equal , greater, less than, or not equal using operator overload #include <stdio.h> #include <string.h> #include <iostream> #include <iomanip> #include <cmath> using namespace std; //class declaration class FeetInches { private:    int feet;   ...

  • I need a c++ visual basic program that will. Carpet Calculator. This problem starts with the...

    I need a c++ visual basic program that will. Carpet Calculator. This problem starts with the FeetInches class that is provided in the course Content area on the assignment page for this week. This program will show how classes will interact with each other as data members within another class. Modify the FeetInches class by overloading the following operators which should all return a bool. <= >= != Next add a copy constructor to the FeetInches class and a multiply...

  • #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured...

    #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured in feet and inches. */ class FeetInches { private: int feet; // The number of feet int inches; // The number of inches /** The simplify method adjusts the values in feet and inches to conform to a standard measurement. */ void simplify() { if (inches > 11) { feet = feet + (inches / 12); inches = inches % 12; } } /**...

  • Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the valu...

    Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the value the user enters based on the selected conversion type. The application should handle the following conversions: From To Conversion Miles - Kilometers: 1 mile = 1.6093 kilometers Kilometers - Miles: 1 kilometer = 0.6214 miles Feet - Meters: 1 foot = 0.3048 meters Meters - Feet: 1 meter = 3.2808 feet Inches - Centimeters: 1 inch = 2.54 centimeters Centimeters - Inches: 1...

  • Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include &lt...

    Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include <iostream> using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test the money class ****************************************************************/ int main() {    int dollars;    int cents;    cout << "Dollar amount: ";    cin >> dollars;    cout << "Cents amount: ";    cin >> cents;    Money m1;    Money m2(dollars);    Money m3(dollars, cents);    cout << "Default constructor: ";    m1.display();    cout << endl;    cout << "Non-default constructor 1: ";    m2.display();    cout << endl;   ...

  • I need help with this C code Can you explain line by line? Also can you...

    I need help with this C code Can you explain line by line? Also can you explain to me the flow of the code, like the flow of how the compiler reads it. I need to present this and I want to make sure I understand every single line of it. Thank you! /* * Converts measurements given in one unit to any other unit of the same * category that is listed in the database file, units.txt. * Handles...

  • code in c++ Question 3 [30 Marks In architectural drawings, the distances are measured in feet...

    code in c++ Question 3 [30 Marks In architectural drawings, the distances are measured in feet and inches according to the English system of measurement. There are 12 inches in a foot. The length of a living room, for example, might be given as 15–8", meaning 15 feet plus 8 inches. The hyphen isn't a negative sign; it merely separates the feet from the inches. Figure 1 shows typical length measurements in the English system. Suppose you want to create...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT