Arithmetic with objects and classes

I've been scratching my head at this for a while and I just can't figure it out for some reason, any help or direction at all would be great. I just am having trouble wrapping my head around the idea of operator overloading and how I would add/subtract/multiply/divide objects. Thanks for any help!

Directions of assignment:
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
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


Instructor provided code
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
87
88
89

#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 << " :"
         << 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 three 
    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 half 
    cout << " = ";          		// places results in DollarsCents dc
    dc.print(); 				    // prints DollarsCents object dc
    cout << endl << endl;
   	
    system ("pause");
    return 0;

} // end main   


My class .h file
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
#include <iostream>
#include <iomanip>
#include <cstdlib>

//DollarsCents class definition
class DollarsCents
{
//variables
private:
	int dollars;
	int cents;
	void simplify()
	{
		int numDollars = 0;
		int numCents = 0;

		numCents = cents % 100;
		numDollars = dollars + cents - (cents % 100);
	}
//functions
public:
	DollarsCents(int numDollars, int numCents);
	int getDollars();
	int getCents();
	int add(DollarsCents dc);
	int subtract(DollarsCents dc);
	int divide(int const num);
	int multiply(int const num);
	void print();
}; 


My .cpp file for function implementation
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

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "DollarsCents.h"
using namespace std;


//default constructor
DollarsCents::DollarsCents(int numDollars, int numCents)
{
	dollars = numDollars;
	cents = numCents;
}


//Get Functions
int DollarsCents::getDollars()
{
	return dollars;
}

int DollarsCents::getCents()
{
	return cents;
}

//Calculation Functions
int DollarsCents::add(DollarsCents dc)
{
	return dc += dc;
}

int DollarsCents::subtract(DollarsCents dc)
{
	return dc -= dc;
}

int DollarsCents::divide(int const num)
{
	return dc /= num;
}

int DollarsCents::multiply(int const num)
{
	return dc *= num;	
}

void DollarsCents::print()
{
	cout << dollars << "." << cents;
}


I'm sorry if this is entirely silly...I don't have much experience in programming, and for some reason this lesson in particular has gone entirely over my head...
An easy way to go about doing the arithmetic operations (particularly the multiply and divide) is to convert everything to cents first, then do the math on the cents, then call simplify() to get it back to dollars and cents.
Ok, would that work within the parameters of the problem? If simplify() is a private utility function isn't it already involved in the entirety of the code without calling it?

I fiddled a bit and started getting these error messages...

1
2
3
4
5
6
7
8
9
10
11
12
13
error C2512: 'DollarsCents' : no appropriate default constructor available
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
dollarscents.h(42): could be 'DollarsCents &DollarsCents::operator =(const DollarsCents &)'
1>          while trying to match the argument list '(DollarsCents, int)'
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
dollarscents.h(42): could be 'DollarsCents &DollarsCents::operator =(const DollarsCents &)'
1>          while trying to match the argument list '(DollarsCents, int)'
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
dollarscents.h(42): could be 'DollarsCents &DollarsCents::operator =(const DollarsCents &)'
1>          while trying to match the argument list '(DollarsCents, int)'
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
dollarscents.h(42): could be 'DollarsCents &DollarsCents::operator =(const DollarsCents &)'
1>          while trying to match the argument list '(DollarsCents, int)'
Updated .cpp file...I don't believe there's anything wrong with the .h file, its just the .cpp file that's giving me some trouble, same errors as previously posted.

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "DollarsCents.h"
using namespace std;


//default constructor
DollarsCents::DollarsCents(int numDollars, int numCents)
{
	dollars = numDollars;
	cents = numCents;
}


//Get Functions
int DollarsCents::getDollars()
{
	return dollars;
}

int DollarsCents::getCents()
{
	return cents;
}

//Calculation Functions
int DollarsCents::add(DollarsCents dc)
{
	return dc.dollars + dollars, cents + cents;
}

int DollarsCents::subtract(DollarsCents dc)
{
	return dc.dollars - dollars, cents - cents;
}

int DollarsCents::divide(int const num)
{
	return (dollars + cents)/num;
}

int DollarsCents::multiply(int const num)
{
	
	return (dollars + cents)*num;	
}

void DollarsCents::print()
{
	cout << dollars << "." << cents;
}
Last edited on
I think I'm confused most by the error saying that there's no default constructor available? I thought I created that unless I made an error in doing so? Anyone got any suggestions?
Last edited on
I hate to be a bother, but suggestions/aid/examples/something would be much appreciated, I'm quite stuck.
There's a lot of room for change. Here's some of the things that need to happen:

1.) DollarsCents.subtract() should return a DollarsCents object, not an integer. Same goes for DollarCents.add()

2.) Nice try, but you can't return multiple values. I'm talking about DollarsCents.subtract() and DollarsCents.add(). Once you correctly modify the functions as I've said in the first step, this error should resolve itself.

3.) You need to add a default constructor (one which takes no parameters), in which it would be a good idea to initialize dollars and cents to zero.
Oh my goodness I feel so foolish ok, I worked with it after getting a lot more caffeine in my system. I finally got it to compile however now I'm just getting the wrong numbers everything equals 1.1 apparently...
Ok I'd imagine that has to do with my simplify function... am I doing something arithmetically incorrect?
Actually, yes. Your simplify function is incorrect.

You create two local variables (numDollars and numCents), which are the only variables you modify.They then fall out of scope(effectively get discarded) when the function ends, so you haven't really changed anything.

In addition, the math isn't quite correct either.
My first instinct would have been to use the modulus operator as well, but something like this seems more straight forward in your case:

1
2
3
4
5
6
void DollarsCents::simplify() {
	while(cents>=100) {
		cents-=100;
		dollars++;
	}
}
Last edited on
Ahhhhh yay thank you soo soo much, I figured it out!! You are absolutely fantastic thank you so much for your direction!! My only concern now is the divide function... I keep getting 11.10 as my result when I should be getting 11.60...how do I get rid of fractional cents, not whole cents? I seem to be discarding them all. Would I use the modulus operator in some fashion? I hope I haven't bothered you too much, you've been such a wonderful help!!
No worries, I like helping people when I can. To be honest I feel kind of silly that I didn't see some of these things earlier.

Now, I'm not sure what your current divide function looks like, since I'm not sure if it's been modified since you posted it here. I'm just going to assume it still looks like the following:

1
2
3
4
int DollarsCents::divide(int const num)
{
	return (dollars + cents)/num;
}


Which does yield some strange results.
Let's assume I have 5 dollars and 10 cents.
If I divide that amount of money by 2, the result should be $2.55 (2 dollars and 55 cents).

Here's what your divide function actually returns:

1.) (dollars + cents) = 5 + 10 = 15

2.) 15/2 = 7.5

3.) And, because it's an integer that's getting returned, it would return 8.

Which, as we know, is not correct.
By the way, based on your original post, it looks like your instructor wants this function (and the multiply function) to also return a DollarsCents object, not an integer, which makes sense.

I would probably do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
DollarsCents DollarsCents::divide(const int num) {

	int totalCents = (dollars*100) + cents;

	totalCents /= num;

	DollarsCents temporary(0, totalCents);

	temporary.simplify();

	return temporary;
}


First, we declare a variable named "totalCents". We then convert dollars into cents by multiplying the dollars by 100. We add the converted dollars and the cents to totalCents. Now, we can work with the currency more easily than if we didn't convert everything to one format.

Then, we perform the actual division.

Next, we create a new DollarsCents object called "temporary", and pass zero dollars and totalCents as arguments to its constructor.

Then, we call temporary's method "simplify" to effectively convert its cents into dollars and cents.

Finally, we return the temporary DollarsCents object.

You can also use the above function as a "template" function for your multiply() function, which probably isn't correct either. They're basically the same, only that in the case of multiply(), you're not dividing, but instead, multiplying.
Last edited on
Thank you again, I think I get it now, it works wonderfully!!
Topic archived. No new replies allowed.