Javatpoint Logo
Javatpoint Logo

Rules for Operator overloading in C++

In this article, you will learn about the rules for operator overloading in C++. There are several rules for operator overloading in C++. Some main rules are as follows:

1. Syntax

Overloading an operator is defined by defining a function with the operator keyword followed by the Operator to be overloaded.

For example:

2. Logical and Consistent Implementation

Overloaded operators should be implemented logically and consistently with the expected behaviour of the corresponding built-in operators.

Avoid overloading operators in a way that might lead to confusion or unexpected behaviour.

3. Number of Operands

Most unary and binary operators can be overloaded.

Unary operators take one operand (e.g., +, -, ++, --).

Binary operators take two operands (e.g., +, -, *, /).

4. Precedence and Associativity

The precedence and associativity of the relevant built-in operators are passed down to overload operators.

Overloading cannot alter an operator's precedence and associativity.

5. Types of Operand and Return

An operand for an overload operator must be of at least one user-defined type.

The return type should usually reference the class or the class type for assignment operators.

6. Overloading Restrictions

Some operators cannot be overloaded, or their behaviour cannot be changed significantly. For example, (member access), .* (member pointer access), :: (scope resolution), ?: (ternary conditional), sizeof, and typeid.

7. Overloading as a Member Function

When overloaded as a member function, the left operand is implicitly the calling object.

Binary operators must have at least one operand of the user-defined type.

Example:

8. Overloading as a Non-Member Function

Both operands are supplied as arguments when overloading as a non-member function.

Example:

9. Friend Function Overloading

Operator overloading can also be handled using a friend function if it requires access to private members.

Example:

10. Common Operators to Overload

Some commonly overloaded operators include +, -, *, /, %, ==, !=, <, >, <=, >=, +=, -=, *=, /=, ++, --, =, <<, and >>.

11. Overloading << for Output

Overloading << allows you to define how objects of your class should be displayed using std::cout.

Example:

Example:

Output:

Complex Number 1: 2.5 + 3.5i
Complex Number 2: 1.5 + 2.5i
Sum: 4 + 6i

Explanation:

1. Definition of Class (Complex)

  • In this example, the 'Complex' class represents complex numbers having real and imaginary components.
  • It has a public constructor and two private data members, 'real' and 'imag' for initialisation.

2. Overloaded Operator + (Member Function)

  • It is overloaded because the '+' Operator is a member function of the Complex class.
  • The parameter for the function is a reference to another Complex object (other).
  • The relevant real and imaginary parts are added to generate a new Complex object (result).

3. Overloaded Operator << (Friend Function)

  • The << Operator is overused as a friend function to alter the output format.
  • It requires two references: a Complex object (obj) and a std::ostream& reference (os).
  • The real and imaginary components are printed to the output stream in the designated format.

4. Main Function

  • Two Complex objects (c1 and c2) are created with initial values.
  • The '+' Operator adds c1 and c2; the result is stored in the sum variable.
  • The << Operator displays the complex numbers and their sum.

5. Output

  • When executed, the program outputs the complex numbers and their total in the predetermined format.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA