Trouble writing code for arithmetic calculations of complex numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 2
Reputation: scrappy is an unknown quantity at this point 
Solved Threads: 0
scrappy scrappy is offline Offline
Newbie Poster

Trouble writing code for arithmetic calculations of complex numbers

 
0
  #1
Nov 13th, 2004
I'm taking CMIS140, and I have a project using complex numbers. I can't figure out how to write the calculations into the program. I have attached the project, and the code I have written at this point. If anyone has any suggestions on how to do this, I would appreciate it.
Attached Files
File Type: cpp Project P3 Complex Numbers.cpp (3.1 KB, 35 views)
File Type: doc ComplexNumbers.doc (20.5 KB, 34 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Trouble writing code for arithmetic calculations of complex numbers

 
0
  #2
Nov 13th, 2004
paste your code inside code tags instead of attaching the sourcefiles. M
Makes it a lot easier for everyone to see what you're talking about.

And posting your assignment verbatim doesn't help either (but at least you're honest stating it's a school assignment ).

Now for a bit of help to get you started.
I've yesterday for fun and training (I've been more or less out of C++ for years, starting to get pretty rusty) created a class that does addition and subtraction of complex numbers.
The rest you can easily think up to do yourself, they work similarly.

I'll not post the full implementation (which is pretty trivial) nor the test application (which is even more trivial), but the headerfile giving all function prototypes.
That should give you enough hints to get started implementing the system yourself.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class complexNumber
  6. {
  7. private:
  8. float real; // real part
  9. float imag; // imaginary part
  10.  
  11. public:
  12. complexNumber();
  13. complexNumber(float rPart, float iPart);
  14. ~complexNumber(); // for form's sake, not really needed
  15.  
  16. complexNumber operator+(const complexNumber& c); // addition, so you can call 'c = a + b' where a,b, and c are complexNum objects
  17. complexNumber operator-(const complexNumber& c); // same for subtraction
  18. friend ostream& operator<<(ostream& s, const complexNumber& c); // so you can do for example 'cout << a' where a is a complexNumber object
  19. };

you can of course include far more functionality than this, but this should be a good start.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 2
Reputation: scrappy is an unknown quantity at this point 
Solved Threads: 0
scrappy scrappy is offline Offline
Newbie Poster

Re: Trouble writing code for arithmetic calculations of complex numbers

 
0
  #3
Nov 14th, 2004
Thank you for your help, and it did help. Now I keep getting the following error, and I don't know what it means.

error C2556: 'int __thiscall ComplexNumbers::AddComp(const class ComplexNumbers &,const class ComplexNumbers &)' : overloaded function differs only by return type from
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Trouble writing code for arithmetic calculations of complex numbers

 
0
  #4
Nov 14th, 2004
You probably made a typo somewhere in the return type of a function.
If you define 2 functions of the same name (overloaded functions) in the same namespace (or class) they are required to have different argument lists else the compiler and runtime don't know which function to call.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Trouble writing code for arithmetic calculations of complex numbers

 
0
  #5
Nov 14th, 2004
>they are required to have different argument lists
Or constness for member functions. The following introduces no ambiguity yet the return types and argument lists for the overloaded function are identical:
  1. #include <iostream>
  2.  
  3. using std::cout; using std::endl;
  4.  
  5. class test {
  6. public:
  7. void f() { cout<<"f()"<<endl; }
  8. void f() const { cout<<"f() const"<<endl; }
  9. };
  10.  
  11. int main()
  12. {
  13. test t;
  14. const test s;
  15.  
  16. t.f(); // Will print f()
  17. s.f(); // Will print f() const
  18. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 1
Reputation: lohith.s1985 is an unknown quantity at this point 
Solved Threads: 0
lohith.s1985 lohith.s1985 is offline Offline
Newbie Poster

Re: Trouble writing code for arithmetic calculations of complex numbers

 
0
  #6
Jun 19th, 2009
i am not able to run the program on complex numbers can any one help me out
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Trouble writing code for arithmetic calculations of complex numbers

 
0
  #7
Jun 19th, 2009
what code are you talking about?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC