c++operator-overloadingcomplex-numbers

C++ operator overloading for complex number operations


I have an assignment in C++ and I'm having trouble getting started. The goal is to "design a class that uses the following overloaded operators for complex numbers: >> << + - * / "

My question isn't about the syntax of this, but more about the logic. I could use some help brain storming.

Input Sample:
2.5 -2.2
1.0 1.0

OutPut Sample:
A = (2.5) + (-2.2)i
B = (1.0) + (1.0)i

A + B = (3.5) + (-1.2)i
A - B = ..............
A * B = ..............
A / B = ..............

So how do I start this? The class "Complex" overloads these operators, so does that mean that I can only use these operators in the class (i.e. inside public functions)? If so would I want to do it this way? Or would I want to do it in my client/driver code?

Second, is it just adding i to the second value of each line? That seems too easy. Any direction would be much appreciated. (Just for the record, I'm not looking for anybody to do my homework for me... could just use some input)


Solution

  • It seems to me that the point is to demonstrate class operation overloading, so I think the idea is for you to make a class Complex which holds information about the real number and the imaginary number (the i means it's imaginary). Handle various operations between complex numbers in operator overrides you do yourself.

    Once you have that and you see that it works (make a static test method which does various operations and prints the results to the screen), then worry about using that class to work with input since parsing input will be another task in of itself. Sometimes it's just simpler to divide problems into smaller problems than to attempt to do both at the same time.