Operator Overloading in Object Oriented Programming


C++ permits a programmer to control and alter the performance of just about each operator that’s utilized in the usual. This course of, often called operator overloading, is a really helpful course of that enables a programmer to avoid wasting time and assets while writing a code. There are particular guidelines and conventions that govern the overall technique of overloading operators in an object oriented paradigm. On this article, I’ll attempt to define these guidelines and why these have been carried out by the creators and pioneers of C++.

The entire idea of operator overloading makes C++ a really helpful and malleable language through which to program. A programmer can basically redefine what the same old operators job is on a particular class and thus can mould all operators in a manner as to go well with his exact wants.

For instance, say now we have a category named Fraction whose object possesses a numerator and denominator discipline. Now, in the actual world we all know that including fractions will not be the identical as simply including integers. Assuming now we have two objects of the fraction class referred to as ‘obj1’ and ‘obj2’ writing code like:

Fraction obj3 =obj1 + obj2;

Wouldn’t work until and till the ‘+’ operator is overloaded or redefined. Nonetheless as soon as the overloaded perform is written, the compiler can acknowledge and execute all such statements and retailer the specified lead to our obj3.

Operator Overloading: Guidelines and Conventions:

1. You may solely overload these operators which are already supported by the usual. Thus made-up symbols (corresponding to ‘ ” ‘ or ‘ @ ‘) will not be overloaded.

2. Current operators can’t be mixed and made into new symbols. For instance, ‘-+’ will not be used.

three. All operators utilized in C++ will be overloaded except for six. These are ‘?: ‘, ‘ sizeof ‘, ‘.* ‘, ‘:: ‘, ‘. ‘ and ‘ typeid ‘ operators.

four. You can’t change the priority or associativity degree of the operators by overloading them. This would be the identical as already predefined within the language commonplace.

5. All inherited lessons robotically inherit all overloaded operators apart from the task operator.

6. No default parameters will be handed to overloaded operator capabilities.

7. There are particular different limitations. For instance. The task operator should be declared as a member perform of the category. Then again, insertion and extraction operator ought to at all times be declared as non-member capabilities of the category. Do not forget that they need to be declared non-member capabilities solely when related to istream and ostream; for enter and output to things.

eight. Though you possibly can select any performance while overloading an operator, it’s higher to stay as near the unique performance as potential. By that, I imply that at all times carry out addition with addition operator somewhat than subtraction. It’s simpler to grasp and saves undue problems in your code.

9. If there’s even a single pointer as information member in your class, it at all times helps to overload at the least the task operator within the very starting. This can assist in a while in your program.

10. Take particular care of the nested use of operators,altering what the overloaded perform returns accordingly. E.g. operations corresponding to: a = b = c and x + y + z ought to be catered for.

As we are able to see, there are a lot of guidelines and conventions programmer ought to take of adhering to whereas overloading operators. Nonetheless, this software can show to be very highly effective in writing a dependable and environment friendly code, particularly when the code offers with pointers, inheritance and different superior programming ideas.

Leave a Reply

Your email address will not be published. Required fields are marked *

The New Demand for Resource in the Mobile Application Development Job Market

How You Can You Develop iPad / iPhone Apps On A Windows Computer