Categories: C++

Operator Overloading in C++

What is Operator Overloading?

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the user-defined data type with a special meaning. Most of the operators available in C++ are overloaded or redefined using operator overloading. It’s used to run an operation on a user-defined data type.

Operators that can be overloaded:
  • Binary Arithmetic     ->     +, -, *, /, %
  • Unary Arithmetic     ->     +, -, ++, —
  • Assignment     ->     =, +=,*=, /=,-=, %=
  • Bitwise      ->     & , | , << , >> , ~ , ^
  • Dereferencing     ->     (->)
  • Dynamic memory allocation and Deallocation     ->     New, delete
  • Subscript     ->     [ ]
  • Function call     ->     ()
  • Logical      ->     &,  | |, !
  • Relational     ->     >, < , = =, <=, >=

There are, however, a few operators who cannot be overloaded. The operators which are not overloaded are as follows:

  • scope operator -::
  • sizeof
  • member selector -.
  • member pointer selector – *
  • ternary operator – ?:
Operator Overloading Syntax:
return_type class_name  : : operator op(argument_list)  
{  
     // body of the function.  
}  
Implementation of Operator Overloading:

Operator overloading can be accomplished by implementing the following function:

  • Member Function
  • Non-Member Function
  • Friend Function
Overloading Rules for Operators:
  • Existing operators can only be overloaded, whereas new operators cannot.
  • At least one operand of the user-defined data type is present in the overloaded operator.
  • The friend function cannot be used to overload specific operators. The member function, on the other hand, can be used to overload those operators.
  • When unary operators are overloaded via a member function, they take no explicit arguments, but when overloaded via a friend function, they take one argument.
  • When binary operators are overloaded via a member function, they take one explicit argument; when they are overloaded via a friend function, they take two explicit arguments.

Note: also read about Virtual Destructors in C++

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

22 seconds ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago