CSC15 Lab 6 Due Wednesday 3/28 In the previous lab (animated stick figures) you saw a demonstration of how the organization of object-oriented programs facilitated the ease of program construction and modification. In this lab you'll be constructing your own class, which must meet my specifications. That is, my "main" function must still work with the class you will define. Review of basic OOP syntax: class className { private: // Declaration of variables and functions that you do not want to be // visible outside of the class. Most variables go here public: className(...) // the designated "constructor" function or method { // code to initialize certain variables } className() {} // alternate constructor for uninitialized declarations // Declaration of variables and functions that you want visible outside // of the class. Most functions go here. }; // end className Remember: a class is not an object but a blueprint for constructing objects. To use a class (inside a function), you create instances of this class with declarations of the form: className myinstance(...); This declares the variable "myinstance" to represent an object which contains all the components (variables and functions) declared in the class definition. The parameters (or arguments) in parentheses are the required parameters of the designated "constructor" method of the class, which is used to initialize the data inside the class. You can only access public components of the object in functions that are not defined inside the class. Review examples so that these concepts are clear to you before coding. ------- The Assignment: ------- Following the example of the bank account, minutes-seconds, and other classes shown in class, you are to contruct a class called "spareChange". The idea is that every instance of this class represents a collection of coins consisting of quarters, dimes, nickels and pennies. What you do below must be consistent with my "main" function, which is given to you. YOU CAN NOT CHANGE MY main FUNCTION! 1. Define a class called spareChange (type out the format first: public, private sections, {}'s, etc...) 2. In the private section, declare variables quarters, dimes, nickels, and pennies (these should be int variables) that will represent how many coins of each type you have in a collection. 3. In the public section, write the constructor method to initialize these variables according to parameters passed in: spareChange(int q, int d, int n, int p) { // code to initialize quarters, dimes, etc... } // q, d, n, p are the initial values of // quarters, dimes, nickels and pennies respectively // Also define the alternate constructor function 4. Define the following functions in the public section of the class: The functions must meet the specification described in comments. For additional clarification of how these functions should be defined look at how these functions are called from my main function below. a. int totalcents() // This returns the value of the coin collection in cents. For example, // if there are 2 quarters, 1 dime and 3 pennies, it should return 63. b. // The following four functions changes the number of coins depending // on the parameter passed in. I've written the first one, you write // the rest; void changePennies(int p) { pennies = pennies + p; } // add a negative value to subtract void changeNickels(int n) // ... // *** also define changeDimes and changeQuarters *** c. void printall() // This function should print out the contents of the collection // in the format "4 quarters, 2 dimes, 8 nickels, and 2 pennies" d. A function called "totalCoins". You have to make up the header. // This function takes no arguments and returns the total number // of coins in the collections. Please note that it doesn't return the // $ value of the collection, only the total number of coins of all types. // I.E., if you have 4 quarters, 2 dimes, 8 nickels, and 2 pennies it // should return 16. Look at how totalCoins is called in the main function. // to make sure it's defined correctly. e. A function called "printvalue". You are to decide if this function takes any parameters and/or return anything: // This function should print the *value* of the collection in a readable // format: "2 dollars and 11 cents" for example. For this you'll need // to use the remainder (%) function again, as you did in the timesum // program. Also, you should call your function "totalcents" which // will give you the total number of cents in your collection. After that // it'll be easy. That is, you should assign a variable to totalcents(): // x = totalvalue(); this should be one of the lines of the function. f. double dollarvalue() // This function should return the value of the collection // in floating point format. For example, if you have 2 dimes and 3 pennies // it should return 0.23 as the return value. To get C++ to treat an // integer as a "double", put (double) in front of it. For example, // ((double)10) / ((double)4) should give you 2.5, because in the expression // both 4 and 10 are treated as double values. You should also use your // totalcents function as in the above function. g. void getchange(int cents) // add to values // this function takes an integer representing the value you // want to add to the collection in cents. For example, if the // parameter is 62, you'll need to add 2 quarters, one dime and 2 pennies // to the collection. You always add the optimal (as few as possible) // number of coins. Be careful: after adding the number of quarters, // you'll need to subtract that amount from the cents variable before // adding the number of dimes: i.e., for 50 cents you don't want to // add 2 quarters AND 5 dimes (and 10 nickels and 50 pennies). After you've finished defining the class, add the following main function to complete the program (the main function should come after the entire spareChange class definition): /* YOU MUST NOT CHANGE THIS MAIN FUNCTION! */ int main() // driver for the spareChange class { spareChange mychange(3,2,0,6); // one set of spare change spareChange yourchange(2,5,4,19); // another set of spare change mychange.printall(); // prints contents yourchange.printall(); // prints contents cout << "I have $" << mychange.dollarvalue() << "\n"; cout << "You have $" << yourchange.dollarvalue() << "\n"; mychange.getchange(67); // add 67 cents' worth of (optimal) change yourchange.changeNickels(-2); // take away two nickels from your change; cout << "\n --- changes made ---\n"; cout << "\nI now have "; mychange.printall(); // prints new contents cout << "and you now have "; yourchange.printall(); // prints contents if (mychange.dollarvalue() > yourchange.dollarvalue()) { cout << "I have more money than you do.\n"; } else { cout << "I do not have more money than you do.\n"; } if (mychange.totalCoins() < yourchange.totalCoins()) cout << "You have more coins than I do.\n"; else cout << "You do not have more coins than I do.\n"; return 0; } // end main --- The output of this program should be EXACTLY: 3 quarters, 2 dimes, 0 nickels, and 6 pennies 2 quarters, 5 dimes, 4 nickels, and 19 pennies I have $1.01 You have $1.39 --- changes made --- I now have 5 quarters, 3 dimes, 1 nickels, and 8 pennies and you now have 2 quarters, 5 dimes, 2 nickels, and 19 pennies I have more money than you do. You have more coins than I do. ******************* Don't forget to #include at the top of your file. WHAT TO HAND IN: paper write-up of the getchange function source code, well-commented (remember to comment all variables declared).