Topical Information

This lab will help you practice with classes and libraries.

Program Information

Write a class for a complex number ADT. To refresh your memory, complex numbers have the form: a + bi. Where a is the real part, b is the imaginary part, and i represents the square root of -1 (which doesn't exist and is therefore imaginary).

Standard mathematical operations are defined on complex numbers:

    a+bi + c+di = (a+c) + (b+d)i

    a+bi - c+di = (a-c) + (b-d)i

    a+bi * c+di = (a*c-b*d) + (a*d+b*c)i    // i*i == -1

                  (a*c+b*d) - (a*d-b*c)i
    a+bi / c+di = ----------------------
                        c*c + d*d

        -(a+bi) = (-a) + (-b)i

That last one is negation (aka opposite), of course.

And special operations are also defined:

              ___________
   |a+bi| = \/ a*a + b*b                    // magnitude

    ____
    a+bi  = a-bi                            // conjugate

(For the curious:

         2         ____
   |a+bi| = a+bi * a+bi

                    ____
    a+bi     a+bi * c+di
   ------ = -------------
    c+di             2
               |c+di|

If you really wanted to know...)

Define these operations (along with constructors, input/output, and accessors/mutators) for your ADT/class. Place your ADT in a library.

Write a driver to test the ADT behaviors thoroughly.

Thought Provoking Questions

  1. Why do your class methods take fewer arguments than you would expect?

  2. Does the compiler change y when you have 'x + y' in your program? So should your addition method change the other Complex number (the argument object)? How can you tell the compiler this in the most efficient way?

    Does this phenomenon extend to the other operations?

  3. What kind of value should be returned from the standard math operations (i.e. what TYPE of value)? From conjugate? From magnitude?

  4. Does your input method prompt the user? Why should it not?

  5. Does your output method print anything besides the Complex number (even an endl)? Why should it not?

This assignment is (Level 3).

Options


Total Level Possible

If you did all above options, this lab could be worth as much as (Level 11).