Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

Posted on 2003-02-23
Medium Priority
199 Views
I'm having a little trouble creating a complex number class using operator overloading.

I have a class called ComplexNum and I have the constructors for that class.  I know the formula's for using imaginary numbers but I don't know how to do the operator overloading for +,-,/,*.  My book has a fraction sample program but it doesn't help me to understand how to do the binary operators. Like for addition its (a,b) + (c,d) = (a+c, b+d) How do I do that?  Would one set be my real and one set my imaginary numbers. Any direction onto the right path would help.  I think I'm making this harder than it is but I'm really frustrated right now! Thanks
0
Question by:chellebaby50

Expert Comment

ID: 8007557
you can overload the operators by,having the following membter functions.

<return type> operator <operator symbol> ( parameter)

functions to be included in your class:
ComplexNum operator + (ComplexNum&);
ComplexNum operator / (ComplexNum&);
ComplexNum operator - (ComplexNum&);
ComplexNum operator * (ComplexNum&);

code sample:
////////
ComplexNum ComplexNum::operator + (ComplexNum& cnParam)
{
ComplexNum cnTemp;
cnTemp.real_value = this.real_value+cnParam.real_value;
cnTemp.im_value = this.im_value+cnParam.im_value;
return cnTemp;
}
main()
{
ComplexNum c1(10,10),c2(20,20);
ComplexNum c3=c1+c2;
}
////

But for any operator overloading, you need the make the compiler map your function. For that you have to specify the left most operand of any expression to be of your object type;

you cant use like,
cout << 10 + 20 // this will not get called for your
cout<< ComplexNum(10) + 20 // this will call your operator
0

Author Comment

ID: 8008902
The part I don't understand is how does the cnTemp.real_value = this.real_value+cnParam.real_value;
cnTemp.im_value = this.im_value+cnParam.im_value;

equal the same thing as the (a,b) + (c,d) = (a+c, b+d).  That is where I'm getting confused.  Also why did you have to call by reference.

Does this seem right.  ComplexNum ComplexNum::operator+(ComplexNum cnum2) const
{
double realAns;
double imagAns;
realAns = realNum + cnum2.realNum;
imagAns = imagNum + cnum2.imagNum;
ComplexNum complexAns (resultReal, resultImag);
return complexAns;
}
this is what one of my fellow students posted in my classroom but I don't understand this line of code ComplexNum complexAns (resultReal, resultImag);
Any help would be great!
0

LVL 101

Accepted Solution

mlmcc earned 200 total points
ID: 8010619
Actually better is

ComplexNum ComplexNum::operator + (const ComplexNum& cnParam) const
{
ComplexNum cnTemp;
cnTemp.real_value = this.real_value+cnParam.real_value;
cnTemp.im_value = this.im_value+cnParam.im_value;
return cnTemp;
}

Pass by reference passes less information and is faster than passing the entire object.

Do you understand this line
ComplexNum c1(10,10),c2(20,20);
It is the same as this except you have constants for the values.
ComplexNum complexAns (resultReal, resultImag);

cnTemp.real_value = this.real_value+cnParam.real_value;
a     +     c
cnTemp.im_value = this.im_value+cnParam.im_value;
b      +     d

You are declaring a new ComplexNum and giving it initial values of resultReal and resultImag.

mlmcc
0

Author Comment

ID: 8014065
Is it the same format to do the multiply and divide.  The formula for multiply is (a,b) * (c,d) = (a*c-b*d, a*d+b*c)

Would it be ComplexNum ComplexNum::operator*(Complex cnum2) const
{
double realResult= real*cnum2.real;
double imagResult= imag*cnum2.imag;
ComplexNum complexResult(realResult, imagResult)
return complexResult;
}

Does that seem right?
0

LVL 101

Expert Comment

ID: 8014173
Lets look at it

double realResult= real*cnum2.real;
a  *    c

double imagResult= imag*cnum2.imag;
b    *  d

Where is the -b*d  for the realpart

Where does b*d fit into the imaginary part?

Try again.
mlmcc
0

Author Comment

ID: 8014401
Would it be
double realResult= real*cnum2.real - imag*cnum2.imag;

a* c         -    b*d

See this is where I'm getting confused because I still don't understand how this cnTemp.real_value=this.real_value+cnParam.real_value;
a     +     c
is the same as (a,b) + (c,d) = (a+c, b+d). I pretty much did the same thing for the divide function as well.
ComplexNum ComplexNum::operator/(ComplexNum cnum2)const
{
double realResult= real/cnum2.real;
double imagResult= imag/cnum2.imag;
ComplexNum complexResult(realResult, imagResult)
return complexResult;
}
For some reason I don't understand why this is so hard for me to grasp.  Is there a way for you to be able to break it down for me.
0

Expert Comment

ID: 9447557
chellebaby50:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

LVL 101

Expert Comment

ID: 9447807
I provided a lot of help but am not sure if I solved or answered all the questions.

mlmcc
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Loops Section Overview
###### Suggested Courses
Course of the Month13 days, 7 hours left to enroll

#### 579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.