16.7 Lab: Distance class
Distance Class
The Distance class will represent a distance in some number of feet and inches. This Distance object must always be positive and never contain an inch value greater than or equal to 12 (12 inches equals 1 foot). It is your job, as the engineer/designer of this Distance class to make sure this is always true.
Separate Files
As is commonly done when designing a class, you are to separate your class from the program that will use the class. You will need the following 3 files:
You will submit all 3 files, main.cpp, Distance.h and Distance.cpp. Distance.h. You cannot change Distance.h.
main.cpp has also been provided for you. You will want to add your own unit tests of the other public functions, but make sure main.cpp looks like the below code when submitting.
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;
}
Given this main function, if your constructors and operator functions are working correctly, your output will look like:
d1: 0' 0"
d2: 2' 5.9"
d3: 0' 3.75"
d4: 6' 7.34"
d5: 8' 4"
d4 + d5: 14' 11.34"
d2 + d4: 9' 1.24"
d3 - d1: 0' 3.75"
d1 - d3: 0' 3.75"
d4 - d2: 4' 1.44"
d2 - d4: 4' 1.44"
d4 - d5: 1' 8.66"
d5 - d2: 5' 10.1"
Main.cpp (Must use)
#include <iostream>
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;
}
Distance.h (Read only)
// Distance.h file
#include <iostream>
using namespace std;
class Distance
{
private:
unsigned feet;
double inches;
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.
*/
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.
*/
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.
Hint: Convert both distances to all inches before adding.
*/
const Distance operator+(const Distance &rhs) const;
/* Returns the difference between 2 Distances.
Hint: Convert both distances to all inches before subtracting.
*/
const 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();
};
thanks for the question, here is the full code in C++.
=============================================================================================
#ifndef DISTANCE_H
#define DISTANCE_H
// Distance.h file
#include <iostream>
using namespace std;
class Distance
{
private:
unsigned int feet;
double inches;
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.
*/
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.
*/
Distance(double in);
/* Returns just the feet portion of the Distance
*/
unsigned int 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.
Hint: Convert both distances to all inches before adding.
*/
const Distance operator+(const Distance &rhs) const;
/* Returns the difference between 2 Distances.
Hint: Convert both distances to all inches before subtracting.
*/
const 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();
};
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "Distance.h"
// Distance.h file
#include <iostream>
#include<iomanip>
Distance::Distance(){
this->feet=0;
this->inches=0;
init();
}
/* 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.
*/
Distance::Distance(unsigned ft, double in){
this->feet=ft;
this->inches=in;
init();
}
/* 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.
*/
Distance::Distance(double in){
if (inches>=12){
feet=((int)inches)/12;
inches=inches - feet*12;
}else{
feet=0;
inches=in;
}
init();
}
/* Returns just the feet portion of the Distance
*/
unsigned int Distance::getFeet() const{
return this->feet;
}
/* Returns just the inches portion of the Distance
*/
double Distance::getInches() const{
return inches;
}
/* 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 Distance::distanceInInches() const{
return feet*12 + inches;
}
/* Returns the entire distance as the equivalent amount of feet.
(e.g., 3 feet 6 inches would be returned as 3.5 feet)
*/
double Distance::distanceInFeet() const{
return feet+inches/12;
}
/* 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 Distance::distanceInMeters() const{
return 0.0254*(feet*12+inches);
}
/* Returns the sum of 2 Distances.
Hint: Convert both distances to all inches before adding.
*/
const Distance Distance::operator+(const Distance &rhs) const{
double total_inches = distanceInInches() + rhs.distanceInInches();
return Distance(total_inches);
}
/* Returns the difference between 2 Distances.
Hint: Convert both distances to all inches before subtracting.
*/
const Distance Distance::operator-(const Distance &rhs) const{
double total_inches = distanceInInches() - rhs.distanceInInches();
return Distance(total_inches);
}
/* 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 Distance::init(){
this->feet=this->feet<0?-feet:feet;
this->inches=this->inches<0?-inches:inches;
if (inches>=12){
int ft=(int)inches/12;
this->feet+=(int)inches/12;
this->inches=inches - ft*12;
}
}
/* Outputs to the stream out the Distance in the format:
feet' inches'' (i.e. 3' 3.41'')
*/
ostream & operator<<(ostream &out, const Distance &rhs){
out<<fixed<<showpoint<<setprecision(2);
out<<rhs.feet<<"' "<<rhs.inches<<"''";
return out;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
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;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16.7 Lab: Distance class Distance Class The Distance class will represent a distance in some number...