Operator Overloading in C++ With Example

Posted on

Operator Overloading

Operator Overloading

Operator overloading is a crucial idea in C++. It’s a kind of polymorphism during which an operator is overloaded to provide consumer outlined that means to it. Overloaded operator is used to carry out operation on user-defined knowledge kind. For instance ‘+’ operator may be overloaded to carry out addition on numerous knowledge sorts, like for Integer, String(concatenation) and many others.

operator overloading - Operator Overloading in C++ With Example

Nearly any operator may be overloaded in C++. Nonetheless there are few operator which can’t be overloaded. Operator that aren’t overloaded are follows

  • scope operator – ::
  • sizeof
  • member selector – .
  • member pointer selector – *
  • ternary operator – ?:

Operator Overloading Syntax

operator overloading syntax - Operator Overloading in C++ With Example


Implementing Operator Overloading

Operator overloading may be executed by implementing a operate which may be :

  1. Member Operate
  2. Non-Member Operate
  3. Buddy Operate

Operator overloading operate generally is a member operate if the Left operand is an Object of that class, but when the Left operand is totally different, then Operator overloading operate have to be a non-member operate.

Operator overloading operate may be made pal operate if it wants entry to the personal and guarded members of sophistication.


Restrictions on Operator Overloading

Following are some restrictions to be saved in thoughts whereas implementing operator overloading.

  1. Priority and Associativity of an operator can’t be modified.
  2. Arity (numbers of Operands) can’t be modified. Unary operator stays unary, binary stays binary and many others.
  3. No new operators may be created, solely current operators may be overloaded.
  4. Can not redefine the that means of a process. You can’t change how integers are added.

Operator Overloading Examples

Nearly all of the operators may be overloaded in infinite alternative ways. Following are some examples to study extra about operator overloading. All of the examples are carefully related.


Overloading Arithmetic Operator

Arithmetic operator are mostly used operator in C++. Nearly all arithmetic operator may be overloaded to carry out arithmetic operation on user-defined knowledge kind. Within the under instance we have now overridden the + operator, so as to add to Time(hh:mm:ss) objects.


Instance: overloading ‘+’ Operator so as to add two time object


Overloading I/O operator

  • Overloaded to carry out enter/output for consumer outlined datatypes.
  • Left Operand can be of sorts ostream& and istream&
  • Operate overloading this operator have to be a Non-Member operate as a result of left operand will not be an Object of the category.
  • It have to be a pal operate to entry personal knowledge members.

You could have seen above that << operator is overloaded with ostream class object cout to print primitive kind worth output to the display screen. Equally you may overload << operator in your class to print user-defined kind to display screen. For instance we’ll overload << in time class to show time object utilizing cout.

NOTE: When the operator doesn’t modify its operands, the easiest way to overload the operator is by way of pal operate.


Instance: overloading ‘<<‘ Operator to print time object

Output


Overloading Relational operator

You can even overload Relational operator like == , != , >= , <= and many others. to match two user-defined object.


Instance


Copy constructor Vs. Project operator

Project operator is used to repeat the values from one object to a different already current object. For instance

Copy constructor is a particular constructor that initializes a new object from an current object.


  • Subsequent →