(Tutorial) Operator Overloading (C++ The Overloading Principle)
Operator Overloading (C++ The Overloading Principle)
In C++ the overloading principle applies not only to
functions, but to operators too. That is, the meaning of operators can be
extended from built-in types to user-defined types. In this way a programmer can
provide his or her own operator to a class by overloading the built-in operator
to perform some specific computation when the operator is used with objects of
that class. One question may arise here: is this really useful in real world
implementations? Some programmers consider that overloading is not useful most
of the time. This and the fact that overloading makes the language more
complicated is the main reason why operator overloading is banned in Java.
Even if overloading adds complexity to the language it can provide a lot of
syntactic sugar, and code written by a programmer using operator overloading can
be easy, but sometimes misleading, to read. We can use operator overloading
easily without knowing all the implementation's complexities. A short example
will make things clear:
Complex a(1.2,1.3); //this class is used to represent complex numbers
Comblex b(2.1,3); //notice the construction taking 2 parameters for the real and imaginary part
Complex c = a+b; //for this to work the addition operator must be overloaded
The addition without having overloaded operator + could look like this:
a.Add(b);
Complex c(a);
This piece of code is not as suggestive as the first one and
the readability becomes poor. Using operator overloading is a design decision,
so when we deal with concepts where some operator seems fit and its use
intuitive, it will make the code more clear than using a function to do the
task. However, there are many cases when programmers abuse this technique, when
the concept represented by the class is not related to the operator (like using
+ and - to add and remove elements from a data structure). In this cases
operator overloading is a bad idea, creating confusion.
In order to be able to write the above code we must have the "+" operator
overloaded to make the proper addition between the real members and the
imaginary ones and also the assignment operator. The overloading syntax is quite
simple, similar to function overloading, the keyword operator followed by the
operator we want to overload as you can see in the next code sample:
class Complex
{
public:
Complex(double re,double im)
:real(re),imag(im)
{};
Complex operator+(Complex);
Complex operator=(Complex);
private:
double real;
double imag;
}
Complex Complex::operator+(Complex num)
{
real = real + num.GetRealPart();
imag = imag + num.GetImagPart();
return *this;
}
The assignment operator can be overloaded similarly. Notice that we had to call the accessor function in order to get the real and imaginary parts from the parameter since they are private.
Read full article..
Courtesy : Cprogramming.com
- guru's blog
- Login or register to post comments

