.. _elab2: Exam2Lab: Simple Complex Numbers ################################ We are going to implement a class that manages a form of ``complex numbers``. These numbers are made up of two numerical parts, called `real` and `imaginary`. Traditionally, they are written like this: 1.0 + 2.0i Objects constructed from this class can be combined to form new objects using simple math operations. Yikes, I hear you say, we do not know how to set up methods that let us create new math operations. Well, this is pretty easy. Here is the skeleton of the class you need to get started which includes the method needed to do addition. The `operator` `+` method is how we `overload` the plus symbol so it knows how to `add` two `complex` data items. .. literalinclude:: code/Complex.h :language: c :linenos: The implementation for this skeleton is here: .. literalinclude:: code/Complex.cpp :language: c :linenos: .. note:: The `friend` operator << function is special,, and we will talk about it in class. It lets is print out our complex nembers using the normal cout methods. And here is a simple test program that runs this code: .. literalinclude:: code/main.cpp :language: c :linenos: Your job ******** Here is what we need to do for this lab part: First, make sure the given code works! Set this project up in your Homework repository on GitHub, and commit it before you go any further! Once that is working, extend this class so it supports the following math operations: .. code-block:: text add: (a+b*i) + (c+d*i) = (a+c) + (b+d)*i sub: (a+b*i) - (c+d*i) = (a-c) + (b-d)*i mul: (a+b*i)*(c+d*i) = a*c + a*d*i + b*c*i + b*d*i*i = (a*c-b*d) + (a*d+b*c)*i mul: B * (a+b*i) = (a*B + b*B*i) div: (a+b*i)/(c+d*i) = (a*c + b*d)/(c*c + d*d) + (b*c-a*d)/(c*c+d*d)*i div: (a+b*i)/B = (a/B + b/B*i) .. note:: In the above formulas, ``B`` is a simple number, not a complex one. You should follow the notation shown in the example for addition in the given code. This will not be too bad if you add things slowly, and test things as you go.