So 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++;
}
}
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
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 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 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 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 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 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 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 <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 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 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...