C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

Operator overloading and its rules in C++

Learn: What is Operator Overloading in C++? What are the rules for operator overloading?

What is Operator Overloading in C++?

Operator overloading is an important part of object oriented programming languages. Operator overloading is a type of static or compile time polymorphism.

By using the concept of operator overloading, we can assign extra task to an existing operator.

For example, we can assign more than one task to + operator:

  1. + operator is used to add two number.
  2. + operator is also used to concatenate two string.

Purpose of Operator Overloading

The basic purpose of operator overloading is used to provide facility to the programmer, to write expressions in the most natural form.

Rules of Operator Overloading

There are following rules of operator overloading given below:

  1. The first and basic rule of operator overloading is: we can overload unary operator as only unary operator, it cannot be overload as binary operator and vice versa.
  2. We cannot overload those operators that are not a part of C++ language like ‘$’.
  3. We can perform operator overloading in only user defined classes. We cannot change the operators existing functionality.
  4. Using operator overloading we cannot change the presidency and associatively of operators.
  5. There are some operators cannot be overloaded that are given below:
    • :: Scope resolution operator.
    • . Class membership operator.
    • ?: ternary or conditional operator.
    • .* pointer to member operator.
    • ->* pointer to member operator.

Operator overloading can be done in two ways:

  1. Operator overloading of member functions
  2. Operator overloading of non-member or free functions

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.