Non Member Operator Overloading c++

In this assignment I am supposed to un-comment what my professor has given us and test each function by implementing it in Money.cpp. For the first overloaded + function I have a decent understanding, the Money& left, and Money& right are the two sets of dollar and cent parameters that need to be added. As far as I can tell he has already implemented functions for cents to adjust when it goes past 100, but I cannot figure out how to pass those parameters through the overloaded+ function.

Money.h
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
#ifndef MONEY_H
#define MONEY_H

#include <iostream>
using namespace std;

#include "Memo.h"

class Money
{
public:
   Money();			   // 0 dollars 0 cents
   Money(int dollars); // 0 cents
   Money(int dollars, int cents);
   Money(int dollars, int cents, string memo);
   int getDollars() const;
   int getCents() const;
   Memo* getPmemo() const;
   virtual ~Money(); // always declare destructor as VIRTUAL

   // Money(const Money& right); // copy constructor, Memo object is copied

// overloaded operators using member functions
// implement the following ones by uncommenting them

    //Money& operator=(const Money& right); // Memo object is copied
   // Money& operator+=(const Money& right); // messages in Memo are concatenated if not NULL
   // Money& operator-=(const Money& right); // no change in Memo
   // Money& operator++(); // prefix increment cents, no change in Memo
   // Money operator++(int unused); // postfix increment cents, no change in Memo

private:
   int dollars;
   int cents;
   Memo* pmemo; // a pointer to a Memo object
};

// overloaded operators using non-member functions
// implement the following ones by uncommenting them

ostream& operator<<(ostream& out, const Money& value); // print $ddd.cc[memo]
 Money operator+(const Money& left, const Money& right); // messages in Memo are concatenated
 Money operator-(const Money& left, const Money& right); // message of left is copied
// bool operator<(const Money& left, const Money& right); // compares value only
// bool operator>(const Money& left, const Money& right); // compares value only
// bool operator==(const Money& left, const Money& right); // compares value && memo
// istream& operator>>(istream& in, Money& r); // read dollars cents memo

#endif


Money.cpp

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
#include <iostream>
#include <iomanip>
using namespace std;
#include "Money.h"
//***********************************
// implementation of member function
//***********************************
Money::Money() {
    this->dollars = 0;
    this->cents = 0;
    this->pmemo = NULL;
}
Money::Money(int dollars) {
    this->dollars = dollars;
    this->cents = 0;
    this->pmemo = NULL;
}
Money::Money(int dollars, int cents) {
    // if cents exceed 99, adjust dollars
    this->cents = cents % 100;
    this->dollars = dollars + cents / 100;
    this->pmemo = NULL;

	 
}
Money::Money(int dollars, int cents, string message) {
    this->cents = cents % 100;
    this->dollars = dollars + cents / 100;
    this->pmemo = new Memo(message);

}
Money::~Money() {
    if (pmemo != NULL) delete pmemo;
}
int Money::getDollars() const {
    return dollars;
}
int Money::getCents() const {
    return cents;
}
Memo* Money::getPmemo() const {
    return pmemo;
}
Money operator+(const Money& right, const Money& left) {
	Money Money1(right.getDollars(), right.getCents());
	Money Money2(left.getDollars(), left.getCents());
	Money Money3(Money1 + Money2); 
	return Money3; 
}
/*Money operator-(const Money& right, const Money& left) {
	
	 
}*/
	

//****************************************
// implementation of non-member functions
//****************************************
ostream& operator<<(ostream& out, const Money& value) {
    out << "$" << value.getDollars() << "." 
    	<< setfill('0') << setw(2) << value.getCents();
    if (value.getPmemo() != NULL)
    	out << "[" << (value.getPmemo())->getMessage() << "]";
    return out;
}


Memo.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef MEMO_H
#define MEMO_H

//stores message
#include <string>
using namespace std;

class Memo {
public:
	Memo();
    Memo(string message);
    string getMessage() const;
private:
    string message;
};
#endif 


Memo.cpp

1
2
3
4
5
6
7
8
9
10
#include "Memo.h"
Memo::Memo() {
    message = "";
}
Memo::Memo(string message) {
    this->message = message;
}
string Memo::getMessage() const {
    return message;
}


Main.cpp

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
#include <iostream>

using namespace std;
#include "Money.h"
int main()
{
   // testing the constructor in case 'cents' exceeds 99
   // testing << output operator
   Money m1(0, 351, "money1");
   cout << "Expected: 3 dollars 51 cents ... " << m1 << endl;
   
   // testing overloaded + operator
   Money m2(2, 45), m3(3, 57);
   Money m4 = m2 + m3;
   cout << "Expected: 6 dollars 02 cents ... " << m4 << endl;
    /*
   // testing overloaded - operator
   Money m5(3, 15, "money5"), m6(1, 99, "money6");
   Money m7 = m5 - m6;
   cout << "Expected: 1 dollar 16 cents ... " << m7 << endl;
  
   // testing += operator
   m7 += m6;
   cout << "Expected: 3 dollars 15 cents ... " << m7 << endl;
   // testing + operator with an integer
   Money m8 = 2 + m7; // implicit conversion from int (2) to Money m(2)
   cout << "Expected 5 dollars 15 cents ... " << m8 << endl;
   // testing >> input operator
   cout << "TYPE: 7 45 money9 ";
   Money m9;
   cin >> m9;
   cout << "Expected 7 dollars 45 cents[money9] ... " << m9 << endl;
   // testing ++ prefix operator
   for (int i = 0; i < 99; i++) // 99 times
   		++m9;
   cout << "Expected 8 dollars 44 cents[money9] ... " << m9 << endl;
   // testing ++ postfix operator
   Money m10 = m9++; // copy constructor is called!
   cout << "m10 Expected 8 dollars 44 cents[money9] ... " << m10 << endl;
   cout << "m9 Expected 8 dollars 45 cents[money9] ... " << m9 << endl;
   // testing > < == boolean operators
   if (m9 > m10) {
       cout << "m9 is greater than m10." << endl;
    }
    if (m10 < m9) {
        cout << "m10 is less than m9." << endl;
    }
    if (m9 == m10)
        cout << "m9 == m10" << endl;
    else
        cout << "m9 is not equal to m10." << endl;
    Money m11(8, 45, "money11");
    cout << "m9 = " << m9 << "  m11 = " << m11 << endl;
    if (m11 == m9)
       cout << "m11 == m9" << endl;
    else
      cout << "m11 != m9 because of difference in Memo." << endl;
//****************************************
//**** Add your own test code below ******
//****************************************
   return 0;
   */
}
To shorten this (that's a lot of code!)
Declaration:
Money operator+(const Money& left, const Money& right);

Definition:
1
2
3
4
5
6
Money operator+(const Money& right, const Money& left) {
	Money Money1(right.getDollars(), right.getCents());
	Money Money2(left.getDollars(), left.getCents());
	Money Money3(Money1 + Money2); 
	return Money3; 
}


When things like this aren't defined in a class, the two parameters represent the left and right hand sides.

First, I see a problem: your definition has right as the first parameter and left as the second problem.
Next, you set Money3(Money1 + Money2). You use the operator+ function in your function. That's recursion and it doesn't have an exit conditon.

The function that your professor has provided is a constructor. To use it create an object with two int values like so:

Try this:
1
2
3
4
5
6
7
8
Money operator+(const Money& left, const Money& right) {

    int TotalDollars = right.getDollars() + left.getDollars();
    int TotalCents   = left.getCents() + right.getCents();

    Money Output(TotalDollars, TotalCents);
    return Output;
}


Or a shorter version (that I like better):
1
2
3
4
5
Money operator+(const Money& left, const Money& right) {

    return Money(right.getDollars() + left.getDollars(),
                 right.getCents()   + left.getCents());
}

Last edited on
Topic archived. No new replies allowed.