+= operation

Discussion in 'C++' started by foolsmart2005@gmail.com, Jun 14, 2008.

  1. Guest

    ComplexNum ComplexNum::eek:perator +(const ComplexNum& rhs) const //
    - operation is the similar way
    {
    ComplexNum ans;
    ans.real = real + rhs.real;
    ans.imaginary = imaginary + rhs.imaginary;
    return ans;
    }

    it works and my lecturer said it is correct but,
    for += operation, my code is:
    ComplexNum ComplexNum::eek:perator += (const ComplexNum& rhs) const
    {
    ComplexNum ans;
    ans.real += rhs.real;
    ans.imaginary += rhs.imaginary;
    return ans;
    }

    here is lecturer's comment:
    += should be different. a += b means a = a + b
    therefore *this object (i.e. a) should be updated with the answer.

    Do anyone know how to write += operation?
    , Jun 14, 2008
    #1
    1. Advertising

  2. Guest

    On Jun 15, 12:35 am, Sam <> wrote:
    > writes:
    > > for += operation, my code is:
    > > ComplexNum ComplexNum::eek:perator += (const ComplexNum& rhs) const
    > > {
    > > ComplexNum ans;
    > > ans.real += rhs.real;
    > > ans.imaginary += rhs.imaginary;
    > > return ans;
    > > }

    >
    > > here is lecturer's comment:
    > > += should be different. a += b means a = a + b

    >
    > Your lecturer should've also told you that your results are undefined since
    > the contents of the ans object are, apparently, not initialized. Your +=
    > operation on ans's members are applied to, apparently, uninitialized members
    > of this object. This would be the same as writing:
    >
    > int x;
    >
    > x += 4;
    >
    > Obviously, the results of this are undefined.
    >
    > > therefore *this object (i.e. a) should be updated with the answer.

    >
    > Correct.
    >
    > > Do anyone know how to write += operation?

    >
    > Yes, but why don't you try to figure this out yourself. Look around, there
    > are plenty of examples of assignment operators for other classes. Here are
    > two free clues:
    >
    > 1) operator += modifies this object, therefore it cannot be a const
    > function (well, it can be, but you're not there, yet).
    >
    > 2) The assignment operator, be it operator =(), operator +=(), operator
    > -=(), or anything else, traditional does not return another instance of the
    > object, but rather a reference to this, modified, object.
    >
    > application_pgp-signature_part
    > 1KDownload


    Of course I have searched the Internet but, on Google, I type +=
    operation, it cannot detect '+='. That's why I find unsuitable ones.
    , Jun 14, 2008
    #2
    1. Advertising

  3. Guest

    On Jun 15, 12:35 am, Sam <> wrote:
    > writes:
    > > for += operation, my code is:
    > > ComplexNum ComplexNum::eek:perator += (const ComplexNum& rhs) const
    > > {
    > > ComplexNum ans;
    > > ans.real += rhs.real;
    > > ans.imaginary += rhs.imaginary;
    > > return ans;
    > > }

    >
    > > here is lecturer's comment:
    > > += should be different. a += b means a = a + b

    >
    > Your lecturer should've also told you that your results are undefined since
    > the contents of the ans object are, apparently, not initialized. Your +=
    > operation on ans's members are applied to, apparently, uninitialized members
    > of this object. This would be the same as writing:
    >
    > int x;
    >
    > x += 4;
    >
    > Obviously, the results of this are undefined.
    >
    > > therefore *this object (i.e. a) should be updated with the answer.

    >
    > Correct.
    >
    > > Do anyone know how to write += operation?

    >
    > Yes, but why don't you try to figure this out yourself. Look around, there
    > are plenty of examples of assignment operators for other classes. Here are
    > two free clues:
    >
    > 1) operator += modifies this object, therefore it cannot be a const
    > function (well, it can be, but you're not there, yet).
    >
    > 2) The assignment operator, be it operator =(), operator +=(), operator
    > -=(), or anything else, traditional does not return another instance of the
    > object, but rather a reference to this, modified, object.
    >
    > application_pgp-signature_part
    > 1KDownload


    is this time correct?
    ComplexNum operator += (const ComplexNum& in1, const ComplexNum& in2)
    {
    ComplexNum ans;
    ans.real = in1.real + in2.real;
    ans.imaginary = in1.imaginary + in2.imaginary;
    return ans;
    }

    I make it a friend function.
    friend ComplexNum operator+=(const ComplexNum& in1, const ComplexNum&
    in2);
    , Jun 14, 2008
    #3
  4. Hi!

    Sam schrieb:
    > This would be the same as writing:
    >
    > int x;
    >
    > x += 4;


    The class could have a default constructor which initializes the
    members, though.

    Frank
    Frank Birbacher, Jun 14, 2008
    #4
  5. Frank Birbacher, Jun 14, 2008
    #5
  6. On Jun 14, 10:08 pm, ""
    <> wrote:
    > On Jun 15, 12:35 am, Sam <> wrote:
    > > writes:
    > > > for += operation, my code is:
    > > > ComplexNum ComplexNum::eek:perator += (const ComplexNum& rhs) const
    > > > {
    > > >            ComplexNum ans;
    > > >            ans.real += rhs.real;
    > > >            ans.imaginary += rhs.imaginary;
    > > >            return ans;
    > > >  }

    >
    > > > here is lecturer's comment:
    > > > += should be different. a += b means a = a + b

    >
    > > Your lecturer should've also told you that your results are undefined since
    > > the contents of the ans object are, apparently, not initialized. Your +=
    > > operation on ans's members are applied to, apparently, uninitialized members
    > > of this object. This would be the same as writing:

    >
    > >    int x;

    >
    > >    x += 4;

    >
    > > Obviously, the results of this are undefined.

    >
    > > > therefore *this object (i.e. a) should be updated with the answer.

    >
    > > Correct.

    >
    > > > Do anyone know how to write += operation?

    >
    > > Yes, but why don't you try to figure this out yourself. Look around, there
    > > are plenty of examples of assignment operators for other classes. Here are
    > > two free clues:

    >
    > > 1) operator += modifies this object, therefore it cannot be a const
    > > function (well, it can be, but you're not there, yet).

    >
    > > 2) The assignment operator, be it operator =(), operator +=(), operator
    > > -=(), or anything else, traditional does not return another instance of the
    > > object, but rather a reference to this, modified, object.

    >
    > >  application_pgp-signature_part
    > > 1KDownload

    >
    > is this time correct?
    > ComplexNum operator += (const ComplexNum& in1, const ComplexNum& in2)
    > {
    >            ComplexNum ans;
    >            ans.real = in1.real + in2.real;
    >            ans.imaginary = in1.imaginary + in2.imaginary;
    >            return ans;
    >  }
    >
    > I make it a friend function.
    > friend ComplexNum operator+=(const ComplexNum& in1, const ComplexNum&
    > in2);



    No. It is not correct yet. The idea is to have operator+= as a non-
    static member function, that's how you get a 'this' argument with it
    and hence it would just be needing one argument instead of two.
    Whatever you are doing with the local object 'ans' should be done with
    the '*this' object.

    When you get the operator+= working, as a next step, try to write
    operator+ in terms of it (operator+=). So that, at least you have to
    maintain the code of just one of those two operators.
    Abhishek Padmanabh, Jun 14, 2008
    #6
  7. On 2008-06-14 18:20, wrote:
    > ComplexNum ComplexNum::eek:perator +(const ComplexNum& rhs) const //
    > - operation is the similar way
    > {
    > ComplexNum ans;
    > ans.real = real + rhs.real;
    > ans.imaginary = imaginary + rhs.imaginary;
    > return ans;
    > }
    >
    > it works and my lecturer said it is correct but,
    > for += operation, my code is:
    > ComplexNum ComplexNum::eek:perator += (const ComplexNum& rhs) const
    > {
    > ComplexNum ans;
    > ans.real += rhs.real;
    > ans.imaginary += rhs.imaginary;
    > return ans;
    > }
    >
    > here is lecturer's comment:
    > += should be different. a += b means a = a + b
    > therefore *this object (i.e. a) should be updated with the answer.
    >
    > Do anyone know how to write += operation?


    In the += case you want to add the values in rhs to the values of the
    complex number on the left hand side (which will be "this" in the body
    of the += operator. The += operator should also return a reference to
    the number on the left hand side instead of a new object containing the
    value of the operation.

    --
    Erik Wikström
    Erik Wikström, Jun 14, 2008
    #7
  8. Guest

    On Jun 15, 9:57 am, "Daniel T." <> wrote:
    > "" <> wrote:
    > > ComplexNum ComplexNum::eek:perator +(const ComplexNum& rhs) const //
    > > - operation is the similar way
    > > {
    > > ComplexNum ans;
    > > ans.real = real + rhs.real;
    > > ans.imaginary = imaginary + rhs.imaginary;
    > > return ans;
    > > }

    >
    > > it works and my lecturer said it is correct but,
    > > for += operation, my code is:
    > > ComplexNum ComplexNum::eek:perator += (const ComplexNum& rhs) const
    > > {
    > > ComplexNum ans;
    > > ans.real += rhs.real;
    > > ans.imaginary += rhs.imaginary;
    > > return ans;
    > > }

    >
    > > here is lecturer's comment:
    > > += should be different. a += b means a = a + b
    > > therefore *this object (i.e. a) should be updated with the answer.

    >
    > > Do anyone know how to write += operation?

    >
    > class ComplexNum
    > {
    > public:
    > ComplexNum& operator+=( const ComplexNum& rhs )
    > {
    > // add code here
    > return *this;
    > }
    > // everything else
    >
    > };
    >
    > int main() {
    > ComplexNum a( 1, 2 );
    > ComplexNum b( 3, 5 );
    > a += b;
    > assert( a.real() == 4 );
    > assert( a.imag() == 7 );
    > cout << "OK\n";
    >
    > }
    >
    > Your mission is to add code where it say "add code here" so that the
    > main function will print "OK" instead of asserting.


    This time my code is:

    ComplexNum& ComplexNum::eek:perator += (const ComplexNum& rhs)
    {
    if(&rhs != this)
    {
    real+= rhs.real;
    imaginary += rhs.imaginary;
    }
    return *this;
    }

    is it correct?
    , Jun 15, 2008
    #8
  9. Sze Guest

    > This time my code is:
    >
    > ComplexNum& ComplexNum::eek:perator += (const ComplexNum& rhs)
    > {
    > if(&rhs != this)
    > {
    > real+= rhs.real;
    > imaginary += rhs.imaginary;
    > }
    > return *this;
    > }
    >
    > is it correct?


    Why shouldn`t you allow addition to itself?
    I think it does make sense to allow it, this is another case as with
    the assignment operator.
    Sze, Jun 15, 2008
    #9
  10. Guest

    On Jun 15, 11:05 am, Sze <> wrote:
    > > This time my code is:

    >
    > > ComplexNum& ComplexNum::eek:perator += (const ComplexNum& rhs)
    > > {
    > > if(&rhs != this)
    > > {
    > > real+= rhs.real;
    > > imaginary += rhs.imaginary;
    > > }
    > > return *this;
    > > }

    >
    > > is it correct?

    >
    > Why shouldn`t you allow addition to itself?
    > I think it does make sense to allow it, this is another case as with
    > the assignment operator.


    Do you mean this:
    ComplexNum& ComplexNum::eek:perator += (const ComplexNum& rhs)
    {
    if(&rhs != this)
    {
    real= real+rhs.real;
    imaginary = imaginary+ rhs.imaginary;
    }
    return *this;
    }

    ?
    , Jun 15, 2008
    #10
  11. Kai-Uwe Bux Guest

    wrote:

    > On Jun 15, 11:05 am, Sze <> wrote:
    >> > This time my code is:

    >>
    >> > ComplexNum& ComplexNum::eek:perator += (const ComplexNum& rhs)
    >> > {
    >> > if(&rhs != this)
    >> > {
    >> > real+= rhs.real;
    >> > imaginary += rhs.imaginary;
    >> > }
    >> > return *this;
    >> > }

    >>
    >> > is it correct?

    >>
    >> Why shouldn`t you allow addition to itself?
    >> I think it does make sense to allow it, this is another case as with
    >> the assignment operator.

    >
    > Do you mean this:
    > ComplexNum& ComplexNum::eek:perator += (const ComplexNum& rhs)
    > {
    > if(&rhs != this)
    > {
    > real= real+rhs.real;
    > imaginary = imaginary+ rhs.imaginary;
    > }
    > return *this;
    > }


    No.

    You made the first test case work. Now, also do this one (without breaking
    the first):

    int main() {
    ComplexNum a( 1, 2 );
    a += a;
    assert( a.real() == 2 );
    assert( a.imag() == 4 );
    cout << "OK\n";
    }



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jun 15, 2008
    #11
  12. Guest

    On Jun 15, 11:45 am, Kai-Uwe Bux <> wrote:
    > wrote:
    > > On Jun 15, 11:05 am, Sze <> wrote:
    > >> > This time my code is:

    >
    > >> > ComplexNum& ComplexNum::eek:perator += (const ComplexNum& rhs)
    > >> > {
    > >> > if(&rhs != this)
    > >> > {
    > >> > real+= rhs.real;
    > >> > imaginary += rhs.imaginary;
    > >> > }
    > >> > return *this;
    > >> > }

    >
    > >> > is it correct?

    >
    > >> Why shouldn`t you allow addition to itself?
    > >> I think it does make sense to allow it, this is another case as with
    > >> the assignment operator.

    >
    > > Do you mean this:
    > > ComplexNum& ComplexNum::eek:perator += (const ComplexNum& rhs)
    > > {
    > > if(&rhs != this)
    > > {
    > > real= real+rhs.real;
    > > imaginary = imaginary+ rhs.imaginary;
    > > }
    > > return *this;
    > > }

    >
    > No.
    >
    > You made the first test case work. Now, also do this one (without breaking
    > the first):
    >
    > int main() {
    > ComplexNum a( 1, 2 );
    > a += a;
    > assert( a.real() == 2 );
    > assert( a.imag() == 4 );
    > cout << "OK\n";
    >
    > }
    >
    > Best
    >
    > Kai-Uwe Bux


    for a +=a; this one, I have no idea. How can I do this?
    , Jun 15, 2008
    #12
  13. Kai-Uwe Bux Guest

    wrote:

    > On Jun 15, 11:45 am, Kai-Uwe Bux <> wrote:
    >> wrote:
    >> > On Jun 15, 11:05 am, Sze <> wrote:
    >> >> > This time my code is:

    >>
    >> >> > ComplexNum& ComplexNum::eek:perator += (const ComplexNum& rhs)
    >> >> > {
    >> >> > if(&rhs != this)
    >> >> > {
    >> >> > real+= rhs.real;
    >> >> > imaginary += rhs.imaginary;
    >> >> > }
    >> >> > return *this;
    >> >> > }

    >>
    >> >> > is it correct?

    >>
    >> >> Why shouldn`t you allow addition to itself?
    >> >> I think it does make sense to allow it, this is another case as with
    >> >> the assignment operator.

    >>
    >> > Do you mean this:
    >> > ComplexNum& ComplexNum::eek:perator += (const ComplexNum& rhs)
    >> > {
    >> > if(&rhs != this)
    >> > {
    >> > real= real+rhs.real;
    >> > imaginary = imaginary+ rhs.imaginary;
    >> > }
    >> > return *this;
    >> > }

    >>
    >> No.
    >>
    >> You made the first test case work. Now, also do this one (without
    >> breaking the first):
    >>
    >> int main() {
    >> ComplexNum a( 1, 2 );
    >> a += a;
    >> assert( a.real() == 2 );
    >> assert( a.imag() == 4 );
    >> cout << "OK\n";
    >>
    >> }
    >>
    >> Best
    >>
    >> Kai-Uwe Bux

    >
    > for a +=a; this one, I have no idea. How can I do this?


    Hint: ask yourself why you insist on the if(&rhs != this) check.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jun 15, 2008
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MajorTom
    Replies:
    4
    Views:
    2,267
    MajorTom
    May 27, 2005
  2. Sunil Shukla

    floating point operation in VHDL

    Sunil Shukla, Sep 1, 2004, in forum: VHDL
    Replies:
    1
    Views:
    633
    ALuPin
    Sep 2, 2004
  3. david ullua
    Replies:
    13
    Views:
    663
  4. raan
    Replies:
    2
    Views:
    449
  5. Buzz Lightyear
    Replies:
    10
    Views:
    1,119
    Alexander Bartolich
    Aug 12, 2009
Loading...

Share This Page