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
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
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
#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;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started