Operator Overloading in C++

Operator Overloading in C++

What is Operator Overloading?

  • Operator overloading in C++ allows you to define custom behaviors for operators when applied to objects of user-defined classes.
  • In other words, it enables you to give new meanings to operators, beyond their standard operations on built-in data types.

Operator Overloading in C++ Syntax

Operator overloading in C++ follows a specific syntax. Here's the general syntax for overloading operators:
Loading…
  • return_type: This is the data type of the result that the operator will produce when it's applied to objects of your class.
  • operator: The keyword operator followed by the specific operator you want to overload. For example, +, -, ==, !=, <<, etc.
  • OPERATOR: Replace this with the actual operator you want to overload, such as +, -, ==, !=, <<, etc.
  • parameters: These are the parameters that the overloaded operator function takes.
  • The parameters depend on the operator being overloaded. For binary operators like + or -, etc.

Operator Functions vs. Normal Functions

Operator Functions

Naming Conventions

  • Operator functions have names like operator+, operator-, etc., and they have specific syntax and rules for overloading.
  • Normal functions do not have these specific naming conventions or restrictions.

Can we overload all operators?

Not all operators can be overloaded. Some operators, like the scope resolution operator::, member access operator. and .*, and conditional operator? : cannot be overloaded. However, all other operators can be overloaded.

Rules for Operator Overloading

  • Overloaded operators must have at least one user-defined type as an operand.
  • Some operators must be overloaded as member functions of a class, while others can be overloaded as global functions.
  • Operators retain their precedence and associativity unless changed explicitly.

Examples of Operator Overloading in C++

Unary Operator Overloading in C++

Unary operators are operators that work on a single operand. Let's overload the ++ and -- operators for a simple Counter class:
Loading…
In this example, we've overloaded the prefix increment (++) and decrement (--) operators for the Counter class, allowing us to increment and decrement the count member easily.

This Pointer in C++

  • In C++, the "this" pointer is a special pointer that is automatically available within member functions of a class.
  • It points to the object for which the member function is being called. Essentially, it refers to the current instance of the class.

Nameless Objects

Nameless objects are objects created without assigning them to a variable. They are often used for temporary calculations. Here's an example:
Loading…
  • In this example, we create two Point objects without assigning them to variables and add them together using the overloaded + operator.

Binary Operator Overloading in C++

Binary operators work on two operands. Let's overload the + operator for a Complex class:
Loading…
  • We define a Complex class to represent complex numbers with real and imaginary parts.
  • We overload the + operator within the Complex class to add two complex numbers together.
  • In the main function, we create two Complex objects, num1 and num2, with different values.
  • We use the overloaded + operator to add num1 and num2, storing the result in the sum variable.

Operator overloading in C++ using the friend function

  • Friend functions are non-member functions that are granted access to the private members of a class.
  • Here's an example of a Point class with a friend function for addition:
Loading…
In this example, we've defined a friend function operator+ that allows us to add two Point objects.

Conversion between Basic Types and User-Defined Types

  • You can define conversion operators to convert between user-defined types and basic types.
  • Here's an example where we convert a Distance class to an int:
Loading…
In this example, we've defined a conversion operator operator int() that allows us to convert a Distance object to an int.

Conclusion

Operator overloading in C++ empowers you to redefine the behavior of operators for user-defined classes.