Question
Using the Rat Class as a guideline, complete the essentials for a complex number class. A complex number can be represented as a+bi. When adding,
Using the Rat Class as a guideline, complete the essentials for a complex number class.
A complex number can be represented as a+bi.
When adding, simple add the two parts separately
e.g. (4+5i) + (7-3i) = 11 + 2i
When multiplying, the terms need to "cross": just as you would do with (a+b) * (c+d)
e.g. (4+5i) * (7-3i) = 4*7 + 5*7i - 3*4i -3*5ii = 28 + (35-12)i - 15(-1) = (28+15) + 23i = 43 + 23i
Normal: A complex number can have a real part and the imaginary part. Use double to implement them.
Extra Credit: Use Rat instead of double.
Note: If you want to implement Extra Credit on a separate file, you can attach a link.
#include
using namespace std;
class Complex {
private:
double r;
double i;
public:
// constructors
// default constructor
Complex() {
r = 0;
i = 0;
}
Complex(double r1) {
//fill this in
}
Complex(double r1, double i1) {
//fill this in
}
// setters and getters (these should be easy)
int getR() {
}
int getI() {
}
void setR(double r1) {
}
void setI(double i1) {
}
// arithmetic operators
Complex operator+(Complex c) {
}
Complex operator-(Complex c) {
}
Complex operator*(Complex c) {
}
Complex operator/(Complex c) {
}
//We only need to output for this one
friend ostream &operator<<(ostream &os, Complex c);
}; // end of Complex Class
ostream &operator<<(ostream &os, Complex c) {
//fill this in as well
//if a complex number only has the real part, only print the real part
//i.e. Do not print 4+0i
}
int main() {
Complex c1, c2(1), c3(1,1);
cout << c1 << endl;
cout << c2 << endl;
cout << c3 << endl;
c1 = c2 + c3;
cout << c1 << endl;
c3 = c1 * c2;
cout << c3 << endl;
c3 = c3 / c2;
cout << c3 << endl;
c2 = c2 - c2;
cout << c2 << endl;
//feel free to add your own tests below here.
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