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

• Download Lab09_start.cpp corresponding to the class complexType from Blackboar

ID: 3850365 • Letter: #

Question

• Download Lab09_start.cpp corresponding to the class complexType from Blackboard. • Extend class complexType so that it performs the subtraction and division operations. To do so, overload the operators subtraction and division for this class as member functions. • Suppose (a,b) and (c,d) are complex numbers such that a and c are the real parts and b and d are the imaginary parts. (a,b)-(c,d) = (a -c,b-d). When (c,d) is nonzero: (a,b)/(c,d)=( (ac+bd)/(c^2,d^2) , (-ad+bc)/(c^2,d^2) ) When (c,d) is zero; the division is undefined.

Explanation / Answer

The code is given below :

#include <iostream>
#include <iomanip>
using namespace std;

class complexType
{
// overload stream insertion and extraction operators
friend ostream& operator<< (ostream&, const complexType&);
friend istream& operator>> (istream&, complexType&);

public:
void setComplex(const double& real, const double& imag);
//set the complex number according to the parameters
//Postcondition: realPart = real; imaginaryPart = imag

void getComplex(double& real, double& imag) const;
//Function to retrieve the complex number.
//Postcondition: real = realPart; imag = imaginaryPart

complexType(double real = 0, double imag = 0);
//Constructor
//Initialize the complex number according to the parameters
//Postcondition: realPart = real; imaginaryPart = imag

complexType operator+(const complexType& otherComplex) const;
//overload +
complexType operator*(const complexType& otherComplex) const;
//overload *
  

bool operator==(const complexType& otherComplex) const;
//overload ==

private:
double realPart; // variable to store the real part
double imaginaryPart; // variable to store the imaginary part
};


//Implementation complexType--------------------------------------------


ostream& operator<< (ostream& os, const complexType& complex)
{
os << "(" << complex.realPart << ", "
<< complex.imaginaryPart << ")";
return os;
}

istream& operator>> (istream& is, complexType& complex)
{
char ch;

is >> ch; //read and discard (
is >> complex.realPart; //get the real part
is >> ch; //read and discard,
is >> complex.imaginaryPart; //get the imaginary part
is >> ch; //read and discard)

return is;
}

bool complexType::operator==(const complexType& otherComplex) const
{
return(realPart == otherComplex.realPart &&
imaginaryPart == otherComplex.imaginaryPart);
}

//constructor
complexType::complexType(double real, double imag)
{
realPart = real;
imaginaryPart = imag;
}

void complexType::setComplex(const double& real, const double& imag)
{
realPart = real;
imaginaryPart = imag;
}

void complexType::getComplex(double& real, double& imag) const
{
real = realPart;
imag = imaginaryPart;
}

//overload the operator +
complexType complexType::operator+(const complexType& otherComplex) const
{
complexType temp;

temp.realPart = realPart + otherComplex.realPart;
temp.imaginaryPart = imaginaryPart + otherComplex.imaginaryPart;

return temp;
}

//overload the operator *
complexType complexType::operator*(const complexType& otherComplex) const
{
complexType temp;

temp.realPart = (realPart * otherComplex.realPart) -
(imaginaryPart*otherComplex.imaginaryPart);
temp.imaginaryPart = (realPart * otherComplex.imaginaryPart) +
(imaginaryPart * otherComplex.realPart);
return temp;
}