Question
Add a data member to the IngredMeasure class that indicates whether U.S. units are being used or not (assume Imperial units if not). Add a
Add a data member to the IngredMeasure class that indicates whether U.S. units are being used or not (assume Imperial units if not).
Add a constant for conversion factors needed for Imperial ouncespints (the U.S. conversion factor is already provided).
Change IngredMeasure mutator set() to also accept whether U.S. units are used or not and store that in the object. Make measure1 use U.S. units, measure2 use Imperial units.
Display whether U.S. or Imperial are being used in parentheses after the units displayed by member function output().
Modify member function convertToUnits() so that it perform the correct conversion for U.S. vs. Imperial units.
Add an accessor member function that returns the number of milliliters for an IngredMeasure object. Add constants for conversion to milliliters as needed.
Call the new accessor member function and use the value returned to display the number of millimeters for measure1 and measure2 (for the latter, both before and after conversion).
#include
const int UNITS_PINTS = 1; const int UNITS_FLUID_OUNCES = 2; const double OUNCES_IN_PINT = 16.0;
// IngredMeasure class definition with member data and // prototypes for member functions. class IngredMeasure { public: // Accessors double getAmt(); void output();
// Mutators // Precondition: newUnits must be one of valid constants. void set(double newAmt, int newUnits); void convertToUnits(int newUnits);
private: double amt; int units; };
int main() { IngredMeasure measure1, measure2; double amount;
// Set fixed values for measurement. measure1.set(0.75, UNITS_PINTS);
// Allow user to enter amount for measurement. cout << "Enter measure2 in fluid ounces: "; cin >> amount; measure2.set(amount, UNITS_FLUID_OUNCES);
// Output floating-point numbers to one decimal place. cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(1);
cout << "measure1: "; measure1.output(); cout << endl;
cout << "measure2: "; measure2.output(); cout << endl;
measure2.convertToUnits(UNITS_PINTS); cout << "measure2 (different units): "; measure2.output(); cout << endl;
return 0; }
// IngredMeasure member function definitions.
double IngredMeasure::getAmt() { return amt; }
void IngredMeasure::output() { // Display amount. cout << amt << ' ';
// Display units. if (units == UNITS_PINTS) cout << "pints"; else if (units == UNITS_FLUID_OUNCES) cout << "ounces"; }
void IngredMeasure::set(double newAmt, int newUnits) { // Validate the units. if (newUnits != UNITS_PINTS && newUnits != UNITS_FLUID_OUNCES) cerr << "Unknown measure units: " << newUnits << endl;
// Store data. amt = newAmt; units = newUnits; }
void IngredMeasure::convertToUnits(int newUnits) { // Validate the units. if (newUnits != UNITS_PINTS && newUnits != UNITS_FLUID_OUNCES) cerr << "Unknown measure units: " << newUnits << endl;
// Perform conversion.
if (units == UNITS_FLUID_OUNCES && newUnits == UNITS_PINTS) amt /= OUNCES_IN_PINT; else if (units == UNITS_PINTS && newUnits == UNITS_FLUID_OUNCES) amt *= OUNCES_IN_PINT;
units = newUnits; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started