944,191 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6070
  • C++ RSS
Nov 13th, 2004
0

Trouble writing code for arithmetic calculations of complex numbers

Expand Post »
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, 130 views)
File Type: doc ComplexNumbers.doc (20.5 KB, 140 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scrappy is offline Offline
2 posts
since Nov 2004
Nov 13th, 2004
0

Re: Trouble writing code for arithmetic calculations of complex numbers

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.

C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 14th, 2004
0

Re: Trouble writing code for arithmetic calculations of complex numbers

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scrappy is offline Offline
2 posts
since Nov 2004
Nov 14th, 2004
0

Re: Trouble writing code for arithmetic calculations of complex numbers

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 14th, 2004
0

Re: Trouble writing code for arithmetic calculations of complex numbers

>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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 19th, 2009
0

Re: Trouble writing code for arithmetic calculations of complex numbers

i am not able to run the program on complex numbers can any one help me out
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lohith.s1985 is offline Offline
1 posts
since Jun 2009
Jun 19th, 2009
0

Re: Trouble writing code for arithmetic calculations of complex numbers

what code are you talking about?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

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: Editing a file
Next Thread in C++ Forum Timeline: bit of help with please :)





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


Follow us on Twitter


© 2011 DaniWeb® LLC