C++ ClassComplex Number Now that you can harness the power of operator overloads, you can tackle making your own complex
Posted: Sun May 15, 2022 1:03 pm
C++ ClassComplex Number
Now that you can harness the power of operator
overloads, you can tackle making your own complex number class.
Turns out this is an amazingly useful class to make if you want to
do any interesting math with your code.
A complex number has two components, a real (a) and
imaginary (b) component normally written as a + bi
You are going to develop a class that represents one of
these pairs and write some operator overloads that will help
manipulate these value in an appropriate way. Your complex class
will need to have separate .cpp and .h files.
Design the basics of the class including four
contractors: An empty constructor (sets a and b to 0), a
constructor that takes in a real component (sets b to 0), a
constructor that sets both a and b, and a copy constructor.
Implement the following overloads and class methods:
ComplexNumber abs() – Returns the absolute value of the
complex number (this is also the 'distance' from the
origin)
string to_string() – Returns a string: "a + bi", if b is
0, just return "a"
getRealComponent() and getImaginaryComponent() – Gets
the real and imaginary components respectively
float operator[](int index ) – The 0 index will return
the real component, the 1 index will return the imaginary
component
Overloads for the following operators: +, -, *, / - Be
aware that complexX * complexY is not 'just' X.a * Y.a and X.b *
Y.b:
(a +bi) * (c + di) = ac + adi + bic + bidi = (ac + bd(ii)) + (cd +
a*d)I = (ac – bd) + (cd + ad)i
Overloads for the following operators: +=, -=, *=, /= -
These should utilize the other operators!
Overloads for all 6 relational operators: You should
compare the abs() of each complex number
Overload stream insertion: << - In your .h file
this will take the form:
friend ostream& operator<<(ostream& out,
ComplexNumber& cn);
You will also need to access the elements of cn directly
(cn.a and cn.b)
Show that all of the above work correctly. This
means that you should have each of the operators demonstrated as
working correctly.
Now that you can harness the power of operator
overloads, you can tackle making your own complex number class.
Turns out this is an amazingly useful class to make if you want to
do any interesting math with your code.
A complex number has two components, a real (a) and
imaginary (b) component normally written as a + bi
You are going to develop a class that represents one of
these pairs and write some operator overloads that will help
manipulate these value in an appropriate way. Your complex class
will need to have separate .cpp and .h files.
Design the basics of the class including four
contractors: An empty constructor (sets a and b to 0), a
constructor that takes in a real component (sets b to 0), a
constructor that sets both a and b, and a copy constructor.
Implement the following overloads and class methods:
ComplexNumber abs() – Returns the absolute value of the
complex number (this is also the 'distance' from the
origin)
string to_string() – Returns a string: "a + bi", if b is
0, just return "a"
getRealComponent() and getImaginaryComponent() – Gets
the real and imaginary components respectively
float operator[](int index ) – The 0 index will return
the real component, the 1 index will return the imaginary
component
Overloads for the following operators: +, -, *, / - Be
aware that complexX * complexY is not 'just' X.a * Y.a and X.b *
Y.b:
(a +bi) * (c + di) = ac + adi + bic + bidi = (ac + bd(ii)) + (cd +
a*d)I = (ac – bd) + (cd + ad)i
Overloads for the following operators: +=, -=, *=, /= -
These should utilize the other operators!
Overloads for all 6 relational operators: You should
compare the abs() of each complex number
Overload stream insertion: << - In your .h file
this will take the form:
friend ostream& operator<<(ostream& out,
ComplexNumber& cn);
You will also need to access the elements of cn directly
(cn.a and cn.b)
Show that all of the above work correctly. This
means that you should have each of the operators demonstrated as
working correctly.