0

When I use this-> operator it saves the value of complex number having higher magnitude of real value in compare function, but it also overrides the value of real variable. I am unable to assess the value of c2.real.

#include <iostream>
using namespace std;
class Complex
    {
       int real,img;
       public:
       void get()
         {
            cin>>real>>img;
         }
      void display()
       {
           cout<<real<<"+"<<img<<"i"<<endl;
       }

     Complex &compare(Complex&);
     Complex &add(Complex&);

  };

int main()
  {
      Complex c1,c2,c3;
      c1.get();
      c2.get();
      c3=c1.compare(c2);
      c3.display();
      c3=c1.add(c2);
      c3.display();
      return 0;
   }

For example for inputs 2+4i and 7+9i this function compare check for real value having high magnitude and saves the value 7 and 9 in real and img variables.

  Complex& Complex::compare(Complex &a)
     {
        if(real>a.real)
            {
               this->real=real;
               this->img=img;
            }

       else
         {
           this->real=a.real;
           this->img=a.img;
         }

         return *this;
     }

But now when I use add function it gives a sum of 14+18i which is 7+8i + 7+8i why the value of object c2.real and c2.img have been overwritten and what can i do to have a sum of 2+4i and 7+8i. Also this is a hacker rank question so the main function and class block are locked and cannot be edited I can only edit the compare and add function definitions.

    Complex& Complex::add(Complex &b)
             {
            this->real=real+b.real;
            this->img=img+b.img;
            return *this;
             }
Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
  • 6
    `compare` is a very misleading name for a mutating member function. – François Andrieux Jan 26 '18 at 16:17
  • 3
    Do you mean to do `this->real= a.real;`? `this->real` and `real` refer to the same property, and `this->real` would only *need* to be used when there is a local or parameter value of the same name (and this case can be avoided). – crashmstr Jan 26 '18 at 16:18
  • 1
    @crashmstr I don't think that's a mistake, since it's what's done in the `else` branch. If that's how it was meant to be, both branches would be identical. – François Andrieux Jan 26 '18 at 16:19
  • @François Andrieux the class block was static so i could not change the function name i can only edit the function declarations – Derek Frost Jan 26 '18 at 16:20
  • @FrançoisAndrieux yes, but `this->real = real;` has no value if you understand that you are not really doing anything. It is either a mistake or it is very poorly written (in other words, you don't need an `else`). – crashmstr Jan 26 '18 at 16:22
  • 3
    I *think* you want `compare` to be just `return (real>a.real) ? (*this) : (a);` – Caleth Jan 26 '18 at 16:25
  • @FrançoisAndrieux actually the first branch is a NOP. he could have written `if (real<=a.real) {this->real=a.real; this->img=a.img;};` – Jabberwocky Jan 26 '18 at 16:25
  • @DerekFrost what is the `compare` function supposed to do if `real>a.real` ?? – Jabberwocky Jan 26 '18 at 16:27
  • When I said that I don't think it's a mistake, I meant it seems to me like it was written as intended. Not that the code itself was right. In other words I don't believe that it's a typo, though the reasoning does seem off. – François Andrieux Jan 26 '18 at 16:28
  • @DerekFrost Please [edit] your question and provide a [MCVE] as well as an example of input, actual output and expected output. – Jabberwocky Jan 26 '18 at 16:30
  • @DerekFrost be aware that `c3=c1.compare(c2);` will actually also modify `c1` which is probably not what you want. – Jabberwocky Jan 26 '18 at 16:34
  • I suggest you read about the differences between values and references in [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). currently when you do `c1.compare(c2);` you change the value of `c1`, the previous value is *gone forever* – Caleth Jan 26 '18 at 16:34

0 Answers0