C++ : Operator Overloading

Table of Contents


Operator overloading is one of the most exciting features of object-oriented programming. It can transform complex, obscure program listings into intuitively obvious ones.
For example, statements like

     d3.addobjects(d1, d2);

or the similar but equally obscure

d3 = d1.addobjects(d2);

can be changed to the much more readable

d3 = d1 + d2;

The rather forbidding term operator overloading refers to giving the normal C++ operators, such as +, *, <=, and +=, additional meanings when they are applied to user-defined data types. Normally

a = b + c;

works only with basic types such as int and float, and attempting to apply it when a, b, and c are objects of a user-defined class will cause complaints from the compiler. However, using overloading, you can make this statement legal even when a, b, and c are user-defined types. In effect, operator overloading gives you the opportunity to redefine the C++ language. If you find yourself limited by the way the C++ operators work, you can change them to do whatever you want. By using classes to create new kinds of variables, and operator overloading to create new definitions for operators, you can extend C++ to be, in many ways, a new language of your own design.

Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators.
We can overload all the C++ operators except the following:

1. Scope Resolution Operator (::)
2. Size operator (sizeof)
3. Class member access operators (. , *)
4. Conditional operator (?:)

The reason why we cannot overload these operators because these operators take names (example class name) as their operand instead of values

Defining Operator Overloading

To define an additional task, we must specify the class to which the operator is applied.This is done with the help of a special function called operator function which describe the task.
The general form of an operator function is:


return_type classname :: operator op(argument_list)
Function Body



where return_type is thee type of value returned by specified operation and op is the operator being overloaded. operator op is the function name where operator is the keyword.

Operator functions must be either member functions or friend function.A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators, while member function has zero argument for unary operators and only one for binary operators.This is because the object used to invoke the member function is passed impicitly and therefore its available for member function. This not the case with friend function, argument may be passed by either by value or by reference
Operator functions are declared in class using prototype as mention below:


// Consider example as the data type of class

int operator==(example)                                          // comparison
friend int operator==(example,example)              // comparison
example operator+(example)                                // example addition
example operator-()                                                // unary minus
friend example operator+(example,example)     // example addition
friend example operator-(friend)                          // unary minus



THe process of overloading involves the following steps:

1. Create a class that defines the data type that is to be used in the overloading operation.
2. Declare the operator function operator op() in the public part of the class.It may be either a member function or a friend function.
3. Define the operator function to implement the required operation.

Overloaded operator function can be invoked as below:
For friend function,

x.operator op(y)
For member function,

operator op(x,y)

Rules for Overloading Operators

1. Only exiting operators can be overloaded. New operator cannot be created.
2. The overloaded operator must have at least one operand that is of user defined type.
3. Overloaded operators follow the syntax rules of the original operators.They cannot be overridden.
4. There are some operators that cannot be overloaded.
5. We cannot use friend functions to overload cer tain operators.However member function can be used to overload them.
6. Unary operator overloaded by means of a member function, take no explicit argument and return no explicit values but those overloaded by means of a friend function take one reference argument.
7. Binary operator overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit argument.
8. When using binary object operators overloaded through member function the left hand operand must be an object of the relevant class.
9. Binary arithmetic operators such as +, -, *, and / must explicitly return a value.They must not attempt to change their own arguments.
10. Operator that cannot be overloaded are:

Membership operator (.)
Pointer to member operator(.*)
Scope resolution operator(::)
Conditional operator(?:)

11. Operator that friend function cannot be used:

Assignment operator (=)
Function call operator(())
Subscripting operator([ ])
Class member access operator(->)

Leave a Comment