4.2.1 Write the Class Declaration in Complex.hpp
Here is the UML class diagram for the complex class. Study the comments in Complex.hpp to write the class declaration.
– mimag – m real double double + complexO + complex (initreal double, init imag double) etor + get imag double + get_real double + set_imag (new imag double) : void + set_real (new real double) : void to string string – init(init real: double, init imag: double) i void ctor ” accessora accessora autator autator
4.2.2 Implement the class in Complex.cpp
Contains the implementation (member function definitions) of the complex class. Here is a brief description of each function. See the Complex.cpp source code file for the pseudocode descriptions of each function.
+complex::complex () The default constructor. Initializes the complex number to be 0+Oi by calling init(0, 0). Note that ctors never return a value so do not write void as the return data type. +complex::complex (init real: double, init imag : double) A second ctor. Initializes the complex number to be init real +init imagi by calling initfinit_real, init_imag)
+ complex::get imag 0: double An accessor function for the m imag data member. Recall that accessor functions are simple one statement functions: they return the value of the data member
+ O : double
An accessor function for the m_ real data member.
– complex::init (init_real : double. init_imag : double) :
This is a helper function which is intended to be called from only the ctors. Because this furxtion is not going to be called frotn out-
side the class. it is declared as private (helper functions are commonly private). init() initializes the complex number so the m_real
data member is initialized to init_real and the m _ imag data member to init_imag. It does this by calling the and
mutator functions and passing init_real and init_imag as the arguments.
+ (new_imag : double) : void
A mutator function for the m_ imag data member. Recall that mutator functions are simple one statement functions: we assign the
parameter to the data member.
+ complex::set_real (new_real : double) :
A mutator furxtion for the m_ real data member.
+ complex::to_string 0 : nring
Returns a string representation of the complex number. The format is where represents a space character
aid ± means there will either be a plus sign here or a minus sign. m_real and m_imag are formatted to four digits after the decimal
point. For example. if the m_real part of our complex number is 2.3123456 and the m_imag part is 4.543217. then the returned
string will be “(2.31230+04.5432i)”. On the other hand. if the m_imag part is negative, e.g., -4.543217, then the return string will be
“(2.31230-04.5432i)”. If the m_itnag part is O it will be omitted. For example, if the m_real part of the number is 12.1234567 aruf
the rn_ inug is O. then the return string will be “(12.1235)”. If the m_real part is O it will still be inckJded; For example, if the real
part is O and the m_imag part is -76.54329876. then the return string will be “(0.0000 – 76.5433i)”.