[Solved] Overview Assignment Implement Class Called Piggybank Used Represent Collection Coins Funct Q37228521
Overview
In this assignment, implement a class called PiggyBank that willbe used to represent a collection of coins. Functionality will beadded to the class so that coins can be added to the bank andoutput operations can be performed. A driver program is provided totest the methods.
class PiggyBank
The PiggyBank class definition and symbolic constants should beplaced at the top of the driver program source code file while themethod implementation should be done after the closing curly bracefor main().
class PiggyBank{public:PiggyBank();PiggyBank( int, int, int, int );void printPiggyBank();void printPiggyBankValue();void emptyTheBank();void addCoins( int, int, int, int );void addPennies( int );void addNickels( int );void addDimes( int );void addQuarters( int );private:int numPennies, numNickels, numDimes, numQuarters;static const double PENNY, NICKEL, DIME, QUARTER;double calcPiggyBankValue();};
Data Members
The PiggyBank class should contain eight private data members.They are:
- numPennies is an integer to hold the number ofpennies in the piggy bank
- numNickels is an integer to hold the number ofnickels in the piggy bank
- numDimes is an integer to hold the number ofdimes in the piggy bank
- numQuarters is an integer to hold the numberof quarters in the piggy bank
- PENNY is a symbolic constant of type doublethat represents the value of a single penny ($0.01)
- NICKEL is a symbolic constant of type doublethat represents the value of a single nickel ($0.05)
- DIME is a symbolic constant of type doublethat represents the value of a single dime ($0.10)
- QUARTER is a symbolic constant of type doublethat represents the value of a single quarter ($0.25)
C++ 11 allows for symbolic constants to be initialized in theclass definition. However, it’s not something that is supported byall compilers because class definitions are treated as patterns forwhat the class should contain, and therefore, they don’t take upany memory. To create a symbolic constant that will work for allcompilers:
static const int PENNY;
const double PiggyBank::PENNY = 0.01;
Note the dropping of the keyword “static” and the inclusion ofthe “PiggyBank::” in front of the constant name. This lets thecompiler know that the constant belongs to the PiggyBank class.
-
In the class definition, define the symbolic constant with thekeyword “static” before the definition. So:
-
Before main, initialize the symbolic constant with its value.So:
Now, PENNY can be used in the class methods when needed.
Constructors
Constructor 1: PiggyBank()
The first constructor for the PiggyBank class (the defaultconstructor) takes no arguments. It simply initializes the 4integer data members to 0.
Constructor 2: PiggyBank( int initialPennies, intinitialNickels, int initialDimes, int initialQuarters )
The second constructor for the PiggyBank class should take fourinteger arguments: the number of pennies, nickels, dimes, andquarters respectively.
For each argument, if the passed in value is not negative, itshould be used to initialize the appropriate data member. If thepassed in value is negative, initialize the corresponding datamember to 0 and print an informative error message.
Methods
void printPiggyBank()
This method prints the contents of a PiggyBank object. It takesno arguments and returns nothing. It should print the contents ofthe object on a single line:
Pennies: 7 Nickels: 9 Dimes: 3 Quarters: 4
void printPiggyBankValue()
This method prints the value of a PiggyBank object. It takes noarguments and returns nothing. It should print the contents of theobject in the format:
$1.82
void emptyTheBank()
This method sets a PiggyBank object so that it no longercontains any money. It takes no arguments and returns nothing. Itshould simply initialize each of the integer data members to 0.
void addCoins( int morePennies, int moreNickels, int moreDimes,int moreQuarters )
This method will add coins to a PiggyBank object. It takes fourinteger arguments and returns nothing. The arguments passed to thismethod are: the number of pennies, nickels, dimes, and quarters,respectively, to add to the PiggyBank object.
Before adding a value to a data member, verify that that thevalue is not negative. If it is not negative, add the value to theappropriate data member. If the value is negative, print aninformative error message. Think about calling the add methodsdescribed below.
void addPennies( int morePennies )
This method will add pennies to a PiggyBank object. It takes oneinteger argument and returns nothing. The argument passed to thismethod is the number of pennies to add to the PiggyBank object.
Before adding a value to the pennies data member, verify thatthat the passed in value is not negative. If it is not negative,add the value to the pennies data member. If the value is negative,print an informative error message and do not change the penniesdata member.
void addNickels( int moreNickels )
This method will add nickels to a PiggyBank object. It takes oneinteger argument and returns nothing. The argument passed to thismethod is the number of nickels to add to the PiggyBank object.
Before adding a value to the nickels data member, verify thatthat the passed in value is not negative. If it is not negative,add the value to the nickels data member. If the value is negative,print an informative error message and do not change the nickelsdata member.
void addDimes( int moreDimes )
This method will add dimes to a PiggyBank object. It takes oneinteger argument and returns nothing. The argument passed to thismethod is the number of dimes to add to the PiggyBank object.
Before adding a value to the dimes data member, verify that thatthe passed in value is nonnegative. If it is nonnegative, add thevalue to the dimes data member. If the value is negative, print aninformative error message and do not change the dimes datamember.
void addQuarters( int moreQuarters )
This method will add quarters to a PiggyBank object. It takesone integer argument and returns nothing. The argument passed tothis method is the number of quarters to add to the PiggyBankobject.
Before adding a value to the quarters data member, verify thatthat the passed in value is not negative. If it is not negative,add the value to the quarters data member. If the value isnegative, print an informative error message and do not change thequarter data member.
double calcPiggyBankValue()
This method calculates and returns the value (in dollars andcents) of a PiggyBank object. It takes no arguments and returns adouble.
For example, if the current PiggyBank object contains 3 pennies,4 nickels, 5 dimes, and 6 quarters, then this method should returnthe value 2.23.
This method will be used to help in the coding of theprintPiggyBankValue method. It will never be called by anythingother than members of the PiggyBank class, therefore itis implemented as a private method.
Driver Program:
#include <iostream>#include <iomanip>using namespace std;//*************** Place the class description after this line ***************//*************** Initialize the symbolic constants after this line **********//****************************************************************************int main(){//Test 1 — default constructor and printPiggyBankcout << “***** Test 1: Default Constructor and printPiggyBank *****” << endl << endl << “Default constructor produces bank1: ” << endl << endl;PiggyBank bank1;bank1.printPiggyBank();//Test 2 — constructor with argumentscout << endl << endl << endl << “***** Test 2: Constructor with Arguments *****” << endl << endl << “Constructor with 6, 8, 10, 12 produces bank2: ” << endl << endl;PiggyBank bank2( 6, 8, 10, 12 );bank2.printPiggyBank();cout << endl << endl << “Constructor with 4, 1, 0, 7 produces bank3:” << endl << endl;PiggyBank bank3 = PiggyBank( 4, 1, 0, 7 );bank3.printPiggyBank();cout << endl << endl << “Constructor with 23, -8, -2, 29 produces bank4:” << endl << endl;PiggyBank bank4 = PiggyBank( 23, -8, -2, 29 );bank4.printPiggyBank();//Test 3 — printPiggyBankValuecout << endl << endl << endl << “***** Test 3: printPiggyBankValue *****” << endl << endl << “bank1:” << endl;bank1.printPiggyBank();cout << endl << “Total: “;bank1.printPiggyBankValue();cout << endl << endl << endl << “bank2:” << endl;bank2.printPiggyBank();cout << endl << “Total: “;bank2.printPiggyBankValue();cout << endl << endl << endl << “bank3:” << endl;bank3.printPiggyBank();cout << endl << “Total: “;bank3.printPiggyBankValue();cout << endl << endl << endl << “bank4:” << endl;bank4.printPiggyBank();cout << endl << “Total: “;bank4.printPiggyBankValue();//Test 4 — adding coinscout << endl << endl << endl << “***** Test 4: add Methods *****” << endl << endl << “Adding 2 pennies, 47 nickels, 20 dimes, and 5 quarters to bank2 produces:” << endl << endl;bank2.addCoins( 2, 47, 20, 5 );bank2.printPiggyBank();cout << endl << “Total: “;bank2.printPiggyBankValue();cout << endl << endl << endl << “Adding 95 pennies to bank3:” << endl << endl;bank3.addPennies( 95 );bank3.printPiggyBank();cout << endl << “Total: “;bank3.printPiggyBankValue();cout << endl << endl << endl << “Adding -2 nickels to bank3:” << endl << endl;bank3.addNickels( -2 );bank3.printPiggyBank();cout << endl << “Total: “;bank3.printPiggyBankValue();cout << endl << endl << endl << “Adding 41 dimes to bank4:” << endl << endl;bank4.addDimes( 41 );bank4.printPiggyBank();cout << endl << “Total: “;bank4.printPiggyBankValue();cout << endl << endl << endl << “Adding 14 quarters to bank2: ” << endl << endl;bank2.addQuarters( 14 );bank2.printPiggyBank();cout << endl << “Total: “;bank2.printPiggyBankValue();//Test 5 — empty the bankcout << endl << endl << endl << “***** Test 5: Emptying a PiggyBank *****” << endl << endl << “It’s time to empty the bank.” << endl << endl;cout << endl << “bank3 initially contains: ” << endl << endl;bank3.printPiggyBank();cout << endl << “Total: “;bank3.printPiggyBankValue();bank3.emptyTheBank();cout << endl << endl << endl << “bank3 now contains: ” << endl << endl;bank3.printPiggyBank();cout << endl << “Total: “;bank3.printPiggyBankValue();cout << endl << endl;return 0;}//*************** Implement the class methods after this line ***************
Output:
***** Test 1: Default Constructor and printPiggyBank *****Default constructor produces bank1:Pennies: 0 Nickels: 0 Dimes: 0 Quarters: 0***** Test 2: Constructor with Arguments *****Constructor with 6, 8, 10, 12 produces bank2:Pennies: 6 Nickels: 8 Dimes: 10 Quarters: 12Constructor with 4, 1, 0, 7 produces bank3:Pennies: 4 Nickels: 1 Dimes: 0 Quarters: 7Constructor with 23, -8, -2, 29 produces bank4:constructor error: the number of nickels cannot be negativeconstructor error: the number of dimes cannot be negativePennies: 23 Nickels: 0 Dimes: 0 Quarters: 29***** Test 3: printPiggyBankValue *****bank1:Pennies: 0 Nickels: 0 Dimes: 0 Quarters: 0Total: $0.00bank2:Pennies: 6 Nickels: 8 Dimes: 10 Quarters: 12Total: $4.46bank3:Pennies: 4 Nickels: 1 Dimes: 0 Quarters: 7Total: $1.84bank4:Pennies: 23 Nickels: 0 Dimes: 0 Quarters: 29Total: $7.48***** Test 4: add Methods *****Adding 2 pennies, 47 nickels, 20 dimes, and 5 quarters to bank2 produces:Pennies: 8 Nickels: 55 Dimes: 30 Quarters: 17Total: $10.08Adding 95 pennies to bank3:Pennies: 99 Nickels: 1 Dimes: 0 Quarters: 7Total: $2.79Adding -2 nickels to bank3:addNickels error: the number of additional nickels cannot be negativePennies: 99 Nickels: 1 Dimes: 0 Quarters: 7Total: $2.79Adding 41 dimes to bank4:Pennies: 23 Nickels: 0 Dimes: 41 Quarters: 29Total: $11.58Adding 14 quarters to bank2:Pennies: 8 Nickels: 55 Dimes: 30 Quarters: 31Total: $13.58***** Test 5: Emptying a PiggyBank *****It’s time to empty the bank.bank3 initially contains:Pennies: 99 Nickels: 1 Dimes: 0 Quarters: 7Total: $2.79bank3 now contains:Pennies: 0 Nickels: 0 Dimes: 0 Quarters: 0Total: $0.00
Programming Notes
-
Each method must have a documentation box like a function.However, since a lot of the methods perform similar tasks, onedocumentation box that gives a brief explanation for all of therelated methods will suffice.
For example, one documentation box can cover the addCoins,addPennies, addNickels, addDimes, and addQuarters; printPiggyBankand printPiggyBankValue can be combined into one documentationbox.
Expert Answer
Answer to Overview In this assignment, implement a class called PiggyBank that will be used to represent a collection of coins. Fu… . . .
OR

