165k views
4 votes
Define a private helper function called ConvertToPints() that converts the data member gallons to pints and returns a double.

Ex: If the input is 240.0, then the output is:

1920.0 pints
Note: The equation to convert from gallons to pints is: pints = gallons * 8

#include
#include
#include
using namespace std;

class Volume {
public:
void SetGallons(double volumeGallons);
double GetGallons() const;
void PrintInPints();
private:
double gallons;
double ConvertToPints();
};

void Volume::SetGallons(double volumeGallons) {
gallons = volumeGallons;
}

double Volume::GetGallons() const {
return gallons;
}

/* Your code goes here */

void Volume::PrintInPints() {
cout << fixed << setprecision(1) << ConvertToPints() << " pints" << endl;
}

int main() {
Volume volume1;
double inputGallons;

cin >> inputGallons;
volume1.SetGallons(inputGallons);

volume1.PrintInPints();

return 0;
}

1 Answer

3 votes

Final answer:

A private helper function called ConvertToPints() is defined within the Volume class to convert gallons to pints by multiplying the 'gallons' data member by 8 and returning it as a double.

Step-by-step explanation:

To define a private helper function called ConvertToPints() within the Volume class that converts gallons to pints, you need to add the following code snippet inside the class definition but under the 'private' access specifier:

double Volume::ConvertToPints() {
return gallons * 8;
}

The function takes the data member 'gallons', multiplies it by 8 (since there are 8 pints in a gallon), and returns the result as a double data type. When you call the PrintInPints() function, it will utilize this private function to display the volume in pints.

User Adrien Parrochia
by
8.5k points