1

I'm very new to operator operloading concept and the related questions asked before were way ahead of me, so I need to ask a basic question.

Here is the .h file:

#define ACCOUNT_H

using namespace std;

class Account{
  friend Account &operator+ (Account &acc);
  friend ostream &operator<< (ostream &, Account &);

  public:
    Account(double=100.0,double=0.0,double=0.0);

    Account &returnSum(Account &otherAccount) const;
    Account& operator+=(Account &Acc1);

    void setT(double);
    void setD(double);
    void setE(double);
    double getT(void);
    double getD(void);
    double getE(void);
    void printAccount();

  private:
    double t;
    double d;
    double e;
};

#endif

I need to overload + as a global function "with single argument" (this was the challenging part for me here) and += as member function (here I assume I can't take the right hand side operand since it is a member function, so that was the problematic part). Here's my implementation for +=:

Account &Account ::operator+=(Account &Acc1){
   Account *result = new Account(Acc1.getT()+t,Acc1.getD()+d,Acc1.getE()+e);
   Acc1 = *result;
   return *this;
}

I would really appreciate if you could correct this += and write me an implementation for + overloading. I simply need the t,d,e values to be added as an Account object.

Mat
  • 202,337
  • 40
  • 393
  • 406
Umut
  • 409
  • 3
  • 7
  • 20
  • What do you want us to correct? What's the problem with your code? Does it compile? What error do you get? Have you tried anything for `operator+`? – betabandido May 27 '12 at 18:36
  • 1
    If you are new to operator overloading you may want to bookmark [this helpful question/answer](http://stackoverflow.com/questions/4421706/operator-overloading). – Blastfurnace May 27 '12 at 18:36
  • += gives only the first operands value when i run it,not the sum. – Umut May 27 '12 at 18:39

2 Answers2

6

If you want operator+ as a free function, you need:

friend Account operator+ (const Account &acc, const Account &secondAcc);

Also, operator + is a binary operator, so it's impossible for it to only receive one argument. Even when a member function, it takes 2 parameters, just that the first parameter, this, is passed under the hood.

So, your two options:

1) Member operator

class Account{
    Account operator+ (const Account &acc);
};

2) Free operator

class Account{
    friend Account operator+ (const Account &acc, const Account &secondAcc);
};

Account operator+ (const Account &acc, const Account &secondAcc)
{
}

Very important 1:

Note that I'm returning by value, not reference as you do. This is to prevent UB, as you'll probably return a local variable, which is illegal to return by reference.

Very important 2:

Account &Account ::operator+=(Account &Acc1){

   Account *result = new Account(Acc1.getT()+t,Acc1.getD()+d,Acc1.getE()+e);
   Acc1 = *result;

   return *this;

}

This code will leak. Why not use automatic storage variables:

Account &Account ::operator+=(Account &Acc1){
   Account result(Acc1.getT()+t,Acc1.getD()+d,Acc1.getE()+e);
   Acc1 = result;
   return *this;
}

Still not sure about the logic inside, but at least it doesn't leak memory. The way you have it now, you're modifying the parameter, not the object you call += on. So after, say, a+=b, a will still be the same, and b will be modified.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • I thought about that but there is a restriction in my assignment to make it with single argument. maybe there is something wrong with the assg. – Umut May 27 '12 at 18:37
  • @UmutŞenaltan, how would you use it as a unary operator? Both `x+` and `+x` as a single expression make no sense at all for what you're describing. `+x` is only valid for an explicitly positive number. – chris May 27 '12 at 18:38
  • @UmutŞenaltan as a member, it takes one explicit parameter. If not allowed, the assignment is wrong. Please note the other corrections, they're important. – Luchian Grigore May 27 '12 at 18:40
  • You should correct OP's `operator+=`. The argument should be const. Using your code, `a += b` modifies `b`, which is surely not what that method is meant to do. – mfontanini May 27 '12 at 18:48
  • @mfontanini ergo the last comment about the logic. But maybe he does want to keep `this` intact and modify the parameter, who knows?... – Luchian Grigore May 27 '12 at 18:49
  • @mfontanini I do agree it's fishy, but I bet it will turn up when he's debugging. Could be a good learning experience :) – Luchian Grigore May 27 '12 at 18:50
  • What I mean is you should at least state that in your answer. Otherwise OP might think "hey, no leaks, it's OK". I agree about the debugging experience though ;) – mfontanini May 27 '12 at 19:12
  • 1
    @mfontanini fair enough, I added that part. – Luchian Grigore May 27 '12 at 19:14
  • @UmutŞenaltan please see the last sentence of the answer. – Luchian Grigore May 27 '12 at 19:14
1
Account &Account ::operator+=(Account &Acc1){
   Account *result = new Account(Acc1.getT()+t,Acc1.getD()+d,Acc1.getE()+e);
   Acc1 = *result;
   return *this;
}

There are two big problems here. One is the leak that Luchian Grigore mentioned. Another is that this does not behave the way operator+= is supposed to behave. The problem is that you are modifying Acc1. You should be modifying this. The following code will behave very bizarrely with your overload:

Account total;
Account joes_account;
...
total += joes_account;

With your code, it is joes_account that is updated with the sum rather than the variable total.

David Hammen
  • 32,454
  • 9
  • 60
  • 108