Dealing with Complex Number in C++

Rizesky Siallagan
4 min readNov 19, 2018

--

The Definition of Complex Number for me is a number that consist of real number and imaginary number in format a+ib, where squre of i=-1 and a,b is real number.

At my first met with Complex Number, i always think that this number is useless to learn, i will never use complex number in life.

Now, i found this kind of number useful in electrical engineering, to solve electromagnetic waves problem.

Someone has ask me to make simple CLI program in C or C++ to make him easier to operate complex number, including input and output of complex number.

At first, i don’t believe that i can finish that task. But, look at what i found, C++ enables user to do overloading on in-built operators, including stream operators. What a beautiful features of C++.

C++ let us do this in two ways. It is as Member Function and non-Member Function(using friend).

First, i represent the a+ib format to (a,b) where a is real part, and b is imaginary part of complex number to make it easier.

Let break it down to C++.

Make a complex number data type

complex number data type

bilKompleks class stands for complex number data type.

There i make basic of complex number data type,consist of:

  • A constructor(with default realpart = 0, and imaginary part=0)
  • A function to set a complex number value(void setComplex)
  • A function to retrieve a complex number value(void getComplex)
  • Complex number attribute(realPart,imaginaryPart)

Make the prototype of operators that we want to overload

Member function operator overload

I make the protoype of +, * operator that i want to overload. Because i want to define a new +,* operation that work like :

(a+ib) + (x + iy) = (a+x) + i(b+y)

(a+ib) * (x+iy) = (ax-by) + i(ay + bx)

So, when i type num1+num2 or num*num2 of complex number, the operation will follow rules above for complex number class instead of default + and * operation.

Next, the prototype of non member function that we want to overload

I need to overload the input and output stream operator so if input and output meet with complex number data type it will work like my rules instead of default rules of them

non-member function operator overload. you can save it as bilKompleks.h

As you see i add friend there. friend is a reserved word in C++ that means it’s not a member of that class/struct but has access to modify attribute in that class.

Define the all of the functions

First let’s define the input and output stream for our complex number data type. I want to make the input in format of (a,b) and output also as (a,b).

input and output overloading definition
class function definition and member function overloading definition

Your definition file should look like this below

you can save it as bilKompleks.cpp

Make a drive file to try it out

I want to:

— Make 2 complex number (test my data type)

— Printing out the complex number (test ovarloaded cout)

— Changing a value of complex number(test overloaded cin)

— Create an addition operation between them(test overloaded + operator)

— Create an multiplication operation between them(test overloaded * operator)

— Checking if both of them has same value(test overload == operator)

So my main file should look like below:

you can save it as main.cpp

Then, compile it g++ main.cpp bilKompleks.cpp -o apps

The result should look like above, then try to input the change of Num 2

after inserting change for num2

You can see that operation with complex number is easier now. Data structure and the power of operator overloading of C++ is really awesome for me, as i don’t know how to do that with other programming language, lol.

Anyway, thanks for your time reading my article.

--

--