C ++ operator overload for complex number operations

I have a job in C ++, and I am having problems running. The goal is to "create a class that uses the following overloaded operators for complex numbers: β†’ <<+ - * /"

My question is not the syntax of this, but the logic. I could use some brainstorming help.

Input example:
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 get started? The Complex class overloads these operators, which means that I can only use these operators in a class (i.e., Inside public functions)? If I wanted to do it like this? Or do I want to do this in my client / driver code?

Secondly, is this just adding me to the second value of each row? It seems too easy. Any direction would be greatly appreciated. (Just for the record, I'm not looking for anyone who could do my homework for me ... could just use some kind of input)

+6
c ++ operator-overloading complex-numbers
source share
4 answers

It seems to me that the point is to demonstrate the overload of class operations, so I think the idea is for you to create a Complex class that contains information about the real number and imaginary number (this means that it is imaginary). You manage the various operations between complex numbers in operator overrides yourself.

After that, you will see that it works (create a static test method that performs various operations and displays the results on the screen), and then worry about using this class to work with input, since parsing the input will be another task in itself. Sometimes it’s just easier to divide problems into smaller problems than trying to do both at the same time.

Hope this helps. Good luck

+7
source share

They love pairs of meanings:

A = N1 + I1i B = N2 + I2i A + B = (N1 + I1i) + (N2 + I2i) = N1 + I1i + N2 + I2i = (N1 + N2) + (I1i + I2i) = (N1 + N2) + (I1 + I2)i A - B = (N1 + I1i) - (N2 + I2i) = N1 + I1i - N2 - I2i = (N1 - N2) + (I1i - I2i) = (N1 - N2) + (I1 - I2)i // N1, N2, I1, I2 are all just normal numbers. // You can multiply them like normal. You just have to keep track of the `i` // Also not that i = sqrt(-1) // Therefore i * i = sqrt(-1) * sqrt(-1) // = sqrt(-1)^2 // = -1 A * B = (N1 + I1i) * (N2 + I2i) = (N1 * N2) + (N1 * I2i) + (I1i * N2) + (I1i * I2i) = (N1 * N2) + (N1 * I2)i + (N2 * I1)i + (i * i * I1 * I2) = (N1 * N2) + i((N1 * I2) + (N2 * I1)) + (-1 * I1 * I2) // Simplest form = ((N1 * N2) - (I1 * I2)) + ((N1 * I2) + (N2 * I1))i A / B = Repeat as above. 
+2
source share

You need to create a class called Complex, which includes at least:

  • a constructor that allows you to build a Complex object from real and imaginary values ​​of components, for example. Complex (1, 5)

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

  • similar for other operators

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

+2
source share

To complete this task, you need to perform several actions:

Define a class (for example, Complex) that can store data for the real and imaginary parts of the complex number.

Overload the appropriate operators (for example):

 class Complex { public: // other declarations here Complex operator+ (const Complex& rhs) const; // other stuff here }; 

Introduce the appropriate operators to perform a mathematical operation (for example.):

 Complex Complex::operator+ (const Complex& rhs) const { Complex result = *this; result.Real += rhs.Real; result.Imaginary += rhs.Imaginary; return result; } 
+1
source share

All Articles