Function that adds two objects in the same class?

I need to write a member function in class Money that adds two Money objects together and returns the result. My instructor told me this function does not have two parameters.

Any idea on how to do write this function?
Any advice would be greatly appreciated
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
  #include <iostream>
using namespace std;

/*class definition*/
class Money {
private:
   int dollars;
   int cents;
public:
   Money ();
   Money (int d, int c);
   void print ();
   void set (int d, int c);
   int add ();
};

int main()  {
   Money price;
   price.set (9, 99);     // sets the value to $9.99
   price.print();        // displays "$9.99" on console
   Money tax;
   tax.set (0, 87);   // sets the value to $0.87
   tax.print();   // displays "$0.87" on console

}

Money::Money ()   {  //Default Constructor
   dollars=0;
   cents=0;
}

Money::Money (int d, int c)   { //Parameterized constructor
   dollars = d;
   cents = c;
}

void Money::print () {
   /*Display the money on the screen */
   cout<<"$"<<dollars<<"."<<cents<<endl;
}

void Money::set (int d, int c) {
   /*sets the value for the object to "d" dollars and "c" cents.*/
   dollars=d;
   cents=c;
}
You want to write Money Money::operator+(const Money &right);
Then you can do:
1
2
Money a,b,c;
c = a+b;     // calls a.operator+(b) 


For what it's worth, I usually write operator+= instead. Then operator + is almost trivial:
1
2
3
4
5
6
7
Money
Money::operator+(const Money &right)
{
    Money result(this);
    result += right;
    return result;
}
If you are going to overload operators I would also go ahead and overload the = operator

Money& Money::operator=(const Money &rhs)
Thanks @dhayden and @motobus for the reply.
Now, I've made a progress on my program and I think it's getting really close to obtain the intended result, but somehow my addition produces the wrong result.
9.99 + 0.87 is equal to 10.86, but in this program I got 10.86. Anyone knows why?

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
#include <iostream>
using namespace std;

/*class definition*/
class Money {
private:
   int dollars;
   int cents;
public:
   Money ();
   Money (int d, int c);
   void print () const;
   void set (int d, int c);
   void addDollars (int d);
   void addCents (int c);
   Money operator+(const Money & m) const;
};

int main()  {
   Money price;
   price.set (9, 99);     // sets the value to $9.99
   price.print();        // displays "$9.99" on console
   Money tax;
   tax.set (0, 87);   // sets the value to $0.87
   tax.print();   // displays "$0.87" on console
   Money total;
   total= price+tax;
   cout<< "total = ";
   total.print();
}

Money::Money ()   {
   dollars=0;
   cents=0;
}

Money::Money (int d, int c)   {
   dollars = d;
   cents = c;
}

void Money::print () const{
   /*Display the money on the screen */
   cout<<"$"<<dollars<<"."<<cents<<endl;
}

void Money::set (int d, int c) {
   dollars=d;
   cents=c;
}

void Money::addDollars (int d) {
   dollars += d;
}

void Money::addCents(int c) {
   cents += c;
   dollars += cents/100;
   cents &= 100;
}

Money Money::operator+(const Money & m)const {
   Money sum;
   sum.cents= cents + m.cents;
   sum.dollars= dollars + m.dollars + sum.cents /100;
   sum.cents &= 100;
   return sum;
}


/*------MY OUTPUT-----
$9.99
$0.87
total = $10.32
--------------------------*/
Last edited on
Line 66 should be sum.cents %= 100;. % is modulo. & bit bit-wise and.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Money& Money::operator=(const Money &rhs){
	if (this != &rhs){
		this->dollars = rhs.dollars;
		this->cents = rhs.cents;
	}
	return *this;
}

Money Money::operator+(const Money& rhs){
	Money temp;

	temp.cents = this->cents + rhs.cents;
	
	if (temp.cents > 99){
		temp.cents = temp.cents % 100;
		temp.dollars = this->dollars + rhs.dollars + 1;
	}
	else
		temp.dollars = this->dollars + rhs.dollars;
	
	return temp;
}
Topic archived. No new replies allowed.