Operator Overloading is a handy feature in C++ that allows us to “overload” or “over-ride” an operator with our own custom code. Operators such as +
, -
, *
, /
may not work in certain situations, such as when adding together two objects from custom classes you may have created.
In order to resolve this, we “overload” these operators to ensure it correctly adds the objects in a way that we find acceptable. The examples we use in this article, are just one of the many ways you can use Operator Overloading in your C++ program code.
Introduction to Operator Overloading
In the code below we create a Class called “Data” with a variable value. We then proceed to create two objects, initializing the value variable to 5
and 10
respectively.
class Data {
public:
int value;
Data(int n) : value(n) {}
};
int main() {
Data d1(5);
Data d2(10);
// Data d3(0)
// d3 = d1 + d2;
}
Now, what would happen if we attempt to sum together the two objects using the + operator. You can see the would-be statement in the comments above. Due to the way the operators are configured, by default they will try adding both Objects instead of the integer values
. And of course, you can’t add arithmetically just “add” to objects together, so we need a way of telling the program to add the variables inside it instead.
This is where Operator Overloading comes in. The below code shows how to correctly overload the addition (+) operator
to achieve the desired result.
class Data {
public:
int value;
Data(int n) : value(n) {}
Data operator+(Data d) {
Data temp(0);
temp.value = value + d.value;
return temp;
}
}
So basically we created a new overloaded function for the + operator, in which we have a return type of Data
and an argument of type Data
. Why are we doing this? It’s because normally when we do Object + 20, it doesn’t change the value of the Object. It just “returns” a value that can either be printed out or stored in an Object. In short, without the above approach, Object + 20
would have the same effect as Object = Object + 20
.
Furthermore, the reason we take an argument is because operator is like a function which takes a parameter. Imagine the following statement, Object + 10. We can re-write this as, Object.+(10), where +() is a function of Object. The number after the + sign is always the one that gets passed into function. We call such operators, binary operators as they use two operands.
We are still missing one nessacery operator overload, which is for the =
operator. We’ll take a look at this and other operators in the next section.
Overloading Operators in C++
Let’s try and overload other Operators in C++, as a means of demonstration and explanation. Remember, you may overload them however you wish, depending on the situation at hand.
First up is the assignment operator, which requires a fairly simple overload. Whichever data member we want to have assigned to the new Data object, we will write a line for it in the overloaded function.
void operator=(Data d) {
value = d.value;
}
(What we mean here, is that if the Class has 5 – 6 member variables, you can choose which ones you want to assign to the new Data object).
Next is the increment operator.
void operator++(int) {
value += 1;
}
It’s a fairly simple overload, where we just add 1
into value.
Next is the +
operator, but with a slightly different take. Instead of adding two Data objects, what if we want to add an integer and a Data object.
Data operator+(int n) {
Data temp(0);
temp.value = value + n;
return temp;
}
For that we have the above code, which takes an int
as parameter, and returns an object with the new value.
Next are the multiply and divide operators following the same format as before.
Data operator*(Data d) {
Data temp(0);
temp.value = value * d.value;
return temp;
}
Data operator/(Data d) {
Data temp(0);
temp.value = value / d.value;
return temp;
}
Overloading Cout and Cin
I just feel like this is something alot of people will find useful, which is why I decided to address this separately. Here we will be overloading the “<<” and “>>” operators, which is used to print and receive data to and from the console.
In order to overload these operators, we need to use the concept of Friends functions. The functions needed to be created independently of the class, and then given access to the class through the use of the friend keyword.
class Data {
public:
int value;
Data(int n) : value(n) {}
friend ostream & operator << (ostream &out, const Data &d);
friend istream & operator >> (istream &in, Data &d);
};
The above snippet shows the Data class and the friend keyword, and the one below shows the function definition for the two functions.
ostream & operator << (ostream &out, const Data &d)
{
out << d.value;
return out;
}
istream & operator >> (istream &in, Data &d)
{
in >> d.value;
return in;
}
int main() {
Data d1(5);
cout << d1 << endl;
cin >> d1;
cout << d1 << endl;
}
The output of the above code:
5
7
7
This marks the end of the C++ Operator Overloading Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.