Skip to main content
Tweeted twitter.com/StackCodeReview/status/1224482904627130370
Became Hot Network Question
Please don't create new meta-tags ("beginner" is an exception to this rule)
Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327
edited tags
Link
user214772
user214772
Source Link
user214772
user214772

C++ Calculator for complex numbers - follow-up

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.