Introduction

  • In C++, most of the operators are overload so that they perform special operations relative to classes that we create.
  • 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.
  • Operator overloading also forms the basis of C++‘s approach to I/O.
  • The concept of overloading a function can be applied to operators as well.
  • Not all languages support operator overloading like c++.

Definition

  • The mechanism/the ability to provide the operators with a special meaning for data type  & operator is called operator overloading.
  • Operator overloading is a type of polymorphism in which a single operator shows different meaning/operation in a program hence the name operator overloaded.Thus, operator overloading provides a flexibility to create new definitions of C++ operators.
  • Operator overloading is the mechanism 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. we can almost create a new language of our own by the creative use of the function and operator overloading techniques.
  • 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.

Characteristics/Features

  • Operators when overloaded in the program as function is called operator function. Operator function is declared with operator keyword that precedes/followed by the operator that has to be overloaded. 
  • In overloaded operators/operator functions they are distinguished by the number and type of operands used in it, the mechanism is applicable to the operators.
  • Operator overloading is defining a function for a particular operator.
  • Operator overloading allows us to provide new implementations for existing operators.
List of Operators
  • List of C++ operators that support operator overloading are –

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

  • Some C++ operators does not support operator overloading are – sizeof() operator, Conditional 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.
    • Conditional operator does not support operator overloading because it takes 3 argument rather than 1 or 2. Also, there is no mechanism available by which we can pass 3 parameters during operator overloading.
    • The rest other operators(. or .* or ::) that also does not support operator overloading when try 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 overload.
Types of Operators

There are two groups of operators used (on the basis of how they are implemented) in operator overloading – Member operators and Non-member operators.

  • Member operators are operators that are implemented as/used in member functions (methods) of a class.
  • Non-member operators are operators that are implemented/used as  regular, and outside the member functions (non-member functions).
  • Some operators are required to be member operators, others must be non-member operators, and some can be both.
Need/Reason of Operator Overloading
  • As we know that 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 using a function which can be :
    1. Member Function
    2. Non-Member/Global Function
    3. Friend Function
    1. Operator overloading function can be a member function if the Left operand is same as an  object of that class.
    2. Operator overloading function can be a non-member function if the Left operand is different as an object of that class.
    3. Operator overloading function can be made friend function if it needs access to the private and protected members of class.
  • In another 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.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) Friend function will have one argument for unary operator and two arguments for 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 are declared as member functions then it takes no arguments while if declared as global functions, they take one argument.
    • The operator ++, – – and – are unary operators.The unary operators ++ and – – can be used as prefix/suffix with the function.
    • These operators have only 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, they take two arguments.
    • Overloading with a single parameter is called as binary operator overloading.
    • They require two operands.
    • Binary operators are overloaded using two ways – member functions and friend functions.
    • ———————————————————————————-
    • If overloaded as a member function, they require one argument. The argument contains value of the object, which is to the right of the operator.
    • A member function can be called by using class of that object. Hence, the called member function is always preceded by the object.
    • Here, the data members of first object are passed directly and data members of second object are passed as an argument.
    • During binary operator overloading, the left hand operand calls the operator function and right hand operand is used as an argument.
    • ————————————————————————————-
    • If overloaded as a friend function, they require two arguments in its operator function.
    • The use of member function and friend function produces same result.
    • Friend function overloading are useful when we require performing an operation with operand of two different types.
  • 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 specially because they are prefix and postfix operators.
  • Overloaded operators doesn’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 does 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 specially so that assignment should always be overloaded if an object dynamically allocates memory.
Syntax

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

(where return_type=type of output value returned, classname = name of the class, operator= a keyword, op = the operator being overloaded, operator opsymbol = the name of operator member function/friend function.) 

Steps to Create Operator Overloading

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

Step 2 – Now the operator function operator op() is                         declared in public area of 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

  • Operator overloading allows the full integration of new class type into programming environment.
  • The main purpose of operator overloading is to make programs more clear/improves program readability by using conventional meanings for +, ==, [] etc.
  • It enhances the power of extensibility of C++, i.e. extending capability of operators to operate on user defined data.
  • Operator overloading is a function which redefines a particular operator so that it performs a specific operation when it is used with the object of a class.
  • 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.

 566 total views,  1 views today

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.