ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

The concept of operator overloading in C++

Updated on June 21, 2015
radhikasree profile image

Radhika has the degree of master in computer applications. She has worked as a faculty with Aptech Computer Education Center for sometime.

Operator overloading in C++
Operator overloading in C++ | Source

C++ programming language focuses on object-oriented programming that has several advantages over the conventional form. The freedom to create user-defined data type has extended the functionality of coding and has simplified long and complicated programs. Objects may have several attributes relevant to the class to which they belong and they are dealt with member or friend functions as the case may be.

Operator overloading concept gives a special meaning to an operator such as ‘+’ or ‘-‘. Addition or subtraction cannot be directly performed with objects as we do with basic data types. Like overloading functions, operators also can be overloaded for doing this task. This is achieved with the help of member functions and friend functions. The general form of an overloaded operator function is

return type operator op(argument list);

where ‘op’ stands for the operator sign.

Operator overloading by means of member functions


While overloading operators using member functions, only objects belonging to that class can access it. So the object used to invoke it would be the first operand and the objects or variables specified in its argument list would be the remaining operands.

A unary operator overloaded by means of a member function will have no arguments and a binary operator overloaded function will have only one argument.

For example, the unary minus operator overloaded function should be

return type operator-()

and a binary operator overloaded function should be

return type operator+(argument 1);

An example for operator overloading by means of member function can be read here.

There can be situations when all the operands need not be objects (user-defined data types) of a class. When the left-hand operand of a binary operator is not of user-defined data type, using a member function will not work. Such cases are handled by using friend functions that take both the operands as parameters.


Operator overloading by means of friend functions


Friend functions are treated as normal functions that have no access to the private members of a class. They can be called like a normal function and operators can be overloaded through them. All the operands are passed as arguments explicitly here. The general form of an operator overloaded friend function is

friend return type operator op(argument list);

where ‘op’ stands for the operator sign.

For example, a unary operator overloaded friend function should be

friend return type operator-(argument) and

a binary operator overloaded friend function should be

friend return type operator+(argument1, argument2);

Even if friend functions can’t overload operators such as assignment operator (=), subscripting operator ([]), function call operator (()), and member access operator (->) they come handy in some situations where member functions won’t help. In scalar multiplication of a vector, an integer (built-in data type) and an object (user defined data type) are the operands. When the object becomes the left-hand operand, operator overloading through member function would work. But in the other case, this won’t work as an object of the relevant class is required as the left-hand operand in order to invoke the member function. Friend functions can solve this problem by taking both the operands as the arguments.

Some rules for overloading operators


  • Only existing operators can be overloaded. We cannot create new operators for overloading.
  • We cannot change the basic meaning of an operator. We cannot redefine ‘+’ operator to subtract one value from the other.
  • Overloaded operators follow the syntax rules of the original operator. They cannot be overridden.
  • There are some operators that cannot be overloaded.
  • Binary operators such as ‘+’, ‘-‘, ‘*’ and ‘/’ must explicitly return a value.

Operators that cannot be overloaded

Name
syntax
sizeof
sizeof()
membership
.*
conditional
?:
scope resolution
::

Please vote!

Does your program get simplified through operator overloading?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • radhikasree profile image
      Author

      Radhika Sreekanth 4 years ago from Mumbai,India

      Thanks Ruby for such a nice comment. Actually these were invented by some intelligent scientists that I studied during my masters. I'm scribbling a little of what I've learned!!

    • always exploring profile image

      Ruby Jean Fuller 4 years ago from Southern Illinois

      You write such intelligent hubs, i am envious..Ha..Thank's for sharing..Cheers..

    Click to Rate This Article