Hide

Problem A
Complex numbers

For this lab you will create a class that represents a complex number. In essence, you will create a class that can handle addition, subtraction, multiplication and division. Do investigate what constructors are called and how or if they should be implemented.

How complex numbers work can be found out by reading the wikipedia page. You may refresh conjugate algebra when implementing division. You can also reference how the standard library implementation works. You will need to use at least c++11 (-std=c++11) for this assignment. From now on, do use at least c++11 for the upcoming assignments.

You shall upload two files named Complex.cpp and Complex.h containing the implementation of your solution.

Functions

In the style of cppreference the following things should be defined:

Member functions

-

(constructor)

constructs a complex number (see guidance below)

operator=

assigns the contents

real

accesses the real part of the complex number

imag

accesses the imaginary part of the complex number

operator+= -= /= *=

compound assignment of two complex numbers or a complex and a scalar

Non-member functions

-

operator+ -

applies unary operators to complex numbers

operator+ - * /

performs complex number arithmetics on two complex values or a complex and a scalar

operator== !=

compares two complex numbers or a complex and a scalar

real

returns the real component

imag

returns the imaginary component

abs

returns the magnitude of a complex number

operator<

returns true if the magnitude of the left complex number is lesser than the right complex number, and false otherwise

operator<<

Writes to output stream the complex number in the form textit(real,imaginary)

operator>>

Reads a complex number from is. The supported formats are real, (real) and (real,imaginary) Where the input must be convertible to Double

operator""_i

Forms a complex literal representing an imaginary number (note the underscore)

Guidance

The type that you use to save the values should be doubles.

These are the constructor signatures that you will need:

  • Complex();

  • Complex(double real); // Note: make this constructor non explicit to allow expressions such as z = 3;

  • Complex(double real, double imaginary);

  • Complex(const Complex &rhs);

This thing enables you to write complex numbers in literal form.

  • constexpr Complex operator""_i(long double arg);

Any constexpr is resolved in compile time. That means that any constructor called by operator""_i must also be resolved in compile time. To ensure that, use member initializer list, whose syntax is the colon character :, followed by the comma-separated list of one or more member-initializers and let the body of the constructor be empty.

Make your own design decisions

operator+= -= /= *= =

You need to think about what the return type of operator= and operator+= -= /= *= should be. The design decision is up to you. Given Complex x; Complex y; should all of these work or only a few? Make your own design decisions and motivate them.

  • Complex x; Complex y;

  • x = y + 1;

  • x = y + y + 1 + 5; // chaining

  • x = 2 + y;

  • x += y += 4; // chaining

  • x = y = 0; // chaining

Basic test examples

Examples of things that should work. Note you must test more extensively on your own.

  • Complex x;

  • Complex x2 = 5;

  • Complex y(6, 2);

  • Complex z = x + y;

  • Complex k = 3 + 5_i;

  • k -= 5 + 1_i * Complex(5, 3);

  • std::cout << Complex(6, 6) / 6 << std::endl;

Please log in to submit a solution to this problem

Log in