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

Book: DATA STRUCTURES USING C++, 2nd EDITION by D.S. Malik Chapter 2, Page 128,

ID: 3624754 • Letter: B

Question

Book: DATA STRUCTURES USING C++, 2nd EDITION by D.S. Malik
Chapter 2, Page 128, Problem 14

Let a + ib be a complex number. The conjugate of a + ib is a – ib and the absolute value of a + ib is v(a^2+b^2 ) . Extend the definition of a class complexType of the programming example below, Complex Numbers by overloading the operators ~ and ! as member functions so that ~ returns the conjugate of the complex number and ! returns the absolute value. Write the definitions of these operator functions.

complexType.cpp

//Implementation file complexType.cpp

#include <iostream>
#include "complexType.h"

using namespace std;

ostream& operator<<(ostream& osObject,
const complexType& complex)

{
osObject << "("; //Step a
osObject << complex.realPart; //Step b
osObject << ", "; //Step c
osObject << complex.imaginaryPart; //Step d
osObject << ")"; //Step e

return osObject; //return the ostream object
}

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

isObject >> ch; //Step a
isObject >> complex.realPart; //Step b
isObject >> ch; //Step c
isObject >> complex.imaginaryPart; //Step d
isObject >> ch; //Step e

return isObject; //return the istream object
}

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;
}

//Function to set the complex number after the object
//has been declared.
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.h
//Specification file complexType.h

#ifndef H_complexNumber
#define H_complexNumber

//*****************************************************************
// Author: D.S. Malik
// class complexType.h
// This class specifies the members to implement a complex number.
//*****************************************************************

#include <iostream>
using namespace std;

class complexType
{
//Overload the 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);
//Function to set the complex numbers 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
//Initializes the complex number according to
//the parameters.
//Postcondition: realPart = real; imaginaryPart = imag;

complexType operator+
(const complexType& otherComplex) const;
//Overload the operator +

complexType operator*
(const complexType& otherComplex) const;
//Overload the operator *

bool operator == (const complexType& otherComplex) const;
//Overload the operator ==

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

#endif

testcomplexNumber.cpp
//**********************************************************
// Author: D.S. Malik
//
// This program shows how to use the class complexType.
//**********************************************************

#include <iostream> //Line 1
#include "complexType.h" //Line 2

using namespace std; //Line 3

int main() //Line 4
{ //Line 5
complexType num1(23, 34); //Line 6
complexType num2; //Line 7
complexType num3; //Line 8

cout << "Line 9: Num1 = " << num1 << endl; //Line 9
cout << "Line 10: Num2 = " << num2 << endl; //Line 10

cout << "Line 11: Enter the complex number "
<< "in the form (a, b): "; //Line 11
cin >> num2; //Line 12
cout << endl; //Line 13

cout << "Line 14: New value of num2 = "
<< num2 << endl; //Line 14

num3 = num1 + num2; //Line 15

cout << "Line 16: Num3 = " << num3 << endl; //Line 16

cout << "Line 17: " << num1 << " + " << num2
<< " = " << num1 + num2 << endl; //Line 17

cout << "Line 18: " << num1 << " * " << num2
<< " = " << num1 * num2 << endl; //Line 18

return 0; //Line 19
} //Line 20

Explanation / Answer

please rate - thanks

sorry

I don't work in multiple files, so you'll have to incorporate the changes (in red) into the original

#ifndef H_complexNumber
#define H_complexNumber

//*****************************************************************
// Author: D.S. Malik
// class complexType.h
// This class specifies the members to implement a complex number.
//*****************************************************************
#include<cmath>
#include <iostream>
using namespace std;

class complexType
{
//Overload the 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);
//Function to set the complex numbers 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
//Initializes the complex number according to
//the parameters.
//Postcondition: realPart = real; imaginaryPart = imag;
complexType operator~();
complexType operator!() ;    
       
complexType operator+
(const complexType& otherComplex) const;
//Overload the operator +

complexType operator*
(const complexType& otherComplex) const;
//Overload the operator *

bool operator == (const complexType& otherComplex) const;
//Overload the operator ==

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

#endif

//complexType.cpp

//Implementation file complexType.cpp

#include <iostream>
//#include "complexType.h"

using namespace std;

ostream& operator<<(ostream& osObject,
const complexType& complex)

{
osObject << "("; //Step a
osObject << complex.realPart; //Step b
osObject << ", "; //Step c
osObject << complex.imaginaryPart; //Step d
osObject << ")"; //Step e

return osObject; //return the ostream object
}

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

isObject >> ch; //Step a
isObject >> complex.realPart; //Step b
isObject >> ch; //Step c
isObject >> complex.imaginaryPart; //Step d
isObject >> ch; //Step e

return isObject; //return the istream object
}

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;
}

//Function to set the complex number after the object
//has been declared.
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;
}
complexType complexType::operator~()
{complexType temp;
temp.realPart = realPart;
temp.imaginaryPart = -imaginaryPart;
return temp;
}
complexType complexType::operator!()
{complexType temp;
temp.realPart = sqrt(realPart*realPart+imaginaryPart *imaginaryPart );
temp.imaginaryPart = 0;
return temp;
}
//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.h
//Specification file complexType.h


//testcomplexNumber.cpp
//**********************************************************
// Author: D.S. Malik
//
// This program shows how to use the class complexType.
//**********************************************************

#include <iostream> //Line 1
//#include "complexType.h" //Line 2

using namespace std; //Line 3

int main() //Line 4
{ //Line 5
complexType num1(23, 34); //Line 6
complexType num2; //Line 7
complexType num3; //Line 8

cout << "Line 9: Num1 = " << num1 << endl; //Line 9
cout << "Line 10: Num2 = " << num2 << endl; //Line 10

cout << "Line 11: Enter the complex number "
<< "in the form (a, b): "; //Line 11
cin >> num2; //Line 12
cout << endl; //Line 13

cout << "Line 14: New value of num2 = "
<< num2 << endl; //Line 14

num3 = num1 + num2; //Line 15

cout << "Line 16: Num3 = " << num3 << endl; //Line 16

cout << "Line 17: " << num1 << " + " << num2
<< " = " << num1 + num2 << endl; //Line 17

cout << "Line 18: " << num1 << " * " << num2
<< " = " << num1 * num2 << endl; //Line 18
cout<<!num1<<" =absolute value of "<<num1<<endl;
cout<<~num1<<" =conjugate of "<<num1<<endl;

return 0; //Line 19
} //Line 20