laserlight .. that's fine, but what's wrong with my code ? why does (friend) keyword cause my program to operate or not to operate ??
grumpy .. it doesn't work meaning that I get garbage output, no compile error
here is my full code:
Complex Class:
Code:
class Complex
{
private:
float real, imag;
public:
Complex()
{
setAll();
cout<<"Cn"<<endl;
}
// Complex (Complex &c)
// {
// cout<<"CC"<<endl;
// }
//friend Complex operator +(float r,Complex c);
Complex(float i)
{
setImag(i);
cout<<"Ci"<<endl;
};
Complex(float r,float i)
{
this->setAll(r,i);
cout<<"Cri"<<endl;
}
~Complex()
{
cout<<"D"<<endl;
}
void setReal(float r)
{
real=r;
}
void setImag(float i)
{
imag=i;
}
float getReal()
{
return real;
}
float getImag()
{
return imag;
}
void setAll()
{
this->real=this->imag=0;
}
void setAll(float r, float i)
{
this->real=r;
this->imag=i;
}
Complex operator +(Complex c)
{
Complex t;
t.setReal(real+c.getReal());
t.setImag(imag+c.getImag());
return t;
}
Complex operator +(float r)
{
return Complex(r+real,imag);
}
Complex operator -(float r)
{
return Complex(r-real,imag);
}
operator float()
{
return real;
}
Complex operator -(Complex c)
{
Complex t;
t.setReal(real-c.getReal());
t.setImag(imag-c.getImag());
return t;
}
Complex operator =(Complex c)
{
real=c.getReal();
imag = c.getImag();
return *this;
}
int operator ==(Complex c)
{
return ((real==c.getReal())&&(imag==c.getImag()));
}
void operator +=(float v)
{
real+=v;
imag+=v;
}
void operator -=(float v)
{
real-=v;
imag-=v;
}
/* Complex operator *(Complex c)
{
return Complex(real*c.getReal - imag*c.getImag(),real*c.getImag() + imag*c.getReal());
} */
void printme()
{
printf("%f+ i(%f)\n",real,imag);
}
//friend Complex operator +(float r,Complex c);
};
and the main.cpp file:
Code:
int main()
{
Complex c1(-2,-7);
Complex c2(-1,-7);
c1=2.4+c1;
c1.printme();
return 0;
}
Thanks for interest and sorry for late reply