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