943,994 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 6854
  • C++ RSS
Apr 27th, 2006
0

Complex Number Calculator in C++

Expand Post »
Hi,everyone
I have to write a calculator which can + , - , * , / , + = ,- = , * = , /= ,++,-- do this operations with operator overloading
C++ Syntax (Toggle Plain Text)
  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
Quote ...
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:
Similar Threads
Roc
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Roc is offline Offline
6 posts
since Apr 2006
Apr 27th, 2006
0

Re: Complex Number Calculator in C++

Quote originally posted by Roc ...
Hi,everyone
I have to write a calculator which can + , - , * , / , + = ,- = , * = , /= ,++,-- do this operations with operator overloading
C++ Syntax (Toggle Plain Text)
  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...
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 27th, 2006
0

Re: Complex Number Calculator in C++

Quote ...
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?
Roc
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Roc is offline Offline
6 posts
since Apr 2006
Apr 27th, 2006
0

Re: Complex Number Calculator in C++

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
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 28th, 2006
0

Re: Complex Number Calculator in C++

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...
C++ Syntax (Toggle Plain Text)
  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. }
Roc
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Roc is offline Offline
6 posts
since Apr 2006
Apr 28th, 2006
0

Re: Complex Number Calculator in C++

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;
}
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 28th, 2006
0

Re: Complex Number Calculator in C++

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
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 29th, 2006
0

Re: Complex Number Calculator in C++

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..!
Roc
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Roc is offline Offline
6 posts
since Apr 2006
Apr 29th, 2006
0

Re: Complex Number Calculator in C++

I'm just learning this myself. He he.

I hope I not mis-leading you. :lol:
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 29th, 2006
0

Re: Complex Number Calculator in C++

I've finished writing Basic Complex Numbers Calculator thank you very much...!
have a nice day...
Roc
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Roc is offline Offline
6 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please Help!
Next Thread in C++ Forum Timeline: help w/ dictionary look-up program by using Associative_Array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC