Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ overloading the operators ? and / Download files corresponding to the class complexType from Blackboard. Extend class complexType so that it performs the subtraction

C++

overloading the operators ? and / Download files 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, division is undefined. Write the definitions of the functions to overload the operators ? and /. Write a test program that tests various operations on the class complexType. Format your answer with two decimal places.

-----------------------complexType.cpp-----------------------

//Implementation file complexType.cpp #include  #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 #include  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-------------------------------------------

#include  #include "complexType.h" using namespace std; int main() { complexType num1(23, 34); //Line 1 complexType num2; //Line 2 complexType num3; //Line 3 cout << "Line 4: Num1 = " << num1 << endl; //Line 4 cout << "Line 5: Num2 = " << num2 << endl; //Line 5 cout << "Line 6: Enter the complex number " << "in the form (a, b) "; //Line 6 cin >> num2; //Line 7 cout << endl; //Line 8 cout << "Line 9: New value of num2 = " << num2 << endl; //Line 9 num3 = num1 + num2; //Line 10 cout << "Line 11: Num3 = " << num3 << endl; //Line 11 cout << "Line 12: " << num1 << " + " << num2 << " = " << num1 + num2 << endl; //Line 12 cout << "Line 13: " << num1 << " * " << num2 << " = " << num1 * num2 << endl; //Line 13 return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+ Why have these changes occurred?

Answered: 1 week ago

Question

=+90 percent of all oil refineries) into several smaller companies

Answered: 1 week ago