Question: Requirements: You are required to implement the functions as declared in the header file ComplexNumber.h in a new file ComplexNumber.cpp. The implementation should include: ComplexNumber();
Requirements: You are required to implement the functions as declared in the header file ComplexNumber.h in a new file ComplexNumber.cpp. The implementation should include:
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-------------------------------------------------------------
#include #include "Mandelbrot.h" #include "ComplexNumber.h" using namespace std; int main() { ComplexNumber cNum(1.0, 1.0); ComplexNumber aNum(2.0, 2.0); ComplexNumber result = aNum * cNum; cout << aNum << " " << cNum << endl; cout << result << endl; cout << aNum * cNum << endl; cout << aNum + cNum << endl; Mandelbrot myFractal(100, 60); myFractal.Draw(); system("pause"); return 0; } -------------------------------------------------------------------------Mandelbrot.h-----------------------------------------------------------
#ifndef MANDELBROT_H #define MANDELBROT_H #include "ComplexNumber.h" class Mandelbrot { public: Mandelbrot(int width, int height); ~Mandelbrot(); void Draw(); bool Compute(const ComplexNumber z0); private: int _width, _height; bool* _data; }; #endif --------------------------------------------------------Mandelbrot.cpp----------------------------------------------------------
#include #include "Mandelbrot.h" using namespace std; const int MAX_ITERATIONS = 1000; Mandelbrot::Mandelbrot(int width, int height) { _width = width; _height = height; } Mandelbrot::~Mandelbrot() { // Only needed for completeness } void Mandelbrot::Draw() { double x, y; ComplexNumber z0; // Set up a complex number for (int row = 0; row < _height; row++) // Iterate over the rows of the image { z0.SetImaginary((4.0 * row) / _height - 2.0); for (int col = 0; col < _width; col++)// Iterate over the column of the image { z0.SetReal((4.0 * col) / _width - 2.0); if (Compute(z0)) // Determine if this point is in the set { cout << "*"; // And draw it as such } else cout << "."; } cout << endl; } cout << endl; } bool Mandelbrot::Compute(const ComplexNumber z0) { ComplexNumber z = z0; int iterations = 0; while (z.Magnitude() < 2 && iterations < MAX_ITERATIONS) { z = z*z + z0; iterations++; } return z.Magnitude() < 2; } -------------------------------------------------------------------------------ComplexNumber.h----------------------------------------------------------
#ifndef COMPLEXNUMBER_H #define COMPLEXNUMBER_H #include 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
