After following the suggestions from the first question on that topic (link), I'd like to show you the result now:
#include <iostream>
class ComplexNumber {
private:
double real;
double imaginary;
public:
ComplexNumber operator+(ComplexNumber b) {
//Just add real- and imaginary-parts
double real = this->real + b.real;
double imaginary = this->imaginary + b.imaginary;
ComplexNumber c = ComplexNumber(real, imaginary);
return c;
}
ComplexNumber operator-(ComplexNumber b) {
//Just subtract real- and imaginary-parts
double real = this->real - b.real;
double imaginary = this->imaginary - b.imaginary;
ComplexNumber c = ComplexNumber(real, imaginary);
return c;
}
ComplexNumber operator*(ComplexNumber b) {
//Use binomial theorem to find formula to multiply complex numbers
double real = this->real * b.real - this->imaginary * b.imaginary;
double imaginary = this->imaginary * b.real + this->real * b.imaginary;
ComplexNumber c = ComplexNumber(real, imaginary);
return c;
}
ComplexNumber operator/(ComplexNumber b) {
//Again binomial theorem
double real = (this->real * b.real + this->imaginary * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary);
double imaginary = (this->imaginary * b.real - this->real * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary);
ComplexNumber c = ComplexNumber(real, imaginary);
return c;
}
void printNumber(char mathOperator) {
std::cout << "a " << mathOperator << " b = " << this->real << " + (" << this->imaginary << ") * i" << std::endl;
}
/*
* Constructor to create complex numbers
*/
ComplexNumber(double real = 0.0, double imaginary = 0.0) {
this->real = real;
this->imaginary = imaginary;
}
};
int main() {
/*
* Variables for the real- and imaginary-parts of
* two complex numbers
*/
double realA;
double imaginaryA;
double realB;
double imaginaryB;
/*
* User input
*/
std::cout << "enter real(A), imag(A), real(B) and imag(B) >> ";
std::cin >> realA >> imaginaryA >> realB >> imaginaryB;
std::cout << std::endl;
/*
* Creation of two objects of the type "ComplexNumber"
*/
ComplexNumber a(realA, imaginaryA);
ComplexNumber b(realB, imaginaryB);
/*
* Calling the functions to add, subtract, multiply and
* divide the two complex numbers.
*/
ComplexNumber c = a + b;
c.printNumber('+');
c = a - b;
c.printNumber('-');
c = a * b;
c.printNumber('*');
c = a / b;
c.printNumber('/');
return 0;
}
If you have any suggestions on further improving the code, I would really appreciate it if you share them with me.
this->
notation. It is only necessary to distinguish between member names and method parameters. Reduce the amount of typing effort and possible typos. \$\endgroup\$this->
extensively in C++ to refer to member fields, so it's not just a Java-acquired habit. I also think it adds clarity. The alternative for me would be some sort of prefix, likem_
, which a lot of people think is even uglier. Overall, if havingthis->
makes your code hard to read, then there are more fundamental problems, just like if havingstd::
makes your code hard to read. \$\endgroup\$