0

I'm having a hard time understanding how to implement operator overloading. I have a simple Account class with the variables name (char[]) and balance (double). I need to overload the += operator and = operator to do the following:

  1. Overload the operator+= so that: If A and B and C are Account objects: A = B += C: adds the balance of C to B and returns the reference of B, so A will be to B afterwards.

  2. Overload the operator= so that: A = B = "new name": Sets the name of B to “new name” and returns the reference of B.

I don't know if its the wording or what but I just don't understand what I should be doing here.

I've made these declarations in my header file although I'm not sure if this correct:

    //Overload the operator+= 
    Account& operator+=(const Account&);

    //Overload the operator= 
    Account& operator=(const Account&); 

I don't even know where to start with the cpp file because I'm not even sure if my declaration is correct. This is what I've done although I'm pretty sure it's wrong (especially the 2nd one).

Account& Account::operator+=(const Account& account) {
    this->balance_ += account.balance_; 
    return *this;
}

Account& Account::operator=(const Account& account) {
    strncpy(this->name_, "new name", MAX_NAME);
    return *this;
}
TheWolf1494
  • 96
  • 1
  • 6
  • So what's wrong with what you've done then? (except for the fact that in the `operator=` you are obviously not making any use of the `account` input argument). – goodvibration Jul 15 '20 at 06:09
  • 1
    Please, provide a [mcve]. Your code doesn't look that bad (if I would write the rest around respectively) but it depends on what else did you write. The only thing which looks wrong to me: `strncpy(this->name_, "new name", MAX_NAME);`. Why to bother with this C string rubbish when you can use `std::string`? ;-) – Scheff's Cat Jul 15 '20 at 06:09
  • The copy assignment operator (`Account& Account::operator=(const Account& account)`) should copy the member variables from `account`. – Ted Lyngmo Jul 15 '20 at 06:09
  • @TedLyngmo: Your `too` is redundant. – goodvibration Jul 15 '20 at 06:10
  • @TedLyngmo Maybe... (Copying an account would double your saved money. This could lead to inflation.) ;-) – Scheff's Cat Jul 15 '20 at 06:11
  • @Scheff :-) Indeed – Ted Lyngmo Jul 15 '20 at 06:11
  • Change that `"new name"` to `account.name_`. Other than that, looks good. You might wanna just verify that `this != &account`, in case someone tries to do something like `x = x`. – goodvibration Jul 15 '20 at 06:12
  • @TedLyngmo: I mean, he ain't copying the name. – goodvibration Jul 15 '20 at 06:12
  • @goodvibration I see your point. Updated comment. – Ted Lyngmo Jul 15 '20 at 06:13
  • @goodvibration: Right, I just realized I'm not making use of the account argument. The instructions say I'm supposed to set the name to "new name". Do I not need an argument in this case? I think I'm more confused about the instructions than the actual code itself lol! – TheWolf1494 Jul 15 '20 at 06:25
  • 1
    I think the instructions additionally want you to write an assignment operator with this signature: `Account& operator=(const std::string&); ` And just implement the copy-assignment operator the usual way (`A = B`). – Jan Jul 15 '20 at 06:40
  • I don't understand the wording either. Particularly this `A = B = "new name": Sets the name of B to “new name” and returns the reference of B.` Why doesn't that also set `A` to "new name"? It seems your teacher is inventing contrived examples to teach you operator overloading. Not surprisingly you're then having a hard time understanding it. You should talk to your teacher, we can't be expected to know what they intended. – john Jul 15 '20 at 06:47

0 Answers0