0

So this a fairly easy assignment that we got from our teacher, but the thing that bugs me here is that I can't seem to get the overloading right. The assignment says:

int main()
{
cMoney papers(6, 50), coffee(5, 70), sugar(1), sugar1(sugar);
std::cout << "Total: " << papers + coffee + sugar1 << std::endl;
}

The portion of the code that I made so far is:

class cMoney{


private:
   double mIznos;


public:
    cMoney();
    cMoney(double a);
    cMoney(cMoney&);
    cMoney& operator+=(const cMoney& paper);
    ~cMoney();

    };

    cMoney::cMoney(double a){

        mIznos=a;

    }

    cMoney::cMoney(cMoney& number){

        mIznos=number.mIznos;
    }

    cMoney& cMoney::operator+=(const cMoney& paper){

            cMoney temp;
            temp.mIznos=paper.mIznos;
            return temp;
    }


    cMoney::~cMoney(){

    std::cout<<"Destructor for:"<<mIznos<<std::endl;
    }


int main(){

    cMoney paper(6.50),coffee(5.70),sugar(1),sugar1(sugar);

    return 0;
}

The problem is that I tried to make the overloading but I can't seem to figure out how to make papers + coffee + sugar1. If it was papers=coffee+sugar or something similar OK. Just for the record; I just started learning C++ so please be gentle. :D

Nebbs
  • 139
  • 1
  • 9
  • Well, to get + working, you have to overload `operator+`. I recommend you implement it in terms of `operator+=`. Also, http://stackoverflow.com/questions/4421706/operator-overloading – chris May 16 '13 at 22:49
  • @chris thx for the link, I saw it and still had trouble :S – Nebbs May 16 '13 at 22:55

1 Answers1

4

Actually, the best way to do this is quite easy. Just put this after your class. (not in)

cMoney operator+(cMoney copy_of_left, const cMoney& reference_to_right)
{return copy_of_left += reference_to_right;}

Now that I actually look at your code though, your operator+= function just copies the right hand side, and ignores the left side entirely, so you'll have to fix that first.

papers + coffee + sugar1;
//is the same as
((papers + coffee) + sugar1);
//is prefix notation is
+( +(papers,coffee), sugar1);
//in C++ is
operator+( operator+(papers, coffee) ,sugar1);

so just adding this one function is enough for C++ to automatically know how to properly compile papers + coffee + sugar1.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • but I am supposed to add three objects or did I understand it totally wrong? paper,coffee,sugar1? – Nebbs May 16 '13 at 22:54
  • @Nebbs, The return value becomes an argument of the next `operator+` call, paired with the missing object. There are two calls happening. – chris May 16 '13 at 22:54
  • @Nebbs, remeber that from a strictly formal standpoint, adding three objects is accomplished by adding the first two, and then adding that temporary result to the third. As long as you supply the binary operator, the compiler will take care of the rest. – StoryTeller - Unslander Monica May 16 '13 at 22:56
  • 1
    He's also missing the cMoney(int, int) constructor asked for in the assignment. – hookenz May 16 '13 at 23:00
  • @Matt, well, it is **his** assignment. We shuoldn't rush forward to completely solve it for him. – StoryTeller - Unslander Monica May 16 '13 at 23:02
  • 1
    @Matt not,actually the teacher made a mistake. Actually it's papers(5.70). :) – Nebbs May 16 '13 at 23:02