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

Rewrite the definition of the class complexType so that the arithmetic and relat

ID: 3737551 • Letter: R

Question

Rewrite the definition of the class complexType so that the arithmetic and relational operators are overloaded as nonmember functions. Write a test program that tests various operations on the class complexType. Format your answer with two decimal places.

**********************

complexType.h

**********************

//Specification file complexType.h

#ifndef H_complexNumber

#define H_complexNumber

#include <iostream>

using namespace std;

class complexType

{

// overload stream insertion and extraction operators

friend ostream& operator<< (ostream&, const complexType&);

friend istream& operator>> (istream&, complexType&);

friend complexType operator+(const complexType& one,

const complexType& two);

//overload +

friend complexType operator*(const complexType& one,

const complexType& two);

//overload *

friend complexType operator-(const complexType& one,

const complexType& two);

//overload -

friend complexType operator/(const complexType& one,

const complexType& two);

//overload /

friend bool operator==(const complexType& one,

const complexType& two);

//overload ==

public:

void setComplex(const double& real, const double& imag);

//Function to 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

private:

double realPart; // variable to store the real part

double imaginaryPart; // variable to store the imaginary part

};

*************************

complexType.cpp

*************************

//Implementation file complexType.cpp

#include <iostream>

#include "complexType.h"

using namespace std;

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;

}

complexType complexType::operator-(const complexType& otherComplex) const

{

complexType temp;

temp.realPart = realPart - otherComplex.realPart;

temp.imaginaryPart = imaginaryPart - otherComplex.imaginaryPart;

return temp;

}

complexType complexType::operator/(const complexType& otherComplex) const

{

complexType temp;

double denominator;

if (otherComplex.realPart == 0 && otherComplex.imaginaryPart == 0)

{

cout << "Cannot divide by zero" << endl;

return otherComplex;

}

else

{

denominator = otherComplex.realPart * otherComplex.realPart +

otherComplex.imaginaryPart * otherComplex.imaginaryPart;

temp.realPart = ((realPart * otherComplex.realPart) +

(imaginaryPart * otherComplex.imaginaryPart)) /

denominator ;

temp.imaginaryPart = ((- realPart * otherComplex.imaginaryPart) +

(imaginaryPart * otherComplex.realPart)) /

denominator;

return temp;

}

}

Explanation / Answer

Please find the updated code below with detailed inline comments.

CODE

=====================

**********************

complexType.h

**********************

//Specification file complexType.h

#ifndef H_complexNumber

#define H_complexNumber

#include <iostream>

using namespace std;

class complexType

{

// overload stream insertion and extraction operators

friend ostream& operator<< (ostream&, const complexType&);

friend istream& operator>> (istream&, complexType&);

friend complexType operator+(const complexType& one,

const complexType& two);

//overload +

friend complexType operator*(const complexType& one,

const complexType& two);

//overload *

friend complexType operator-(const complexType& one,

const complexType& two);

//overload -

friend complexType operator/(const complexType& one,

const complexType& two);

//overload /

friend bool operator==(const complexType& one,

const complexType& two);

//overload ==

public:

void setComplex(const double& real, const double& imag);

//Function to 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

private:

double realPart; // variable to store the real part

double imaginaryPart; // variable to store the imaginary part

};

*************************

complexType.cpp

*************************

//Implementation file complexType.cpp

#include <iostream>

#include "complexType.h"

using namespace std;

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 operator==(const complexType& one,
const complexType& two)
{
return(one.realPart == two.realPart &&
one.imaginaryPart == two.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 operator + (const complexType& one, const complexType& two)
{
complexType temp;

temp.realPart = one.realPart + two.realPart;
temp.imaginaryPart = one.imaginaryPart + two.imaginaryPart;

return temp;
}

//overload the operator *
complexType operator * (const complexType& one, const complexType& two)
{
complexType temp;

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

complexType operator - (const complexType& one, const complexType& two)
{
complexType temp;

temp.realPart = one.realPart - two.realPart;
temp.imaginaryPart = one.imaginaryPart - two.imaginaryPart;

return temp;
}

complexType operator / (const complexType& one, const complexType& two)
{
complexType temp;

double denominator;

if (two.realPart == 0 && two.imaginaryPart == 0)
{
cout << "Cannot divide by zero" << endl;
return two;
}
else
{
denominator = two.realPart * two.realPart +
two.imaginaryPart * two.imaginaryPart;


temp.realPart = ((one.realPart * two.realPart) +
(one.imaginaryPart * two.imaginaryPart)) /
denominator ;
temp.imaginaryPart = ((- one.realPart * two.imaginaryPart) +
(one.imaginaryPart * two.realPart)) /
denominator;
return temp;
}

}

***************************

Main.cpp

***************************

#include <iostream>

#include "complexType.cpp"

using namespace std;

int main() {
complexType c1, c2, result;
c1.setComplex(1, 5);
c2.setComplex(6, 5.5);
  
cout << "Complex number 1: " << c1 << endl
<< "Complex number 2: " << c2 << endl;
result = c1 + c2;
cout << "Addition: " << result << endl;
result = c1 * c2;
cout << "Product: " << result << endl;
result = c1 - c2;
cout << "Difference: " << result << endl;
result = c1 / c2;
cout << "Division: " << result << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote