C++ Calculator for complex numbers I've written a little calculator in C++ for complex numbers:

#include &lt;iostream&gt;
using namespace std;

class ComplexNumber {

    public:
        double real;
        double imaginary;

        void add(ComplexNumber a, ComplexNumber b) {

            //Just add real- and imaginary-parts
            double real = a.real + b.real;
            double imaginary = a.imaginary + b.imaginary;
            ComplexNumber c = ComplexNumber(real, imaginary);
            cout &lt;&lt; "a + b = " &lt;&lt; c.real &lt;&lt; " + (" &lt;&lt; c.imaginary &lt;&lt; ") * i" &lt;&lt; endl; 
        }

        void sub(ComplexNumber a, ComplexNumber b) {

            //Just subtract real- and imaginary-parts
            double real = a.real - b.real;
            double imaginary = a.imaginary - b.imaginary;
            ComplexNumber c = ComplexNumber(real, imaginary);
            cout &lt;&lt; "a - b = " &lt;&lt; c.real &lt;&lt; " + (" &lt;&lt; c.imaginary &lt;&lt; ") * i" &lt;&lt; endl; 
        }

        void multiply(ComplexNumber a, ComplexNumber b) {

            //Use binomial theorem to find formula to multiply complex numbers
            double real = a.real * b.real - a.imaginary * b.imaginary;
            double imaginary = a.imaginary * b.real + a.real * b.imaginary;
            ComplexNumber c = ComplexNumber(real, imaginary);
            cout &lt;&lt; "a * b = " &lt;&lt; c.real &lt;&lt; " + (" &lt;&lt; c.imaginary &lt;&lt; ") * i" &lt;&lt; endl; 
        }


        void divide(ComplexNumber a, ComplexNumber b) {

            //Again binomial theorem
            double real = (a.real * b.real + a.imaginary * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary);
            double imaginary = (a.imaginary * b.real - a.real * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary);
            ComplexNumber c = ComplexNumber(real, imaginary);
            cout &lt;&lt; "a : b = " &lt;&lt; c.real &lt;&lt; " + (" &lt;&lt; c.imaginary &lt;&lt; ") * i" &lt;&lt; endl; 
        }

    /*
     * Constructor to create complex numbers
     */
    ComplexNumber(double real, double imaginary) {
        this-&gt;real = real;
        this-&gt;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
     */
    cout &lt;&lt; "enter real(A), imag(A), real(B) and imag(B) &gt;&gt; ";
    cin &gt;&gt; realA &gt;&gt; imaginaryA &gt;&gt; realB &gt;&gt; imaginaryB;
    cout &lt;&lt; endl;

    /*
     * Creation of two objects of the type "ComplexNumber"
     */
    ComplexNumber a = ComplexNumber(realA, imaginaryA);
    ComplexNumber b = ComplexNumber(realB, imaginaryB);

    /*
     * Calling the functions to add, subtract, multiply and 
     * divide the two complex numbers.
     */
    a.add(a, b);
    a.sub(a, b);
    a.multiply(a, b);
    a.divide(a, b);
    return 0;
}


I would appreciate any suggestions to improve the code.



You can find my follow-up question here.
In short programs it can be OK, but in general avoid  writing using namespace std. You'll find plenty of material here and elsewhere on why this is so.
To promote proper encapsulation of data, both real and imaginary should be declared under private, i.e., not be visible to the outside.
All of the four member functions that perform arithmetic take on too much responsibility and as a result, are very inconvenient for the user. That is, remember: one function, one responsibility. If you add, then you don't also print. For example, as a user, I just want to use your class for complex arithmetic - I don't want to print every time I do so!
Your four member functions don't modify the state of the object. This makes the whole class and its functionality quite rigid and strange. As it is, the functionality appears as it should be a collection of four free functions not inside any class (indeed, perhaps your background is in Java where I can imagine this is more common). A more intuitive interface for let's say the addition would be void add(const ComplexNumber&amp; other) { ... }, where the implementation actually adds to real and imaginary of *this. Same for the other three operations.
If you wanted to get fancy, you could use operator overloading to allow for a natural way to express complex arithmetic for the user.
It would be useful to add a void print() const { ... } method in case the user wants to print.
Use an initializer list if you need to write explicit constructors, i.e., write ComplexNumber(double r, double i) : real(r), imaginary(i) { } instead. If you don't, the compiler will call default constructors on the members first which in your case is unnecessary.
In modern C++, we have the option of using in-class constructors for default values. This is quite handy, i.e., you could have double real {0.0}; (similarly for imaginary) if you wanted to support the creation of complex numbers without an explicit constructor call.
By the way, you don't have to write ComplexNumber a = ComplexNumber(realA, imaginaryA); when it's much cleaner to write ComplexNumber a(realA, imaginaryA);.
Perhaps you know this, but std::complex&lt;T&gt; does exist if you wanted to do complex arithmetic in a more serious setting.