Question
a) Modify the class JarType to include a constructor and the destructor. Additionally, in the constructor include the following statement: std::cout b) In MyProg.cpp, right
a) Modify the class JarType to include a constructor and the destructor. Additionally,
in the constructor include the following statement:
std::cout
b) In MyProg.cpp, right after jar1 is created, try the following statement:
The above line should give you a compilation error. Be sure you understand the
reason for the error. Then, comment out, the line so that your program can
compile, as follows:
// Removed the next line as it causes a compilation error
//jar1.numUnits = 10000;
c) Adjust the code in the jar's add method to guard against negative input values.
d) Expand the capabilities of the JarType to include a subtract method to accept
an input value to lower the number of units in a jar. Again, be sure to check
for negative input values.
e) Add a constructor to JarType to allow you to prefill a jar with an initial amount.
For example, in MyProg.cpp, you should be able to do the following:
JarType jar3(50);
Then, show the number of units in jar3.
Print a message when a prefilled jar is created.
Be sure to test all your changes by including an appropriate call in MyProg.cpp. For example,
jar1.add( -5000 );
f) Expand the capabilities of the class JarType so that the following statements
can be used in MyProg.cpp:
JarType jar4( 'p' ); // Prefill with 16 ounces
JarType jar5( 'q' ); // Prefill with 32 ounces
JarType jar6( 'a' ); // Should print invalid request.
Do cout's to make sure the jars have the requested
quantity.
g) Add a global function to ask the user for the number of units.
The prototype goes before main() as follows:
int getInput( int & n );
The code for the function goes after the closing "}" for main(), as follows:
// Description: A function to ask user for a value
// Return status: 0 a valid number is entered
// 1 a valid number is not entered
int getInput(int & n)
{
int temp;
std::cout
std::cin >> temp;
if ( temp > 0 )
{
n = temp;
return 0;
}
return 1;
} // end of getInput()
h) Now, in main() use getInput for a jar -- for example:
JarType jar6;
int number = 0;
// Get a number from the user to add to jar6.
int status = getInput( number );
// Be sure the number is "good".
if ( status == 0 )
{
jar6.add( number );
}
std::cout
jar1.numUnits = 10000;
// Edit History 5 //" v:shapes="Picture_x0020_72">
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