You have a class ImperialLength that stores values in inches. It can covert to a
ID: 650169 • Letter: Y
Question
You have a class ImperialLength that stores values in inches. It can covert to and from feet and inches to total-inches.
The current class is implemented with files ImperialLength.handImplerialLength.cpp.
I have also give you a file ImperialLengthDriver.cpp that can be used to test the updated version of the class you will be building.
The current implementation of the ImperialLength class contains the following class declaration:
class ImperialLength
{
private:
double totalInches;
public:
ImperialLength(int feet, double inches);
ImperialLength(double totalInches);
~ImperialLength();
std::string to_string(int precision = 6) const;
int getFeet() const;
double getInches() const;
double getTotalInches() const;
};
You will be overloading the operators +=, +, -= and
Explanation / Answer
#ifndef IMPERIALLENGTH_H_
#define IMPERIALLENGTH_H_
#include <string>
#include <iostream>
class ImperialLength
{
private:
double totalInches;
public:
ImperialLength(int feet, double inches);
ImperialLength(double totalInches);
~ImperialLength();
std::string to_string(int precision = 6) const;
int getFeet() const;
double getInches() const;
double getTotalInches() const;
ImperialLength operator+(const ImperialLength& ln);
ImperialLength operator+=(const ImperialLength& ln);
ImperialLength operator-(const ImperialLength& ln);
ImperialLength operator-=(const ImperialLength& ln);
string operator<<(const ImperialLength& ln);
};
#endif /* IMPERIALLENGTH_H_ */
/*
* ImperialLength.cpp
*
* Created on: Apr 1, 2015
* Author: dgv130030
*/
#include "ImperialLength.h"
#include <string>
#include <ostream>
#include <sstream>
#include <iomanip>
using namespace std;
// Create an imperial length object using feet and inches
ImperialLength::ImperialLength(int feet, double inches)
{
// store the length internally as (total) inches.
totalInches = feet * 12.0 + inches;
}
ImperialLength::ImperialLength(double totalInches)
{
// store the length internally as total inches.
this->totalInches = totalInches;
}
ImperialLength::~ImperialLength()
{
// nothing to do here
}
// convert to the number of feet
int ImperialLength::getFeet() const
{
// get the total inches and divide by 12. Note that we don't want fractional feet
return static_cast<int>(totalInches) / 12;
}
double ImperialLength::getInches() const
{
// get the remaining inches. Calculate the number of remaining inches after removing all of the feet.
// There may be a fractional inch since inches are store as a double value.
return totalInches - (getFeet() * 12.0);
}
double ImperialLength::getTotalInches() const
{
// just return the total inches
return totalInches;
}
std::string ImperialLength::to_string(int precision) const
{
// we use the output string stream to build the output for to_string.
// Note that we set the precision for the output of the inches.
// The format is xx'yy.yy" or yy.yy" if feet is 0.
ostringstream outString;
int feet = getFeet();
if (feet != 0)
{
// output the feet
outString << getFeet() << "'";
}
// output the inches
outString << fixed << setprecision(precision) << getInches() << """;
// return the string value of the ostringstream back to the caller
return outString.str();
}
ImperialLength ImperialLength::operator+(const ImperialLength& ln)
{
this->totalInches = this->getInches() + ln.getInches();
int totfeet = this->getFeet() + ln.getFeet();
ImperialLength newln(totfeet,this->totalInches);
return newln;
}
ImperialLength ImperialLength::operator+=(const ImperialLength& ln)
{
this->totalInches = this->getInches() + ln.getInches();
int totfeet = this->getFeet() + ln.getFeet();
ImperialLength(totfeet,this->totalInches);
return *this;
}
ImperialLength ImperialLength::operator-(const ImperialLength& ln)
{
this->totalInches = this->getInches() - ln.getInches();
int totfeet = this->getFeet() - ln.getFeet();
ImperialLength newln(totfeet,this->totalInches);
return newln;
}
ImperialLength ImperialLength::operator-=(const ImperialLength& ln)
{
this->totalInches = this->getInches() - ln.getInches();
int totfeet = this->getFeet() - ln.getFeet();
ImperialLength(totfeet,this->totalInches);
return *this;
}
string ImperialLength::operator<<(const ImperialLength& ln)
{
return ln.to_string(3);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.