-1

Given the following classes:

class A { 
int a;
public:
//..
};

class B : public A {
int b;
public:  
//.....
};

How can I implements operator+= in class B so that given B b1(.. , ..); and B b2(.. , .. ); If I will do b1+=b2; so I will get in b1 the following value for his fields:

b1.a = b1.a + b2.a ,and b1.b = b1.b + b2.b

In the following case:

class A { 
protected:
int a;
public:
//..
};

class B : public A {
int b;
public:  
B& operator+=(const B& bb){
this->a += bb.a; // error..
this->b += bb.b;
return *this;
};

My problem is how can I get the fields of class A..?

AskMath
  • 415
  • 1
  • 4
  • 11
  • 4
    Did you try to research operator overloading? What did you try? Where'd you get stuck? – scohe001 Jul 09 '18 at 22:11
  • See "Binary arithmetic operators" here: https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading/4421708 – alter_igel Jul 09 '18 at 22:13
  • See "Effective C++" and "More Effective C++" by Scott Meyers. – Thomas Matthews Jul 09 '18 at 22:13
  • @scohe001 Yes, I try to research operator overloading. But in this case, I don't understand how can I get (without define the field of `class A` as protected or public) the field of `class A`. – AskMath Jul 09 '18 at 22:14
  • @ThomasMatthews My question is in case of inheritance (for the derived class). I don't to do it for a simple class. – AskMath Jul 09 '18 at 22:15
  • 1
    Define a method, let's call it `assign_plus` as protected in your base class. In your derived class' `operator+=()`, call the base class `assign_plus` method. The `assign_plus` should only manipulate variables in the base class and make no other assumptions. Don't try inheriting overloaded operators. – Thomas Matthews Jul 09 '18 at 22:19
  • Are you against having a public `operator+=` in your derived class that just adds the `a`'s? – scohe001 Jul 09 '18 at 22:21
  • You mean maybe to public fields in the base class? (I don't understand your question, because that `operator+=` is a public in my question. No?) – AskMath Jul 09 '18 at 22:22
  • @ThomasMatthews Can you explain it with answer about the question please? I will be happy to understand your idea :) . – AskMath Jul 09 '18 at 22:24
  • @AskMath see my answer... – scohe001 Jul 09 '18 at 22:29
  • @scohe001 Ok , and you understand what Thomas mean while he said " Don't try inheriting overloaded operators." ? [and Why no to do it?] – AskMath Jul 09 '18 at 22:30
  • I'm not sure why @ThomasMatthews was telling you not to inherit overloaded operators, that's not something I've ever heard. I could easily just be missing something though... – scohe001 Jul 09 '18 at 22:33
  • [I can't reproduce the problem](https://ideone.com/avYgmn). The original code works fine for me (after correcting a few syntax mistakes that prevented compiling). – Remy Lebeau Jul 09 '18 at 22:35
  • @RemyLebeau OP mentions they want `a` to be private but showed they can get it to work if it's not. This is their issue. See their first comment on this post. – scohe001 Jul 09 '18 at 22:41
  • @RemyLebeau It's not like in this question? https://stackoverflow.com/questions/3247671/accessing-protected-members-in-a-derived-class (But anyway, I want the fields will be as a private like that I mentioned in the comments above). – AskMath Jul 09 '18 at 22:41
  • @AskMath that information belonged in your question, not in comments. Nothing in your question says anything about that requirement, or even the exact error you are dealing with. Besides, `A::a` is `private` by default, but you explicitly declared it as `protected`, so `B` has access to it. So what is the REAL problem you are trying to solve? – Remy Lebeau Jul 09 '18 at 22:46
  • @RemyLebeau I wanted that the field of `A` will be private for provide "hide" for the class. In addition, I thought that it's will make to error like in the link that I added (like in this case: https://stackoverflow.com/questions/3247671/accessing-protected-members-in-a-derived-class) , In fcat, why it's not a same to the case that provided in this link? – AskMath Jul 09 '18 at 22:49
  • @RemyLebeau And it's what that I did in the first example.. (The second example was for scohe001 - he asked it). ***But***, why you don't get error while you do it with protected? it's not the case that provided in the link that I added ? – AskMath Jul 09 '18 at 22:51
  • @AskMath stop posting details in **comments** and start EDITING YOUR QUESTION to have the relevant information. The code IN YOUR QUESTION declares `a` as `protected` and not as `private`, that is why the code WORKS – Remy Lebeau Jul 09 '18 at 22:52
  • @RemyLebeau Ok , can you explain why you don't get error? – AskMath Jul 09 '18 at 22:53
  • @AskMath because `B` is using `public` inheritance, and so it has access to `A`'s `protected` members. – Remy Lebeau Jul 09 '18 at 22:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174683/discussion-between-askmath-and-remy-lebeau). – AskMath Jul 09 '18 at 22:54

1 Answers1

3

Give A its own operator+=! Then you just have to call it from B:

class A { 
private:
    int a;
public:
    A(int a) : a(a) { } 

    A& operator+=(const A &other) {
        a += other.a;
        return *this;
    }   
};

class B : public A { 
private:
    int b;
public:
    B(int a, int b) : A(a), b(b) { } 

    B& operator+=(const B &other) {
        A::operator+=(other);
        b += other.b;
        return *this;
    }   
};

See a working example here (ideone link).

scohe001
  • 15,110
  • 2
  • 31
  • 51
  • Firstly, thank you about the answer. Secondly, can you explain this line : `this->A::operator+=(other);` , what is say `this->A` , after all, `A` is a class.. – AskMath Jul 09 '18 at 22:35
  • 1
    @AskMath the `this->` is optional, but I prefer to make it explicit. `A::` means "In the scope of `A`..." So `A::operator+=(...` means "call the function `operator+=()` in the scope of `A`." We have to be specific since if I'd just done `operator+=(other)` it would've called the function we're writing! – scohe001 Jul 09 '18 at 22:37
  • 1
    @AskMath First off, you don't need to use `this->` everywhere, that is considered bad form. Second, calling `A::operator+=()` is telling the compiler to call the inherited `operator+=` from the `A` class, not the `operator+=` in the current `B` class (that would lead to an endless recursive loop!) – Remy Lebeau Jul 09 '18 at 22:37
  • Didn't realize it was considered bad form @RemyLebeau. I'll edit it out. – scohe001 Jul 09 '18 at 22:38