|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected keyword
- as you can see, it is highlighted blue, because it is just another type of access. Replace private
keyword with protected
in your coin class and it will do.
These static variables are initialized at the top of the Coins.cpp file as: const double Pennies::VALUE = 0.01; const double Nickels::VALUE = 0.05; const double Dimes::VALUE = 0.10; const double Quarters::VALUE = 0.25; const double Pennies::WEIGHT = 2.500; const double Nickels::WEIGHT = 5.000; const double Dimes::WEIGHT = 2.268; const double Quarters::WEIGHT = 5.670; The only member function that these derived classes have is a one-parameter constructor to set the number of coins in the particular object. This constructor passes the number and coin type to the Coins constructor to store in its data members. |
|
|
|
|
Adding: 4 quarters ($1.00) (22.68 grams) Piggy bank now has: 0 pennies, 0 nickles, 0 dimes, and 4 quarters. The total worth is $1.00 with the coins weighing 22.68 grams. Requesting $0.36 Remove: 0 pennies, 0 nickels, 0 dimes, and 2 quarters For a total of $0.50 Piggy bank now has: 0 pennies, 0 nickles, 0 dimes, and 2 quarters. The total worth is $0.50 with the coins weighing 11.34 grams. Requesting $1.63 Piggy bank has insufficient funds to honor this request Piggy bank now has: 0 pennies, 0 nickles, 0 dimes, and 2 quarters. The total worth is $0.50 with the coins weighing 11.34 grams. Adding: 38 pennies ($0.38) (95.00 grams) Adding: 86 nickels ($4.30) (430.00 grams) Adding: 48 dimes ($4.80) (108.86 grams) Adding: 38 quarters ($9.50) (215.46 grams) Adding: 26 pennies ($0.26) (65.00 grams) Adding: 55 nickels ($2.75) (275.00 grams) Adding: 74 dimes ($7.40) (167.83 grams) Piggy bank weight exceeded! No coins added. Adding: 23 quarters ($5.75) (130.41 grams) Piggy bank now has: 64 pennies, 141 nickles, 48 dimes, and 63 quarters. The total worth is $28.24 with the coins weighing 1331.07 grams. Requesting $27.53 Remove: 3 pennies, 139 nickels, 48 dimes, and 63 quarters For a total of $27.53 Piggy bank now has: 61 pennies, 2 nickles, 0 dimes, and 0 quarters. The total worth is $0.71 with the coins weighing 162.50 grams. |
Adding: 4 25 (1) (22.68) Piggy bank weight exceeded! No coins added. Piggy bank now has: 0 pennies, 0 nickles, 0 dimes, and 60 quarters. The total worth is $60 with the coins weighing 1360.8. Requesting $0.36 |
|
|
|
|
Adding: 4 quarters ($1.00) (22.68 grams) Piggy bank now has: 0 pennies, 0 nickles, 0 dimes, and 4 quarters. The total worth is $1.00 with the coins weighing 22.68 grams. Requesting $0.36 Remove: 0 pennies, 0 nickels, 0 dimes, and 2 quarters For a total of $0.50 Piggy bank now has: 0 pennies, 0 nickles, 0 dimes, and 2 quarters. The total worth is $0.50 with the coins weighing 11.34 grams. Requesting $1.63 Piggy bank has insufficient funds to honor this request Piggy bank now has: 0 pennies, 0 nickles, 0 dimes, and 2 quarters. The total worth is $0.50 with the coins weighing 11.34 grams. Adding: 38 pennies ($0.38) (95.00 grams) Adding: 86 nickels ($4.30) (430.00 grams) Adding: 48 dimes ($4.80) (108.86 grams) Adding: 38 quarters ($9.50) (215.46 grams) Adding: 26 pennies ($0.26) (65.00 grams) Adding: 55 nickels ($2.75) (275.00 grams) Adding: 74 dimes ($7.40) (167.83 grams) Piggy bank weight exceeded! No coins added. Adding: 23 quarters ($5.75) (130.41 grams) Piggy bank now has: 64 pennies, 141 nickles, 48 dimes, and 63 quarters. The total worth is $28.24 with the coins weighing 1331.07 grams. Requesting $27.53 Remove: 3 pennies, 139 nickels, 48 dimes, and 63 quarters For a total of $27.53 Piggy bank now has: 61 pennies, 2 nickles, 0 dimes, and 0 quarters. The total worth is $0.71 with the coins weighing 162.50 grams. |
Adding: 4 quarters ($1.00) (22.68 grams) Piggy bank now has: 0 pennies, 0 nickles, 0 dimes, and 4 quarters. The total worth is $1.00 with the coins weighing 22.68 grams. Requesting $0.36 Remove: 0 pennies, 0 nickels, 0 dimes, and 2 quarters For a total of $0.50 Piggy bank now has: 0 pennies, 0 nickles, 0 dimes, and 1 quarters. The total worth is $0.25 with the coins weighing 5.67 grams. Requesting $1.63 Piggy bank has insufficient funds to honor this request Piggy bank now has: 0 pennies, 0 nickles, 0 dimes, and 1 quarters. The total worth is $0.25 with the coins weighing 5.67 grams. Adding: 38 pennies ($0.38) (95.00 grams) Adding: 86 nickels ($4.30) (430.00 grams) Adding: 48 dimes ($4.80) (108.86 grams) Adding: 38 quarters ($9.50) (215.46 grams) Adding: 26 pennies ($0.26) (65.00 grams) Adding: 55 nickels ($2.75) (275.00 grams) Adding: 74 dimes ($7.40) (167.83 grams) Piggy bank weight exceeded! No coins added. Adding: 23 quarters ($5.75) (130.41 grams) Piggy bank now has: 64 pennies, 141 nickles, 48 dimes, and 62 quarters. The total worth is $27.99 with the coins weighing 1325.40 grams. Requesting $27.53 Remove: 10 pennies, 16 nickels, 9 dimes, and 10 quarters For a total of $4.30 Piggy bank now has: 9 pennies, 5 nickles, 3 dimes, and 7 quarters. The total worth is $2.39 with the coins weighing 93.99 grams. |
amount
argument type. If you expect your function to process values such as in your expected output: $27.53, the function is supposed to accept value in double
type.while
conditions. What is this *100
supposed to mean anyway?if (value*100 > amount)
while (value + QUARTER < amount && numQuarters - quarters > 0)
QUARTER
has value of 25, but it represents a coin with value of $0.25. It is 100 times too much! You should rather use Quarter::VALUE
instead, which has both type and value consistant with PiggyBank::value
variable, so their sum makes sense in this statement. But...PiggyBank::value
until the end of the function, when you are able to verify you have enough coins of each type to make a successfull transaction. This is why you declared your temporary double valueOfWithdrawal = 0;
.
|
|
|
|
numQuarters-= quarters;
bit the code gives me entirely wrong values as it never registers I suppose, that I removed anything...then if I move it outside the if statement it just says:Requesting $0.36 Remove: 0 pennies, 0 nickels, 0 dimes, and 0 quarters For a total of $0.00 |
|
|
Little Betsy is excited about the piggy bank she has received for Christmas. Her parents have promised to give her their change every week for her to add to the bank. The bank can safely hold only 3 pounds of coins. The coin slot can only take pennies, nickels, dimes, and quarters. Betsy wants to keep track of how much money she has in the bank at any given time. She also wants to know which coins to remove when she needs to spend some money from the bank. Task: Create a Piggy Bank program that has a base Coins class and five derived classes called Pennies, Nickels, Dimes, and Quarters. The valid coins are added to a PiggyBank class that keeps track of the number of each coin in the bank, the total value of the coins in the bank and the total weight. These derived classes are short and can be placed in the same file and the base class. Thus, Coins.h will contain the class definitions for Coins, Pennies, Nickels, Dimes, and Quarters. In addition, the Coins.cpp file will contain the source code implementations of all the member functions in all these same classes. There are four global constants holding the integer value of each of the coin types: const int PENNY = 1; const int NICKEL = 5; const int DIME = 10; const int QUARTER = 25;The Coins class has four data members: int number int type double value double weight They hold the number of the particular coins being added to the bank, the type of coin and the total value and weight the coins. These data members each have a get function, but no set function: int getNumber() int getType() double getValue() double getWeight() The Coins data members, number and type, are set through the two-parameter constructor with default parameters. The constructor calculates and stores the total value and weight: Coins (int = 0, int = 0). In addition, the Coins class has a print function that displays the coin type, number, total value and total weight: void print() The four classes derived from Coins have two static const data members. They hold the individual coin value and weight (in grams) as doubles: static const double VALUE static const double WEIGHT These static variables are initialized at the top of the Coins.cpp file as: const double Pennies::VALUE = 0.01; const double Nickels::VALUE = 0.05; const double Dimes::VALUE = 0.10; const double Quarters::VALUE = 0.25; const double Pennies::WEIGHT = 2.500; const double Nickels::WEIGHT = 5.000; const double Dimes::WEIGHT = 2.268; const double Quarters::WEIGHT = 5.670; The only member function that these derived classes have is a one-parameter constructor to set the number of coins in the particular object. This constructor passes the number and coin type to the Coins constructor to store in its data members. The PiggyBank class has six data members: int numPennies int numNickelsint numDimes int numQuarters double value double weight These data members are set to zero in its only (default) constructor: PiggyBank(). There are member functions to add coins and remove coins from the PiggyBank. There are functions to return the PiggyBank value, weight and the number of each type of coin. These functions are: void addCoins (Coins&) void removeCoins (int) double getValue() double getWeight() The addCoins() member function should begin by checking the weight of the coins it is trying to add. If adding the Coins object causes the total weight of the PiggyBank to rise above three pounds, the addCoins() member function should return an error message and not add the Coins. The removeCoins() member function receives the amount to remove as a number of cents. It should begin by comparing the value of the money it has been requested to remove to the amount currently in the bank. If the bank does not contain the requested amount, then the removeCoins() function should return an error message and not remove the Coins. Function Table for Piggy Bank Program: return value function parameters Task: none Coins int number=0 int type=0 Two-parameter constructor with default values of zero. It sets the number and type of coins. It calculates the value and the weight int getType none Returns the type of coin int getNumber none Returns the number of coins double getValue none Returns the total value of the coins double getWeight none Returns the total weight of the coins void print none Displays the coin type, number, total value and total weight. See Sample Output. none Pennies int number One-parameter constructor to set number of Pennies. It calls the base Coins constructor setting this number and coin type of PENNY. none Nickels int number One- parameter constructor to set number of Nickels. It calls the base Coins constructor setting this number and coin type of NICKEL. none Dimes int number One- parameter constructor to set number of Dimes. It calls the base Coins constructor setting this number and coin type of DIME.none Quarters int number One- parameter constructor to set number of Quarters. It calls the base Coins constructor setting this number and coin type of QUARTER. none PiggyBank none Default constructor initializing all data members to 0 or 0.0. double getValue none Returns the total value of the PiggyBank double getWeight none Returns the total weight of the PiggyBank void addCoins Coins& c Add coins to the PiggyBank, making sure that the total weight is under 3 pounds. The number of coins of each particular type is updated, as well as the total weight and value.void removeCoins int amount Remove coins from the PiggyBank getting the least number of coins needed to fulfill the request. The coins are not removed when the total value in the bank is less than the requested amount which is given in cents. Note: you may have to remove more money than the requested amount based on which coins are in the bank. More Instructions: This programming exercise has five source code files: 1 Lab6B.cpp – This contains the main function (Driver Program) which is used to test your class. It will be provided to you. DO NOT modify this main function. Carefully, look at the calls to the PiggyBank class member functions from this main function. You must code YOUR PiggyBank class member functions such that this main function works exactly as given. You WILL NOT be given any credit for your effort, if your code does not work with this main function. 2 Coins.h – This contains the class definition for the Coins class and each of the classes that are derived from Coins. This file will be provided for you. 3 Coins.cpp – This contains the source code implementations of the member functions for Coins and the classes derived from Coins. 4 PiggyBank.h - This contains the class definition for the PiggyBank class. This file will be provided for you. 5 PiggyBank.cpp - This contains the source code implementations of the PiggyBank member function. |