1

I am performing complex addition & multiplication, the following code produces worng results despite correct formulation.

#include<iostream>

using namespace std;

class Complex{
    int real,img;
    public:

    Complex(int r=0,int i=0){
        real=r;
        img=i;
    }

    Complex operator + (Complex &);

    Complex operator * (Complex &);

    void print(){
        cout<<real<<" + "<<img<<"i"<<endl; 
    }
};

Complex Complex::operator + (Complex &c2){
    //checkig the variables true value
    cout<<real<<endl;
    cout<<c2.real<<endl;

    return(real+c2.real,img+c2.img);
}

Complex Complex::operator * (Complex &c2){
    return(real*c2.real-img*c2.img,real*c2.img+img*c2.real);
}

int main(){
    Complex c1(10,5), c2(2,4);
    Complex c3 = c1 + c2;
    c3.print();
    Complex c4 = c1 * c2;
    c4.print();
    return 0;
}

Actual results

9 + 0i
50 + 0i

Expected results

12 + 9i
0 + 50i
R Sahu
  • 204,454
  • 14
  • 159
  • 270

3 Answers3

3

You are getting wrong output because you are not returning any Complex datatype.

You need to change return(real+c2.real,img+c2.img); into return Complex(real+c2.real,img+c2.img);

Same case for multiplication.

  • That worked , but why so? In general there is a type to a function it returns that type. Here why do i have to explicity return as Complex despite it being a Complex type. – Dipsankar Maity Aug 22 '19 at 05:50
  • 1
    Because you are returning comma separated values. For example `return (A,B)` will actually return `B`. First element gets discarded first, then the second element is returned. You can [see](https://stackoverflow.com/questions/39364263/different-behaviour-of-comma-operator-in-c-with-return) this answer for more details. – MD Billal Hossain Aug 22 '19 at 06:10
3

This code:

return(real+c2.real,img+c2.img);

doesn't do what you think it does.

It's evaluating real + c2.real, img + c2.img using the comma operator and then passing the result of that expression to the constructor for Complex. That generates the results you're seeing where the imaginary value is 0 and the real value is the expected imaginary value - the calculated real value is being discarded and the calculated imaginary value is being passed to the Complex constructor.

You can fix this by writing

return {real+c2.real, img+c2.img};

which will do what you actually want

James Picone
  • 1,509
  • 9
  • 18
1

The line

return(real+c2.real,img+c2.img);

is not interpreted as

return Complex(real+c2.real,img+c2.img);

Due to the comma operator, it is effectively interpreted as:

int tmp = (real+c2.real, img+c2.img);
return Complex(tmp);

The comma operator makes the value of tmp to be equal to img+c2.img.

Effectively, that line translates to

return Complex(img+c2.img);

Hence, you get the wrong result.

You can change that line to

return {real+c2.real,img+c2.img};

Better yet, make it more readable and use

return Complex(real+c2.real,img+c2.img);

Update the second function appropriately.

R Sahu
  • 204,454
  • 14
  • 159
  • 270