Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 660
Steve's a D Avatar asked Dec 03 '10 17:12

Steve's a D


2 Answers

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.

Hope that helps. Good luck!

like image 195
Neil Avatar answered Nov 12 '22 14:11

Neil


You need to design a class called Complex that includes at least:

  • a constructor allowing you to construct a Complex object from real and imaginary component values e.g. Complex(1, 5)

  • overrides the + operator so that you can add two Complex objects, returning a new Complex object e.g. Complex(1, 5) + Complex(3, 7) is Complex(4, 12)

  • similarly for other operators

But first you need to understand the basic math behind complex numbers so that you can write the operator overload methods.

like image 21
jarmod Avatar answered Nov 12 '22 12:11

jarmod