C++ Operator Overloading

OPERATOR OVERLOADING

When an operator is overloaded with multiple jobs it is known as Operator Overloading. It is one of the methods to implement compile time polymorphism.

Operators are overloaded so that they can perform special operations relative to the classes. When an operator is overloaded, none of its original meaning is lost, instead, the types of object it can be applied to is expanded. By overloading operators, we can use objects of classes in expressions in the same way as we use C++ built in data types.

RULES

Any symbol can be used as a function name.
  • If it is a valid operator in C language.
  • If it is preceded by 'operator' keyword.

Operators that can't be overloaded are 'sizeof' and '?:

TYPES

There are two types of Operator Overloading.
  • Unary Operator Overloading.
  • Binary Operator Overloading.

WHY OPERATOR OVERLOADING
  •  It makes the code readable.
  •  Extention of language to include user defined types i.e classes.
  • Makes operator sensitive to context.
  • Generalization of function overloading.

SYNTAX




Share this

Related Posts

FIND US ON FACEBOOK!