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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago