Question
C++ HELP - Let a + ib be a complex number. The conjugate of a + ib is a ib, and the absolute value of
C++ HELP -
Let a + ib be a complex number. The conjugate of a + ib is a ib, and the absolute value of a + ib is sqrt(a^2+b^2)
Extend the definition of the class complexType given below by overloading the operators ~ and ! as member functions so that ~ returns the conjugate of the complex number and ! returns the absolute value. Also write the definition of these operator functions.
What to submit:
complexType.h file
complexType.cpp file
//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; }
|
#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
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