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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

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

10 months ago

Minimum Cost to Paint Houses with K Colors

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

10 months ago

Longest Absolute Path in File System Representation

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

10 months ago

Efficient Order Log Storage

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

11 months ago