PDFVersion
No ads? No problem! Download the PDF book of this tutorial for just $24.99. Your support will help us reach more readers.
Thank You!
Operator Overloading:
Overloading is a concept we've encountered frequently. Let's delve into an example of overloading. In all our examples, we've used the Stream Insertion Operator <<
with cout to display various types of data on the screen. Sometimes we print integers, characters, doubles, floats, and strings as illustrated below:
cout << "string";
cout << int;
cout << char;
cout << float;
cout << double;
We observe that although we use only one operator, <<
, it serves multiple data types. This indicates that the operator is overloaded for various data types. The iostream
header file contains prototypes for the functions that enable overloading this operator.
Without operator overloading in C++, we would require separate insertion operators for each data type. However, thanks to operator overloading, a single operator can work with multiple data types. Let's consider another example involving the +
operator. We use this operator to add integers, floats, or doubles. But what if we need to add two objects? Attempting to add two objects using the +
operator results in an error because computers lack the intelligence to perform such operations. Therefore, we need to overload the +
operator to enable addition of objects.
Operator overloading permits us to redefine how operators function for user-defined types such as objects or structures. Some libraries already contain prototypes and functions for the + operator, enabling the C++ compiler to know how to add two integers using the + operator. However, it does not know how to add two objects, necessitating operator overloading.
Operator overloading involves implementing a function that can be a member function of a class, a non-member function of a class, or a friend function of a class. In this tutorial, we'll implement the function as a member function of a class. To overload an operator, we define a special operator function inside the class as follows:
class ClassName {
public:
return operator OperatorSymbol (ArgumentList) {
// Function body
}
...
};
In the syntax above, we use the keyword operator after the return type. Following operator, we specify the operator symbol +
, -
, *
, /
, etc. that we want to overload. Arguments can be passed to the operator function in a manner similar to regular functions, and we define this function as a public member of the class. Let's illustrate with an example:
#include <iostream>
using namespace std;
class Overload {
private:
int counter;
public:
Overload() {
counter = 5;
}
void operator ++() {
counter = counter + 1;
}
void Display() {
cout << "Counter is: " << counter << endl;
}
};
int main() {
Overload load;
cout << "Before Overload Operator: " << endl;
load.Display();
++load;
cout << "After Overload Operator: " << endl;
load.Display();
return 0;
}
In the above example, we overload the increment operator ++
to increase the value of our object. When we attempt to increment the object, the overloaded operator is called instead of the default operator since the default increment operator cannot increment objects.
Pro-Tip:
Try removing the overloaded operator function from the class, compile the program, and observe if you can increment the object value using the default increment operator.
Here's a list of operators that can be overloaded:
++
+
-
*
/
%
^
&
|
,
=
--
++
<=
=
<
<<
==
!=
&&
||
+=
-=
/=
%=
^=
&=
()
[]
<<=
=
*=
|=
->
->*
new
new[]
delete
delete[]
Operators That Can't Be Overloaded:
Certain operators cannot be overloaded:
::
.*
.
?:
Learn about Operators in C++to gain insights into their functionality and usage within the language.
- Operator overloading is applicable only to user-defined data types like objects or structures.
- Overloading operators for built-in data types such as int, char, or float is not possible.
- Operator overloading does not alter the precedence and associativity of operators.
Sardar Omar
I did my hardest to present you with all of the information you need on this subject in a simple and understandable manner. However, if you have any difficulties understanding this concept or have any questions, please do not hesitate to ask. I'll try my best to meet your requirements.