

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Fall 2004 Due: Tuesday, 14 Sept Emphasis on: Creating and testing a class Calculations involving monetary values can be a problem when your language offers only a floating-point type for representing fractional values. Fractional money values represented as type float are stored with extra digits of precision which can introduce errors into monetary calculations. Create a new class to represent positive dollars-and-cents values with operations that will allow accurate calculations using integer arithmetic rather than float. Your new class should define a single money object (so the client can declare as many of them as necessary for the particular client application) represented by private variables of an integer dollars value and an integer cents value. You should provide operations which allow a client to:
0.00 to +1000.00. No negative values are expected to be input or calculated.
Create a client program to test your class. Since weโre not writing a real application using this class, we just need to call each of the functions to make sure each one operates as it should. The program should read in from the keyboard values for two money objects and test the class functions, producing output similar to the following example (where a and b are the names of money objects). You donโt have to demonstrate all possible ways of declaring and assigning values to money variables; the client can either call the class function to input a money value (#3 in the list of required functions), or the client can input the value and call the class function to assign the value to the money variable (#4 in the list of required functions). For a = 29.99, dollars = 29 and cents = 99 For b = 14.95, dollars = 14 and cents = 95 29.99 + 14.95 = 44. 29.99 - 14.95 = 15. 29.99 * 5 = 149.95 Note: the 5 used here can be a constant 14.95 * 5 = 74. 29.99 / 3 = 9.99 Note: the 3 used here can be a constant 14.95 / 3 = 4. 29.99 < 14.95 is false 14.95 < 29.99 is true 29.99 = 14.95 is false 29.99 > 14.95 is true 14.95 > 29.99 is false Then prompt the user to input new values for a and b and repeat the sequence to produce similar output using the new values. Example of a partial client program: int main () { Money a, b, c; // a, b, and c are initialized to 0. cout << โEnter a dollars-and-cents value, like 14.75 => โ; cin >> a; // or a.Input(cin); cout << โEnter another dollars-and-cents value => โ; cin >> b; // or b.Input(cin); cout << โFor a = โ << a << โ, dollars = โ << a.GetDollars() << โ and cents = โ << a.GetCents() << endl; c = a + b; // or c = a.Add(b); cout << a << โ + โ << b << โ = โ << c << endl; // or if you donโt have the output operator overloaded: // a.Output(cout); // cout << โ + โ; // b.Output(cout); // cout << โ = โ; // c.Output(cout); // cout << endl; c = a * 5; // or c = a.Multiply(5); cout << a << โ * 5 = โ << c << endl; cout << a << โ < โ << b << โ is โ << (a < b) << endl; return 0; }