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

Please help!! This assignment is very similar to previously answered C++ program

ID: 3789775 • Letter: P

Question

Please help!! This assignment is very similar to previously answered C++ programming questions on writing a Complex Number Program, however, there are a few key differences! Would also really appreciate extra help with the CATCH unit tests. Thank you (:

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 withstd::complex.

We define i, the imaginary unit, as follows:

A complex number has the form a+bi, where a and b are real numbers. In this form a is referred to as the “real part” and b as the “imaginary part”. Adding two complex numbers is straightforward:

as is multiplying them:

(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* & is given by:

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

The norm is just the square of the magnitude so:

When dealing with fractions involving complex numbers, try multiplying the numerator & denominator by the complex conjugate of the denominator. This will make the denominator real & 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 & complex.cpp). In addition create C++ source files named complex_tests.cpp & complex_demo.cpp.

3 - Instance Variables

A complex number needs to know its real part & 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 ( "default_should_be_zero", " [complex] ") { complex c; REQUIRE ( c.get_real () == 0); REQUIRE ( c.get_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 & 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.get_imaginary () == Approx ( 10.0 ) . epsilon ( 0.001 ) ); 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 & then test the contents of that stream. Here’s a simple example:

/ / Note : Be sure to #include <sstream> at the top of your test file & to add using namespace std; TESTCASE ( " << _should_produce_only_0_when_wreiting_0+0i " , " [ complex ] " ) { stringstream ss; complexs 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 2nd 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.

?--1

Explanation / Answer


include <iostream> using namespace std; class Complex { private: float real; float imag; public: Complex(): real(0), imag(0){ } void input() { cout << "Enter real and imaginary parts respectively: "; cin >> real; cin >> imag; } // Operator overloading Complex operator - (Complex c2) { Complex temp; temp.real = real - c2.real; temp.imag = imag - c2.imag; return temp; } void output() { if(imag < 0) cout << "Output Complex number: "<< real << imag << "i"; else cout << "Output Complex number: " << real << "+" << imag << "i"; } }; int main() { Complex c1, c2, result; cout<<"Enter first complex number: "; c1.input(); cout<<"Enter second complex number: "; c2.input(); // In case of operator overloading of binary operators in C++ programming, // the object on right hand side of operator is always assumed as argument by compiler. result = c1 - c2; result.output();