Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 Introduction C++ includes a complex number type in the standard library but we

ID: 3796465 • Letter: 1

Question

1 Introduction

C++ includes a complex number type in the standard library but we’re going to write our own here. You’ll have to be

careful with namespaces so that your complex class doesn’t get confused with std::complex.

We define i, the imaginary unit, as follows

i^2 = 1.

A complex number has the form a + bi, where a and b are real numbers. In this form a is refered to as the “real part”

and b as the “imaginary part”. Adding two complex numbers is straightforward:

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

as is multiplying them:

(a + bi)(c + di) = ac bd + (ad + cb)i

(note that the second term comes from multiplying bi by di and using the definition of i).

The complex conjugate of a complex number z is written z*

and is given by:

(a + bi)* = a bi.

The magnitude of a complex number z is written |z| and given by:

The norm is just the square of the magnitude so:

norm(z) = |z|^2

When dealing with fractions involving complex numbers, try multiplying the numerator and denominator by the

complex conjugate of the denominator. This will make the denominator real and make it easier to manipulate these

fractions. This is required in the reciprocal method below.

2 Files

In your IDE create the complex class (which should create complex.h and complex.cpp). In addition create

C++ source files named complex tests.cpp and complex demo.cpp.

3 Instance variables

A complex number needs to know its real part and its imaginary part. We will use doubles for each of these. Create two

double variables in the private section of your class. These are your only instance variables, don’t add others!.

4 Starting Your Tests

Fill in complex tests.cpp with our template for CATCH unit tests. Create a test case that ensures that you can

create a complex number supplying no arguments to the constructor:

TEST CASE ( ” d e f a u l t should be z e r o ” , ” [ complex ] ” ) {

complex c ;

REQUIRE ( c . g e t _r e a l ( ) == 0 ) ;

REQUIRE ( c . g e t_ imaginary ( ) == 0 ) ;

}

To get this test to pass, you need to implement the empty constructor and two “getter” methods. Once you have this

test passing, write the next one before implementing more of the complex class. That is, write the test before you

write the code to make it pass. Continue this process for each method in the class (write the tests first then write the

method) so that you have at least one and often several tests for each method.

When testing equality between two doubles, you will often find that they don’t match exactly. CATCH has support for

approximate comparisons. For example:

REQUIRE ( c . g e t imaginary ( ) == Approx ( 1 0 . 0 ) . epsilon ( 0 . 0 0 1 ) ) ;

checks that the imaginary part is equal to 10 plus or minus 0.001.

When testing your stream insertion operator you will need to “write” your complex number out to a stringstream and

then test the contents of that stream. Here’s a simple example:

/ / note : be sure to # include <s stream> at the top of your test file

/ / and to add ” using namespace s t d ” if you haven’t already

TEST CASE ( ”<< should produce only 0 when writing 0+0 i ” , ” [ complex ] ” ) {

stringstream ss ;

complex c ( 0 . 0 , 0 . 0 ) ;

ss << c ;

REQUIRE ( ss . str ( ) == ” 0 ” ) ;

}

5 Constructors

Create the following constructors:

• complex() – creates the complex number zero

• complex(double real, double imag=0) – creates the complex number real + imag i. If the second

argument is not specified, it is assumed to be zero

6 Simple methods

Create the following methods:

• double get real() const – return the real part

• double get imaginary() const – return the imaginary part (note: should return a double so, for example,

if I ask for the imaginary part of 3 + 5i I would get 5).

• double magnitude() const – return the magnitude of this number

• double norm() const – return the norm of this number

• complex conjugate() const – return the complex conjugate of this number (note: return type should

be complex)

• complex negate() const – return the negative of this complex number

• complex reciprocal() const – return one over this complex number

7 Overloaded operators

Overload the following operators:

• operator << – if both the real and imaginary part are non-zero, write the complex number out as real part

+/- imag part i where the sign is chosen based on the sign of the imaginary part. If the imaginary part

is zero print real part, if the real part is zero and the imaginary part is not, print imag part i. Here are

some examples: 3, 3+2i, 3-8i, 0, 5i.

• operator == – return true if two complex numbers are equal, false otherwise

• operator != – return true if two complex numbers are not equal, false otherwise

• operator + – add complex numbers

• operator - – subtract...

• operator * – multiply...

• operator / – divide...

8 Demo

Write an interactive demo that allows the user to enter to complex numbers and select an operation (+, -, *, /).

Print the result of the user selected operation on the two complex numbers they entered.

Explanation / Answer

complex.cpp

==================================================================================

complex.h

=======================================================================

complex_test.cpp

==============================================================