Introduction

  • Operator overloading in C++ also forms the basis of C++’s approach to I/O.
  • When an operator is overloaded, none of its original meanings/qualities are lost.
  • After overloading the appropriate operators, we can use objects in expressions in just the same way that we use C++’s built-in data types.
  • Not all languages support operator overloading like C++.

Definition

  • Operator overloading is a function that redefines a particular operator so that it performs a specific operation other than the original one when it is used with the object of a class.
  • Operator overloading is a dynamic type of polymorphism in which a single operator shows more than one different meaning/operation in a program hence the name operator overloaded. Thus, operator overloading provides the flexibility to create new definitions of C++ operators.
  • Operator overloading is the mechanism/way of giving special meanings to an operator is known as operator overloading. It provides a flexible option for the creation of new definitions for most of the C++ operators.
  • It is a special function to change the current functionality of some operators within its class. Thus, this is the method by which we can change the function of some specific operators to do some different task other than the original.

Characteristics/Features

  • Normally operators are overloaded in the program as a function is called an operator function. The operator function is declared with the operator keyword that precedes/followed by the operator symbol that has to be overloaded. 
  • In overloaded operators/operator functions, operators are distinguished by the number and type of operands used in it.
  • Operator overloading is defining a function for a particular operator.
  • Operator overloading allows us to provide new implementations for existing operators.
  • When an operator is used as either a unary or a binary operator (&, *, +, and -), we can overload each of them separately.
  • ++ and – – pre-unary operators are overloaded especially because they are prefix and postfix operators.
  • Overloaded operators don’t possess default arguments.
  • Order of precedence must be followed in operator overloading.
  • We cannot redefine the real meaning of operators when applied with built-in data types.
  • We cannot define new operators from built-in operators, such as **.
  • All overloaded operators (except assignment (=) operator) can be inherited by derived classes.
  • We may not redefine those operators that do not take part in operator overloading such as ::, sizeof(), ?:, (.)dot, etc.
  • Plus (+) overloading doesn’t overload +=, and similarly for the other extended assignment operators.
  • The assignment operator overloading (=) is also used especially so that the assignment should always be overloaded if an object dynamically allocates memory.
List of Operators
  • There are following C++ operators that support operator overloading are –

 +     –     *     /     %     ^     &     |     ~     !     =     <     >     +=     -=     *=     /=     %=     ^=
&=     |=     <<     >>     <<=     >>=     ==     !=     <=     >=     &&     ||     ++     – –     ,     ->*
->     ( )     [ ]     new     delete     new[]     delete[]

  • Some C++ operators do not support operator overloading. These are – sizeof() operator, conditional/ternary operator (?:), Scope resolution operator (::), Dot(.)/Class member access or selection operator, dot asterisk (.*)/Class member access or selection operator using pointer, etc.
    • sizeof() operator does not support operator overloading because it is evaluated by the compiler not at runtime so we can not overload it with own runtime code and also is syntactically not possible to do.
    • The conditional operator does not support operator overloading because it takes 3 arguments rather than 1 or 2. Also, there is no mechanism available by which we can pass 3 parameters during operator overloading.
    • The rest of the other operators(. or .* or ::) also do not support operator overloading when trying to overload, cause serious program errors or it is syntactically not possible because these operators ‘take a name, rather than a value, as their second operand and provide the primary means of referring to members. Also, C++ has no syntax for writing code that works on names rather than values so syntactically these operators can not be overloaded.
Types of Operators

Based on how operators are implemented, there are two major groups of operators used in operator overloading – Member operators and Non-member/Normal operators.

  • Member Operators are operators that are implemented/used as member functions of a class.
  • Non-member Operators are operators that are implemented/used as regular and outside the member functions of a class.(non-member functions).
  • Some operators are required to be member operators, others must be non-member operators, and some can be both.
Need/Reason for Operator Overloading
  • As we know most fundamental data types(such as integer, float, double, etc.) have pre-defined operators associated with them having normal behavior and mathematical function in a class.
  • Operator overloading isn’t strictly necessary unless other classes or functions expect operators to be re-defined.
Rules for Operator Overloading
  • Operator overloading can be implemented mostly using different forms of functions. These are:-
    1. Member Function
    2. Non-Member/Global Function
    3. Friend Function
    1. The operator overloading function can be a member function if the Left operand is the same as an object of that class.
    2. The operator overloading function can be a non-member function if the Left operand is different as an object of that class.
    3. The operator overloading function can be made a friend function if it needs access to the private and protected members of the class.
  • In other words, overloaded operators must either be a non-static class member/friend function or a global function, but =, [], and -> operators must be member functions if they are overloaded. An Operator cannot be used to overload when declaring that function as friend function = () [] ->.
  • The first argument for member-function overloaded operators is always of the class type of the object for which the operator is invoked (the class in which the operator is declared, or a class derived from that class). No conversions are supplied for the first argument.
  • The number of arguments in the overloaded operator‘s argument list depends on two factors :

(i) whether it‘s a unary operator(one argument) or a binary operator (two arguments).

(ii) Whether the operator is defined as a global function (one argument for unary, two for binary) or a member function (zero arguments for unary, one for binary – the object becomes the left-hand argument).

(iii) The Friend function will have one argument for the unary operator and two arguments for the binary operator. All the arguments may be passed either by value or by reference.

  • Unary Operator Overloading
    • When a Unary operator is used in operator overloading and is declared as a member function then it takes no arguments while if declared as a global/normal function, it takes one argument.
    • The operator ++, – -, and – are unary operators. The unary operators ++ and – – can be used as prefix/suffixes with the function.
    • These operators have only a single operand.
  • Binary Operator Overloading
    • When a Binary operator is used in operator overloading and declared as member functions then it takes one argument while if declared as global functions, it takes two arguments.
    • Overloading with a single parameter is called binary operator overloading.
    • They require two operands.
    • Binary operators are overloaded in two ways – member functions and friend functions.
      • Binary Operator Overloading for Member Function
        • If overloaded as a member function, they require one argument. The argument contains the value of the object, which is to the right of the operator.
        • A member function can be called by using a class of that object. Hence, the called member function is always preceded by the object.
        • Here, the data members of the first object are passed directly and the data members of the second object are passed as an argument.
        • During binary operator overloading, the left-hand operand calls the operator function and the right-hand operand is used as an argument.
      • Binary Operator Overloading for Friend Function
        • If overloaded as a friend function, it requires two arguments in its operator function.
        • The use of the member function and friend function produces the same result.
        • Friend function overloading is useful when we require performing an operation with an operand of two different types.
Syntax

return_type  classname : : operator opsymbol (op_argument_list)
{
    Source codes/Body of function
}

(where return_type=type of output value returned, class name = name of the class, operator= a keyword, opsymbol = the symbol of operator used in member function/friend function, op_argument_list = the parameter for the operator being overloaded) 

Steps to Create Operator Overloading

Step 1 – Create a class with the required data type that is going to be used in the operation of overloading.

Step 2 – Now the operator function operator op() is declared in a public area of the class.

Step 3 – Now define the operator function to implement the required operations.

Rules for calling/invoking Operator Functions

For overloading unary operators,

  • To declare a unary operator function as a non-static member, it must be declared in the form: return_type operator op(); where op is one of the operators listed in the preceding table.
  • To declare a unary operator function as a global function, it must be declared in the form: return_type operator op(arg); Where op is described for member operator functions and the arg is an argument of class type on which to operate. An overloaded unary operator may return any type.

(i) For member Function
      (a.) For unary operator: –  opsymbol m or m opsymbol
      (b.) For binary operator: – m opsymbol n or m.operator opsymbol (n)
(ii) For friend Function
      (a.) For unary operator:   operator opsymbol (m)
      (b.) For binary operator:   operator opsymbol (m, n)

Benefits/Advantages of Operator Overloading

  • We can almost create a new language/concept of our own by the creative use of the function and operator overloading techniques.
  • The main purpose of operator overloading is to make programs clearer/improve program readability by using conventional meanings for +, ==, [], etc.
  • It enhances the power of extensibility of C++, i.e. extending the capability of operators to operate on user-defined data.
  • The concept of operator overloading is also be applied to data conversion.

Demerits/Disadvantages of Operator Overloading

  • Applied with primitive data type mainly.
  • No new operators can be created, only existing operators can be overloaded.

Loading

Categories: C++

0 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.