Operator Overloading
Operator overloading is one of the most fancy feature in C++. It is is a specific case of polymorphism, where different operators can be overloaded so that object operations are the same as the built-in types of the compiler, which greatly facilitate programmers.
Here we defined a class MyComplex which represents a complex number. The complex number has a real part mreal and an image part mimage.
Now in the main function we would like to add two MyComplex objects. For object types, a + b
is just a.operator+(b)
, so we need to overload operator +
, which simply add the real part and image part of two complex numbers separately.
It is also valid to add MyComplex with a number. In the following case, number 20 is converted into a temporary object c(20, 0) when passed inside.
But it won't work if the constant is before the operator +
. Since 20 is a constant, there's not any evidence for the compiler to call the overloaded function of MyComplex. In this case, the compiler will call the global operator +
, so we also need to overload it.
The global operator takes the object before it and after it as parameters. When the compiler does object operations, it will first call the overloaded function of the member method. If it is not found, the compiler will go on finding the appropriate overloaded function in the global scope.
Here inside this function we use the private member variables of MyComplex, which is apparently not allowed. To fix this, we can use keyword friend
inside MyComplex, to give the function access to private members.
Now our global overloaded function can also handle the sum of two MyComplex objects, so the overloaded member function is no longer needed.
Now let's deal with the self growth operator ++
. There are two types of them, one is before the object and the other is after the object. The former adds one to the object, and returns its value, while the latter returns the original value, and then adds one.
The one before the object is represented as a.operator++()
, and the one after the object is represented as a.operator++(int)
. The int parameter is not used. It is solely for the compiler to distinguish them.
We can also overload operator +=
, which is simple in this case:
We can also overload the output stream operator <<
. It is also a global function, which takes the ostream object as the first parameter, and our MyComplex object as the second. Similarly, it should also be defined as friend inside MyComplex.
Now we can use output stream to print our self-defined complex class.
The input stream is similar:
Last updated