Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Latest commit

 

History

History
179 lines (168 loc) · 6.31 KB

Complex.md

File metadata and controls

179 lines (168 loc) · 6.31 KB

1. Exercise: Complex

Complex models a complex number of the form z = a + ib where:

  • a is a real number.
  • b is a real number.

Implement Complex according to the following specification:

Method Description Usage Exceptions
Complex() Constructs a complex number with no real and no imaginary component.
Complex z; // z = 0 + 0i
None
Complex(double const& real) Constructs a complex number with a real component but no imaginary component.
Complex z(42); // z = 42 + 0i
None
Complex(double const& real, double const& imaginary) Constructs a complex number with a real and imaginary component.
Complex z(42, 75); // z = 42 + 75i
None
~Complex() Destructor.
None
Complex conjugate() Returns the conjugate of *this.
Complex z1(42, 75); // z1 = 42 + 75i
Complex z2{z1.conjugate()}; // z2 = 42 - 75i
None
double modulus() Returns the modulus of *this
Complex z(3, 4); // z = 3 + 4i
double mod{z.modulus()}; // mod = 5
None
double argument() Returns the argument of *this.
Complex z(3, 4); // z = 3 + 4i
double arg{z.argument()}; // arg = 0.9273
None

In addition to the above specification, you must:

  • Implement const-correctness for methods and member variables.
  • Mark the default constructor as default or delete.
  • Mark the destructor as default or delete.
  • Use initialiser lists where required.
  • Use delegating constructors where possible.
  • Have correct access specifiers.

2. Exercise: Complex (Continued)

Take the Complex class from lab02-1 and extend it with the following specification:

Method Description Usage Exceptions
Complex(Complex const& z) Copy constructor.
Complex z1;
Complex z2(z1);
None
Complex(Complex&& z) Move constructor.
Complex z1;
Complex z2(std::move(z1));
None
Complex& operator=(Complex const& z) Copy assignment.
Complex z1;
Complex z2;
z2 = z1;
None
Complex& operator=(Complex&& z) Move assignment.
Complex z1;
Complex z2;
z2 = std::move(z1);
None
static Complex make_conjugate(Complex const& z) Returns the conjugate of z. Equivalent to z.conjugate().
Complex z1(42, 75); // z1 = 42 + 75i
Complex z2{Complex::make_conjugate(z1)}; // z2 = 42 - 75i
None
friend Complex operator+(Complex const& lhs, Complex const& rhs) Returns the addition of two complex numbers.
Complex z1(3, 4); // z1 = 3 + 4i
Complex z2(4, 5); // z2 = 4 + 5i
Complex z3{z1 + z2}; // z3 = 7 + 9i
None
friend Complex operator-(Complex const& lhs, Complex const& rhs) Returns the subtraction of two complex numbers.
Complex z1(3, 4); // z1 = 3 + 4i
Complex z2(4, 5); // z2 = 4 + 5i
Complex z3{z1 - z2}; // z3 = -1 + -1i
None
friend Complex operator*(Complex const& lhs, Complex const& rhs) Returns the multiplication of two complex numbers.
Complex z1(3, 4); // z1 = 3 + 4i
Complex z2(4, 5); // z2 = 4 + 5i
Complex z3{z1 * z2}; // z3 = -8 + 31i
None
friend bool operator==(Complex const& lhs, Complex const& rhs) Returns true if real components are equal and imaginary components equal.
Complex z1(3, 4); // z1 = 3 + 4i
Complex z2(4, 5); // z2 = 4 + 5i
Complex z3(4, 5); // z3 = 4 + 5i
bool t1{z1 == z2}; // false
bool t2{z2 == z3}; // true
None
friend bool operator!=(Complex const& lhs, Complex const& rhs) Returns false if real components are equal and imaginary components equal.
Complex z1(3, 4); // z1 = 3 + 4i
Complex z2(4, 5); // z2 = 4 + 5i
Complex z3(4, 5); // z3 = 4 + 5i
bool t1{z1 != z2}; // true
bool t2{z2 != z3}; // false
None
friend std::ostream& operator<<(std::ostream& os, Complex const& z) Writes the complex number to the output in the format a+bi where a and b are the real and imaginary component respectively.
Complex z(3, 4); // z = 3 + 4i
std::cout << z; // 3+4i
None

In addition to the above specification, you must:

  • Implement const-correctness for methods and member variables.
  • Mark appropriate constructors, assignments, and destructor as default or delete.
  • Have correct access specifiers.
  • Write more tests for the new methods.