?
Solved

complex Numbers using operator overloading

Posted on 2003-02-23
8
Medium Priority
?
198 Views
Last Modified: 2010-04-17
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
Comment
Question by:chellebaby50
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Expert Comment

by:BabuPrasanna
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
                   operator overloaded function
instead:
cout<< ComplexNum(10) + 20 // this will call your operator
                              overloaded function
0
 

Author Comment

by:chellebaby50
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

by:
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:chellebaby50
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

by:mlmcc
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

by:chellebaby50
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

by:CleanupPing
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

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

mlmcc
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

Make the most of your online learning experience.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

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

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

Join & Ask a Question