Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Operator Overloading | Polymorphism Overview

Operator OverloadingOperator Overloading

Operator overloading is a powerful feature in object-oriented programming languages that allows you to redefine the behavior of operators for user-defined classes. By overloading operators, you can provide custom implementations for operations involving objects of your class, enabling more intuitive and expressive code.

The syntax of operator overloading

Overloading s achieved by defining special member functions or friend functions that implement the desired behavior for the operator. The syntax for overloading operators varies depending on operator you want to overload. The general one looks like this:

The syntax, or more specifically, the number of parameters and returning type, also depends on whether you're overloading operators as member functions or friend functions. Consider the following two examples:

h

Member.h

h

Friend.h

The table of operators that can be overloaded

Here is a table listing all the operators that can be overloaded. However, it's important to note that while it is s possible to overload these operators, it is completely not necessary to overload all of them for your classes.

Operators
Purpose
Example
()
Function call
function()
[]
Array subscripting
array[index]
++ --
Increment/decrement
++a, a--, etc.
+ - * / %
Arithmetic operations
a + b, a - b, etc.
+= -= *= /=
Assignment operations
a += b, a -= b, etc.
== != < >
Comparison operations
a == b, a < b, etc.
<< >>
Stream insertion/extraction
cout << a, cin >> a

We will focus on the most commonly used operators, including stream insertion and extraction, arithmetic operators, increment and decrement operators.

Overloading stream insertion operators

To overload the << and >> operators for a class, you typically define a friend function or a member function that takes an output stream (std::ostream&) or input stream(std::istream&) as the left operand and an object of your class as the right operand. This function then formats the object's data and streams it to the output stream.

h

Point.h

Overloading stream insertion operators, such as <<, allows you to define custom output behavior for objects of your classes when they are streamed to an output stream, such as std::cout. This feature is particularly useful for enhancing the readability and usability of your code when dealing with custom data types.

Note

Overloading the << operator for output stream is more common than overloading the >> operator for input stream, because input operations with >> can be more error-prone.

Overloading arithmetic operators

You can overload other arithmetic operators (-, *, /, %) in a similar manner to perform custom operations with your user-defined types.

cpp

main.cpp

In this example, we overloade as a member function of the Point class. It takes another Point object as a parameter and returns a new Point object that represents the sum of the two points. You can change + for -, *, /, % and adjust a implementation logic a bit.

Increment and Decrement

You can overload both the prefix and postfix increment and decrement operators (++ and --) for your custom classes. Let's begin with prefix:

cpp

main.cpp

Note

The decrement operator is overloaded in a similar manner to the increment operator, with the use of the -- operator and subtraction operation.

As you see there is nothing hard in overloading prefix increment and decrement. However it is gets trickier when it comes to postfix one. As you remember there is an order of execution of postfix and prefix increment.

Operator
Expression
Output (Assuming x = 5)
Order of Execution
++Prefix
++x
6
Increment, then use
Postfix++
x++
5
Use, then increment
--Prefix
--x
4
Decrement, then use
Postfix--
x--
5
Use, then decrement

When overloading postfix increment and decrement we also has to implement the order of execution feature. It will look something like this:

cpp

main.cpp

Note

The integer parameter is passed without an argument name solely to signify to the compiler that it is the postfix increment operator being overloaded. It is necessary because the declaration of prefix and postfix operators does not differ in any other way.

In the postfix increment operator implementation, the original value of the object is stored in a temporary variable (temp). While the current object is then incremented, the value returned by the operator is the one stored in temp. So, the variable is incremented, but the incrementation takes effect only in the next expression, as the operator returns the original value.

What does operator overloading mean?

Selecione a resposta correta

Tudo estava claro?

Seção 5. Capítulo 4

Operator OverloadingOperator Overloading

Operator overloading is a powerful feature in object-oriented programming languages that allows you to redefine the behavior of operators for user-defined classes. By overloading operators, you can provide custom implementations for operations involving objects of your class, enabling more intuitive and expressive code.

The syntax of operator overloading

Overloading s achieved by defining special member functions or friend functions that implement the desired behavior for the operator. The syntax for overloading operators varies depending on operator you want to overload. The general one looks like this:

The syntax, or more specifically, the number of parameters and returning type, also depends on whether you're overloading operators as member functions or friend functions. Consider the following two examples:

h

Member.h

h

Friend.h

The table of operators that can be overloaded

Here is a table listing all the operators that can be overloaded. However, it's important to note that while it is s possible to overload these operators, it is completely not necessary to overload all of them for your classes.

Operators
Purpose
Example
()
Function call
function()
[]
Array subscripting
array[index]
++ --
Increment/decrement
++a, a--, etc.
+ - * / %
Arithmetic operations
a + b, a - b, etc.
+= -= *= /=
Assignment operations
a += b, a -= b, etc.
== != < >
Comparison operations
a == b, a < b, etc.
<< >>
Stream insertion/extraction
cout << a, cin >> a

We will focus on the most commonly used operators, including stream insertion and extraction, arithmetic operators, increment and decrement operators.

Overloading stream insertion operators

To overload the << and >> operators for a class, you typically define a friend function or a member function that takes an output stream (std::ostream&) or input stream(std::istream&) as the left operand and an object of your class as the right operand. This function then formats the object's data and streams it to the output stream.

h

Point.h

Overloading stream insertion operators, such as <<, allows you to define custom output behavior for objects of your classes when they are streamed to an output stream, such as std::cout. This feature is particularly useful for enhancing the readability and usability of your code when dealing with custom data types.

Note

Overloading the << operator for output stream is more common than overloading the >> operator for input stream, because input operations with >> can be more error-prone.

Overloading arithmetic operators

You can overload other arithmetic operators (-, *, /, %) in a similar manner to perform custom operations with your user-defined types.

cpp

main.cpp

In this example, we overloade as a member function of the Point class. It takes another Point object as a parameter and returns a new Point object that represents the sum of the two points. You can change + for -, *, /, % and adjust a implementation logic a bit.

Increment and Decrement

You can overload both the prefix and postfix increment and decrement operators (++ and --) for your custom classes. Let's begin with prefix:

cpp

main.cpp

Note

The decrement operator is overloaded in a similar manner to the increment operator, with the use of the -- operator and subtraction operation.

As you see there is nothing hard in overloading prefix increment and decrement. However it is gets trickier when it comes to postfix one. As you remember there is an order of execution of postfix and prefix increment.

Operator
Expression
Output (Assuming x = 5)
Order of Execution
++Prefix
++x
6
Increment, then use
Postfix++
x++
5
Use, then increment
--Prefix
--x
4
Decrement, then use
Postfix--
x--
5
Use, then decrement

When overloading postfix increment and decrement we also has to implement the order of execution feature. It will look something like this:

cpp

main.cpp

Note

The integer parameter is passed without an argument name solely to signify to the compiler that it is the postfix increment operator being overloaded. It is necessary because the declaration of prefix and postfix operators does not differ in any other way.

In the postfix increment operator implementation, the original value of the object is stored in a temporary variable (temp). While the current object is then incremented, the value returned by the operator is the one stored in temp. So, the variable is incremented, but the incrementation takes effect only in the next expression, as the operator returns the original value.

What does operator overloading mean?

Selecione a resposta correta

Tudo estava claro?

Seção 5. Capítulo 4
some-alt