Complex Number Calculator in C++

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2006
Posts: 6
Reputation: Roc is an unknown quantity at this point 
Solved Threads: 0
Roc Roc is offline Offline
Newbie Poster

Complex Number Calculator in C++

 
0
  #1
Apr 27th, 2006
Hi,everyone
I have to write a calculator which can + , - , * , / , + = ,- = , * = , /= ,++,-- do this operations with operator overloading
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class complexNumber
  6. {
  7. private:
  8. double realPart;
  9. double imaginaryPart;
  10. public:
  11. complexNumber(){realPart=0.0; imaginaryPart=0.0;}
  12. ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;}
  13.  
  14. double get_real(){return realPart;}
  15. double get_imaginary(){return imaginaryPart;}
  16.  
  17. void set_real(double r){realPart=r;}
  18. void set_imaginary(double i){imaginaryPart=i;}
  19.  
  20. complexNumber operator+(const complexNumber& c);
  21. complexNumber operator-(const complexNumber& c);
  22.  
  23. };
  24. void main()
  25. {
  26. complexNumber c1,c2,c3;
  27. c1.set_real(3.4);
  28. c1.set_imaginary(4.5);
  29. c2.set_real(5.6);
  30. c2.set_imaginary(6.2);
  31. c3= c1 + c2;
  32. cout<< c1 <<endl<< c2 <<endl<< c3 <<endl;
  33.  
  34. }
i wrote that code according to some topics in this forum but it gives me a error
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class complexNumber' (or there is no acceptable conversion)
How can i fix this?
Also i have to write a display function to print complex numbers according to my code i can not do it please also help me in this problem...

Have a good day...
P.S. i have just registered yet sorry if i did sth wrong...:rolleyes:
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Complex Number Calculator in C++

 
0
  #2
Apr 27th, 2006
Originally Posted by Roc
Hi,everyone
I have to write a calculator which can + , - , * , / , + = ,- = , * = , /= ,++,-- do this operations with operator overloading
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class complexNumber
  6. {
  7. private:
  8. double realPart;
  9. double imaginaryPart;
  10. public:
  11. complexNumber(){realPart=0.0; imaginaryPart=0.0;}
  12. ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;}
  13.  
  14. double get_real(){return realPart;}
  15. double get_imaginary(){return imaginaryPart;}
  16.  
  17. void set_real(double r){realPart=r;}
  18. void set_imaginary(double i){imaginaryPart=i;}
  19.  
  20. complexNumber operator+(const complexNumber& c);
  21. complexNumber operator-(const complexNumber& c);
  22.  
  23. };
  24. void main()
  25. {
  26. complexNumber c1,c2,c3;
  27. c1.set_real(3.4);
  28. c1.set_imaginary(4.5);
  29. c2.set_real(5.6);
  30. c2.set_imaginary(6.2);
  31. c3= c1 + c2;
  32. cout<< c1 <<endl<< c2 <<endl<< c3 <<endl;
  33.  
  34. }
i wrote that code according to some topics in this forum but it gives me a error
How can i fix this?
Also i have to write a display function to print complex numbers according to my code i can not do it please also help me in this problem...

Have a good day...
P.S. i have just registered yet sorry if i did sth wrong...:rolleyes:
You haven't defined the methods for operator overloading so what do you expect. Miracles?

The method for overloading the << operator is slightly different. You have to be careful when doing that.

Also this:

c1.set_real(3.4);

should be:

c1.set_real(3,4); <-- separated with a comma.

And exchange void main with int main. Carry on...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 6
Reputation: Roc is an unknown quantity at this point 
Solved Threads: 0
Roc Roc is offline Offline
Newbie Poster

Re: Complex Number Calculator in C++

 
0
  #3
Apr 27th, 2006
The method for overloading the << operator is slightly different. You have to be careful when doing that.
How can i do that?Please give me some code examples?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Complex Number Calculator in C++

 
0
  #4
Apr 27th, 2006
Well without actually handing out the code for a complex number class the following thread should make things clearer.

It's a fraction class but you should get an idea of what you need to do. It can probably simplified as well.

http://www.daniweb.com/techtalkforums/thread44082.html
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 6
Reputation: Roc is an unknown quantity at this point 
Solved Threads: 0
Roc Roc is offline Offline
Newbie Poster

Re: Complex Number Calculator in C++

 
0
  #5
Apr 28th, 2006
i have looked that topic but i couldn't get the main idea of overloading "<<" operator...
Can you give me a little code for cout complexNumbers from my code???
This code is working but can not print my complex numbers...
Can you edit this code's "<<" operator for me???or give me an idea please...
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class complexNumber
  6. {
  7. private:
  8. double realPart;
  9. double imaginaryPart;
  10. public:
  11. complexNumber(){realPart=0.0; imaginaryPart=0.0;}
  12. ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;}
  13.  
  14. double get_real(){return realPart;}
  15. double get_imaginary(){return imaginaryPart;}
  16.  
  17. void set_real(double r){realPart=r;}
  18. void set_imaginary(double i){imaginaryPart=i;}
  19.  
  20. friend ostream &operator<<( ostream &output, complexNumber &c )
  21. {
  22.  
  23. return output << c.realPart << " / " << c.imaginaryPart;
  24.  
  25. }
  26. complexNumber operator+(complexNumber c)
  27. {
  28. complexNumber temp;
  29. int temp_realPart,temp_imaginaryPart;
  30.  
  31. temp_realPart= realPart * c.get_imaginary() + c.get_real()* imaginaryPart;
  32. temp_imaginaryPart = imaginaryPart * c.get_imaginary();
  33.  
  34. temp.set_real(temp_realPart);
  35. temp.set_imaginary(temp_imaginaryPart);
  36.  
  37.  
  38. return temp;
  39. }
  40. // complexNumber operator-(const complexNumber& c);
  41.  
  42. };
  43. int main()
  44. {
  45. complexNumber c1,c2,c3;
  46. c1.set_real(3.4);
  47. c1.set_imaginary(4.5);
  48. c2.set_real(5.6);
  49. c2.set_imaginary(6.2);
  50. c3= c1 + c2;
  51. cout<<c3<<endl;
  52.  
  53. return 0;
  54. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Complex Number Calculator in C++

 
0
  #6
Apr 28th, 2006
You've more or less got it. I've just highlighted the stuff I've changed. Now you have to finish the methods for overloading the other operators.

Carry on...

#include<iostream>

using namespace std;

class complexNumber
{
private:
    double realPart;
    double imaginaryPart;
public:
    complexNumber(){realPart=0.0; imaginaryPart=0.0;}
    ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;}

    double get_real(){return realPart;}
    double get_imaginary(){return imaginaryPart;}

    void set_real(double r){realPart=r;}
    void set_imaginary(double i){imaginaryPart=i;}

    friend ostream &operator<<( ostream &output, complexNumber &c )
    {
   
       return output <<  c.realPart << " + " << c.imaginaryPart <<"i";
    
    }
    complexNumber operator+(complexNumber c)
    {
       complexNumber temp;
       double temp_realPart,temp_imaginaryPart;
    
       temp_realPart= realPart  + c.get_real();
       temp_imaginaryPart = imaginaryPart + c.get_imaginary();
    
       temp.set_real(temp_realPart);
       temp.set_imaginary(temp_imaginaryPart);
    
       return temp;
    }
//    complexNumber operator-(const complexNumber& c);
    
};
int main()
{
    complexNumber c1,c2,c3;
    c1.set_real(3.4);
    c1.set_imaginary(4.5);
    c2.set_real(5.6);
    c2.set_imaginary(6.2);
    c3= c1 + c2;
    cout<< c3<<endl;
    
    cin.get();
    
return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Complex Number Calculator in C++

 
0
  #7
Apr 28th, 2006
Just one last thing. Multiplication and division of complex numbers is non-intuitive. This is how I was told it should be done:-

http://img142.imageshack.us/img142/5735/c4um.png
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 6
Reputation: Roc is an unknown quantity at this point 
Solved Threads: 0
Roc Roc is offline Offline
Newbie Poster

Re: Complex Number Calculator in C++

 
0
  #8
Apr 29th, 2006
Thanx a lot..!I'm understanding my mistakes now,i just start to write operators if i'll get in trouble,maybe i'll ask sth also...

Thanx for your patience..!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Complex Number Calculator in C++

 
0
  #9
Apr 29th, 2006
I'm just learning this myself. He he.

I hope I not mis-leading you. :lol:
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 6
Reputation: Roc is an unknown quantity at this point 
Solved Threads: 0
Roc Roc is offline Offline
Newbie Poster

Re: Complex Number Calculator in C++

 
0
  #10
Apr 29th, 2006
I've finished writing Basic Complex Numbers Calculator thank you very much...!
have a nice day...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC