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

ComplexNumber(); The default constructor that sets the real and imaginary parts

ID: 3803701 • Letter: C

Question

ComplexNumber(); The default constructor that sets the real and imaginary parts to zero

ComplexNumber(double real, double imaginary); The constructor which takes two double values and sets the real and imaginary parts.

ComplexNumber& operator=(const ComplexNumber& rhs); The assignment operator that takes a reference to a complex number from the right side of the equals and sets this object to those values.

double Magnitude(); A function that returns the magnitude of the complex number calculated using the Pythagorean Theorem, i.e. as the square root of the sum of the squares for the real and imaginary parts.

void SetReal(double realValue); A mutator function that sets the real-value part of the complex number

void SetImaginary(double imaginaryValue); A mutator function that sets the imaginary-value part of the complex number

friend ComplexNumber operator+(const ComplexNumber& lhs, const ComplexNumber& rhs); A friend function that overloads and performs the addition operation for complex numbers, that is, it computes the sum of the real parts and the imaginary parts and returns the resulting complex number.

friend ComplexNumber operator*(const ComplexNumber& lhs, const ComplexNumber& rhs); A friend function that overloads and performs the multiplication operation for complex numbers, that is: it computes the product of the real parts minus the product of the imaginary parts as the resulting real part and the sum of the products of the real and imaginary parts of the two multiplicands as the resulting imaginary parts. It should then return the resulting complex number.

friend ostream& operator<<(ostream& os, const ComplexNumber& cNum); number. A function that overloads the stream insertion operator for printing. Complex numbers are generally printed in the form x + yi where x is the real part and y is the imaginary part.

------------------------------------------------------------------HWmain.cpp-------------------------------------------------------------

-------------------------------------------------------------------------Mandelbrot.h-----------------------------------------------------------

--------------------------------------------------------Mandelbrot.cpp----------------------------------------------------------

-------------------------------------------------------------------------------ComplexNumber.h----------------------------------------------------------

Explanation / Answer

Please refer below code

1) ComplexNumber.h

#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H

#include<iostream>
#include<cmath>

using namespace std;

class ComplexNumber
{
public:
ComplexNumber();
ComplexNumber(double real, double imaginary);

ComplexNumber& operator=(const ComplexNumber& rhs);

double Magnitude();
void SetReal(double realValue);
void SetImaginary(double imaginaryValue);

friend ComplexNumber operator+(const ComplexNumber& lhs, const ComplexNumber& rhs);
friend ComplexNumber operator*(const ComplexNumber& lhs, const ComplexNumber& rhs);
friend ostream& operator<<(ostream& os, const ComplexNumber& cNum);

private:
double _real, _imag;
};
#endif

2) ComplexNumber.cpp

#include "ComplexNumber.h"

ComplexNumber::ComplexNumber()
{
_real = 0.0;
_imag = 0.0;
}
ComplexNumber::ComplexNumber(double real, double imaginary)
{
SetReal(real);
SetImaginary(imaginary);
}

double ComplexNumber::Magnitude()
{
return sqrt(this->_real * this->_real + this->_imag * this->_imag);
}
void ComplexNumber::SetReal(double realValue)
{
_real = realValue;
}
void ComplexNumber::SetImaginary(double imaginaryValue)
{
_imag = imaginaryValue;
}
ComplexNumber& ComplexNumber::operator=(const ComplexNumber& rhs)
{
_real = rhs._real;
_imag = rhs._imag;
return *this; //here we have to return the address of left operand on = operator which is class itself and hence return *this
}

ComplexNumber operator+(const ComplexNumber& lhs, const ComplexNumber& rhs)
{
ComplexNumber result;

result._real = lhs._real + rhs._real;
result._imag = lhs._imag + rhs._imag;
return result;
}
ComplexNumber operator*(const ComplexNumber& lhs, const ComplexNumber& rhs)
{
ComplexNumber result;
//(a+bi) * (c + di)
//real = a*c - b * d;
//imag = a * d + b * c
result._real = (lhs._real * rhs._real) - (lhs._imag * rhs._imag);
result._imag = (lhs._real * rhs._imag) + (lhs._imag * rhs._real);
return result;
}
ostream& operator<<(ostream& os, const ComplexNumber& cNum)
{
os << cNum._real << "+i "<<cNum._imag<<endl;
return os;
}

3) driver.cpp

#include "ComplexNumber.h"

int main()
{
ComplexNumber C1(12.3,10.3);
ComplexNumber C2(10.0,7.0);
ComplexNumber C;

C = C1 + C2;

cout<<C;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote