Create a class called DollarsCents for performing arithmetic with money. A driver program is given to you to test your class. Use integer variables to represent the private data members of the class:
int cents;
int dollars;
Here cents is the amount (number) of cents and dollars is the amount (number) of dollars. The private data members are set through the two-parameter constructor that has default values for these data members. This class has no setters. However, the values of cents and dollars can be updated via an assignment from one DollarsCents object to another. The cents and dollars data members should be reduced and stored in simplified form (where 0 <= cents < 99) after their values are set within the constructor or after some arithmetic operations. You must code a simplify function.
Provide member functions that perform each of the tasks given in the table below. Each of them should have public access, except for the simplify utility function, which should be a private member function:
return value: none
function: DollarsCents
parameters: int numCents=0
Task: Two parameter constructor with default values for dollasrs and cents provided in the prototype. It should set the dollars and cents data members in simplified form
return value: int
function: getDollars
parameters: none
task: returns the amount of dollars
return value: int
function: getCents
parameters: none
task: returns the amount of cents
return value: DollarsCents
function: Subtract
parameters: DollarCents dc
task: subtract another DollarsCents object from this one and return the result in a third DollarsCents object
return value: DollarsCents
function: add
parameters: DollarCents dc
task: Add another DollarsCents object to this one and return the result in a third DollarsCents object
return value: DollarsCents
function: multiply
parameters: int const num
task: Multiply this DollarsCents object by a whole no. and return the result in another DollarsCents object
return value: DollarsCents
function: divide
parameters: int const num
task: divide this DollarsCents object by a whole no. and return the result in a second DollarsCents object
discard any leftover cents
return value: void
function: simplify
parameters: none
task: utility function that simplifies the data members dollars and cents so that 0 <= cents <100
return value: void
function: print
parameters: none
task: Displays the DollarsCents object as a double
the main function is below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "DollarsCents.h" // include definition of class DollarsCents
using namespace std;
int main()
{
cout << fixed << setprecision(2);
int const divideBy = 2;
int const multiplyBy = 3;
// Create three DollarsCents objects
DollarsCents dc;
int dollars1 = 2;
int cents1 = 2121;
DollarsCents dc1 (dollars1, cents1);
int dollars2 = 3;
int cents2 = 333;
DollarsCents dc2 (dollars2, cents2);
// Addition
cout << “Adding “ << dollars2 << “ dollars and “ << cents2
<< “ cents” << endl;
cout << “To “ << dollars1 << “ dollars and “ << cents1
<< “ cents :“ << endl << endl;
dc1.print(); // prints DollarsCents object dc1
cout << " + ";
dc2.print(); // prints DollarsCents object dc2
dc = dc1.add(dc2); // adds object dc2 to dc21
cout << " = "; // places results in DollarsCents dc
dc.print(); // prints DollarsCents object dc
cout << endl << endl;
// Subtraction
cout << “Subtracting “ << dollars2 << “ dollars and “ << cents2
<< “ cents” << endl;
cout << “From “ << dollars1 << “ dollars and “ << cents1
<< “ cents:“ << endl << endl;
dc1.print(); // prints DollarsCents object dc1
cout << " - ";
dc2.print(); // prints DollarsCents object dc2
dc = dc1.subtract(dc2); // subtracts object dc2 from dc1
cout << " = "; // places results in DollarsCents dc
dc.print(); // prints DollarsCents object dc
cout << endl << endl;
// Multiplication by a constant
cout << “Multiplying “ << dollars1 << “ dollars and “ << cents1
<< “ cents” << “ by “ << multiplyBy << “:“ << endl << endl;
dc1.print(); // prints DollarsCents object dc1
cout << " * " << multiplyBy;
dc = dc1.multiply(multiplyBy); // multiplies object dc1 by multiplyBy
cout << " = "; // places results in DollarsCents dc
dc.print(); // prints DollarsCents object dc
cout << endl << endl;
// Division by a constant
cout << “Dividing “ << dollars1 << “ dollars and “ << cents1
<< “ cents” << “ by “ << divideBy << “:“ << endl << endl;
dc1.print(); // prints DollarsCents object dc1
cout << " / " << divideBy;
dc = dc1.divide(divideBy); // divides object dc1 in divideBy
cout << " = "; // places results in DollarsCents dc
dc.print(); // prints DollarsCents object dc
cout << endl << endl;
system("pause");
return 0;
} // end main
|
ANYTHING WILL HELP