//main.cpp #include #include \"ComplexNumber.h\" using namespace std; int main()
ID: 3659442 • Letter: #
Question
//main.cpp #include #include "ComplexNumber.h" using namespace std; int main() { ComplexNumber num1(23, 34);//Line 1 ComplexNumber num2;//Line 2 ComplexNumber 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 cout << "Line 14: " << num1 << " * " << num2 << " = " << num1 * num2 << endl; //Line 14 cout << "Line 15: " << num1 << " / " << num2 << " = " << num1 / num2 << endl; //line 15 return 0; } /*******************************************/ //ComplexNumber.h //header file //Specification file ComplexNumber.h #ifndef COMPLEX_NUMBER #define COMPLEX_NUMBER #include using namespace std; class ComplexNumber { //Overload the stream insertion and extraction operators friend ostream& operator << (ostream&, const ComplexNumber&); friend istream& operator >> (istream&, ComplexNumber&); 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; ComplexNumber(double real = 0, double imag = 0); //Constructor //Initializes the complex number according to //the parameters. //Postcondition: realPart = real; imaginaryPart = imag; ComplexNumber operator + (const ComplexNumber& otherComplex) const; //Overload the operator + ComplexNumber operator - (const ComplexNumber& otherComplex) const; //Overload the operator - ComplexNumber operator * (const ComplexNumber& otherComplex) const; //Overload the operator * ComplexNumber ComplexNumber::operator/(ComplexNumber &div); //Overload the operator / bool operator == (const ComplexNumber& otherComplex) const; //Overload the operator == private: double realPart; //variable to store the real part double imaginaryPart; //variable to store the //imaginary part }; //end class ComplexNumber #endif /************************************************************/ //ComplexNumber.cpp //Implementation file ComplexNumber.cpp #include #include "ComplexNumber.h" using namespace std; ostream& operator<<(ostream& osObject, const ComplexNumber& 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 } //end ostream& operator<<(ostream& osObject, const ComplexNumber& complex) istream& operator>>(istream& isObject, ComplexNumber& 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 } //end istream& operator>>(istream& isObject, ComplexNumber& complex) bool ComplexNumber::operator == (const ComplexNumber& otherComplex) const { return (realPart == otherComplex.realPart && imaginaryPart == otherComplex.imaginaryPart); } //end bool ComplexNumber::operator==(const ComplexNumber& otherComplex) const //Constructor ComplexNumber::ComplexNumber(double real, double imag) { realPart = real; imaginaryPart = imag; } //end ComplexNumber::ComplexNumber(double real, double imag) //Function to set the complex number after the object //has been declared. void ComplexNumber::setComplex(const double& real, const double& imag) { realPart = real; imaginaryPart = imag; } //end void ComplexNumber::setComplex(const double& real, const double& imag) void ComplexNumber::getComplex(double& real, double& imag) const { real = realPart; imag = imaginaryPart; } //end void ComplexNumber::getComplex(double& real, double& imag) const //overload the operator + ComplexNumber ComplexNumber::operator + (const ComplexNumber& otherComplex) const { ComplexNumber temp; temp.realPart = realPart + otherComplex.realPart; temp.imaginaryPart = imaginaryPart + otherComplex.imaginaryPart; return temp; } //end ComplexNumber ComplexNumber::operator + (const ComplexNumber& otherComplex) const ComplexNumber ComplexNumber::operator - (const ComplexNumber& otherComplex) const { ComplexNumber temp; temp.realPart = realPart - otherComplex.realPart; temp.imaginaryPart = imaginaryPart - otherComplex.imaginaryPart; return temp; } //end ComplexNumber ComplexNumber::operator - (const ComplexNumber& otherComplex) const //overload the operator * ComplexNumber ComplexNumber::operator * (const ComplexNumber& otherComplex) const { ComplexNumber temp; temp.realPart = (realPart * otherComplex.realPart) - (imaginaryPart * otherComplex.imaginaryPart); temp.imaginaryPart = (realPart * otherComplex.imaginaryPart) + (imaginaryPart * otherComplex.realPart); return temp; } //end ComplexNumber ComplexNumber::operator * (const ComplexNumber& otherComplex) const ComplexNumber ComplexNumber::operator / (ComplexNumber &div) { double denom; denom = realPart * div.realPart + div.imaginaryPart * div.imaginaryPart; double numReal; double numImag; numReal= realPart * div.realPart + imaginaryPart * div.imaginaryPart; numImag=realPart * (-1)*div.imaginaryPart + imaginaryPart*(div.realPart); return ComplexNumber (numReal/denom, numImag/denom); } //end ComplexNumber ComplexNumber::operator/(ComplexNumber &div) ,**************************************************************************/ //my question is: How do I get the operator == to work in this program. not sure how to write out the bool expression to //work.Explanation / Answer
post the entire question not the link here
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.