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

Pls visit to this link for the code http://courses.muscedere.com/8821101/Lab51.t

ID: 3606567 • Letter: P

Question

Pls visit to this link for the code http://courses.muscedere.com/8821101/Lab51.txt. If you answer this question pls help me with the next one I post here too. I will give a good rating for everything. If you can view my profile pls check for the next question I post.

OBJECTIVE To become familiar with functions, dynamic memory, and structures. SECTIONS: Write the newly added code for the "operator function below; it should be one line like the rest. 1. 2. 3. Operator Overload in the Complex Class Operator Overload in the Matrix Class A Combined Compkex Matrix Class (9 marks) (11 marks) (10 marks) Write the newly added code for the function below Carry out the coding experiment and submit your results on this paper at next week's lab. Before submitting your labs remember to keep a copy of your code as we will be using it in future labs. 1· OPERATOR OVERLOAD IN THE COMPLEX CLASS Write the newly added code for the "operatorfunction below In class we covered a basic Complex Number Class for which we originally added our own "add funetions which we had to call explicitly. Now we would like to change this by taking advantage of"operator overloading" in Ctt, By doing this we will be able to add, subtract, multiply, and divide complex numbers by using the standard C operators. Open http:courses muscedere.com 88210Lab5ltxt and copy the contents into a new project. . If you are using Visual C++ make sure you add it after the #include "stdafx.h” This code is slightly different than the one covered in the lecture. It has been modified to work better with the remaining sections of this lab, different compilers, as well as being more condensed. The code also includes a number of guidelines to complete a full sct of opcrator overloads for both numerical calculations and comparison operations. Please note that although the code will compile it will not generate proper output until you have completed all the necessary changes. Write the newly added code for the "operator-" function below; do not include what is already provided write the newly added code for the "operator ” function below Write the newly added code for the "operator! function below Write the newly added code for the "operatfunction below The following describes the product of two complex numbers write he newly added code for the "operaor+" / "operator.=" function below. they should be the same. Once all thesc changes have been made correctly, the output of the compiled code should be: a-0 b-1+2j c-3+4j (a b(c +dj) - (ac- bd) +(bc +ad)j b-c -2-2j a/d-1.91803+0.098360 a"c-21+72j d is > cin magnitude b+ d or b-b+d The following describes the quotient of two complex numbers: c+ dy Write the newly added code for the "operator"/"opcraton "function below; they should be the same. Note the variable "denom" can be set as the common denominator to simplify the cakulations. a=6+81 and b are equal in magnitude c/-d or c=c/ d b-28+961 c-0.6393440.0327869

Explanation / Answer

#include <iostream>

#include <iomanip>

#include "math.h"

using namespace std;

class Complex

{

private:

double re, im;

public:

Complex(double real, double imag) { setRI(real, imag); }; //Constructor

Complex(double real = 0) { setRI(real, 0); }; // Default Constructor

~Complex() {}; //Destructor

void setRI(double real = 0, double imag = 0) { setReal(real); setImag(imag); }

double getReal() { return re; }

double getImag() { return im; }

void setReal(double real = 0) { re = real; }

void setImag(double imag = 0) { im = imag; }

double mag() { return sqrt(re * re + im * im); };

Complex operator+(const Complex &rhs);

Complex& operator+=(const Complex &rhs);

Complex operator-(const Complex &rhs);

Complex& operator-=(const Complex &rhs);

Complex operator*(const Complex &rhs);

Complex& operator*=(const Complex &rhs);

Complex operator/(const Complex &rhs);

Complex& operator/=(const Complex &rhs);

double operator~(void); // Unary operation, no arguments

bool operator<(Complex &rhs); // All of these compare magnitude only

bool operator>(Complex &rhs);

bool operator<=(Complex &rhs);

bool operator>=(Complex &rhs);

bool operator==(Complex &rhs);

bool operator!=(Complex &rhs);

};

ostream& operator<<(ostream &os, Complex a) {

os << noshowpos << a.getReal();

if (a.getImag()) os << showpos << a.getImag() << "j" << noshowpos;

return os;

}

Complex Complex::operator+(const Complex &rhs) {

Complex t;

t.re = re + rhs.re;

t.im = im + rhs.im;

return t;

}

Complex& Complex::operator+=(const Complex &rhs) {

re = re + rhs.re;

im = im + rhs.im;

return *this;

}

Complex Complex::operator-(const Complex &rhs) {

Complex t; //creating temp variable of complex to return the left side complex object operator'-' is used between 2 complex object

// Add your code here

t.re=re-rhs.re; // Subtracts the real values of 2 complex objects and store t.re --->temporary complex object's real value

t.im=im-rhs.im; // Subtracts the imaginary values of 2 complex objects and store t.re --->temporary complex object's imaginary value

return t;// This will return complex object t which is the difference of values of 2 complex objects

}

Complex& Complex::operator-=(const Complex &rhs) {

// Add your code here

re = re - rhs.re; //difference of the real value of invoking object and right side of the object is taken and stored again in real value of invoking object

im = im - rhs.im; //difference of the imaginary value of invoking object and right side of the object is taken and stored again in imaginary value of invoking object

return *this; //returns the reference of the invoking object with updated values

}

Complex Complex::operator*(const Complex &rhs) {

Complex t;

// Add your code here

t.re=re * rhs.re;

t.im=im*rhs.im;

return t;

}

Complex& Complex::operator*=(const Complex &rhs) {

Complex t;

// Add your code here; should be the same as the one above

t.re=re*rhs.re; // product of invoking object's real value and rightside of operator *='s object of the real value and stores in temp real value

t.im=im*rhs.im;// product of invoking object's imagunary value and rightside of operator *='s object of the imaginary value and stores in temp imaginary value

*this = t; //stores the temporary value in the invoking object

return *this;

}

Complex Complex::operator/(const Complex &rhs) {

Complex t;

double denom;

t.re=re/rhs.re; //divides the invoking oobject's real value and rightside of operator 'object'real value and stores in t.re i.e temp real value

t.im=im/rhs.im; //same as abov but in case it deals with imaginary value

// Add your code here

return t; //returns temporary object which consists of above caluclated value

}

Complex& Complex::operator/=(const Complex &rhs) {

Complex t;

double denom;

// Add your code here; should be the same as the one above

t.re=re/rhs.re; // same as above described in above operator

t.im=im/rhs.im; //// same as above described in above operator

*this = t; // assign temp object t to invoking object

return *this;//returns invoking object

}

double Complex::operator~(void) {

return mag();

}

bool Complex::operator<(Complex &rhs) {

return mag() < ~rhs;

}

bool Complex::operator>(Complex &rhs) {

// Add your code here

if(mag()> ~rhs) //compares whether invoking object magnitude is greater than righthandside object's magnitude

{

return true;

}

else

return false;

}

bool Complex::operator<=(Complex &rhs) {

// Add your code here

if(mag()<=~rhs)//compares whether invoking object magnitude is less than or equal torighthandside object's magnitude

{

return true;

}

else

return false;

}

bool Complex::operator>=(Complex &rhs) {

// Add your code here

if(mag()>=~rhs) //compares whether invoking object magnitude is greater than or equal to righthandside object's magnitude

{

return true;

}

else

return false;

}

bool Complex::operator==(Complex &rhs) {

// Add your code here

if(mag()==~rhs) //compares whether invoking object magnitude is equal to righthandside object's magnitude

{

return true;

}

else

return false;

}

bool Complex::operator!=(Complex &rhs) {

// Add your code here

if(mag()!=~rhs) ////compares whether invoking object magnitude is not equal to righthandside object's magnitude

{

return true;

}

else

return false;

}

int main(void) {

Complex a; // "a" is set to 0+0j - constructor

Complex b(1, 2); // "b" is set to 1+2j - constructor

Complex c(3, 4); // "c" is set to 3+4j - constructor

Complex d(5, 6); // "d" is set to 5+6j - constructor

cout << "a=" << a << endl;

cout << "b=" << b << endl;

cout << "c=" << c << endl;

cout << "d=" << d << endl;

a = b + c + d;

cout << "a=b+c+d=" << a << endl;

cout << "b-c=" << b - c << endl;

cout << "a/d=" << a / d << endl;

cout << "a*c=" << a * c << endl;

if (d > c) cout << "d is > c in magnitude" << endl;

b += d; cout << "b+=d or b=b+d" << endl;

a -= c; cout << "a-=c or a=a-c" << endl;

cout << "a=" << a << endl;

cout << "b=" << b << endl;

if (a == b) cout << "a and b are equal in magnitude" << endl;

b *= a; cout << "b*=a or b=b*a" << endl;

c /= d; cout << "c/=d or c=c/d" << endl;

cout << "b=" << b << endl;

cout << "c=" << c << endl;

return 0;

}