Operator Overloading C++
C++ Operator overloading
Operator Overloading C++ allows us to change how we can use operators such as +, -, ++, & etc. This can be done for user defined class and we can write our own code to give them new functionality.
Operator Overloading in C++
- We can use + to concatenate two strings together using an equation like str1 = str2 + str3, and can choose not to use concat() function.
- We can override ++ operator to not increment value by 1. But, rather by 100 possibly.
Why do we need operator overloading?
Operators work with operands. For example, we can use an increment operator with int values or int type variables like count ++.
But, we can’t use these operators with objects, directly.
We can't do that simply as obj++ operators don't work with classes.
However, we can overload these operators and give them special meanings to run along with objects and write code to increment the value by 100 for all variables. This is done via operator overloading.
How to define Overloading?
- We must use a keyword ‘operator’ before the operator want to overload
- It works as a function itself thus, a return type must also be defined
Example –
void operator ++() { counter1 = counter1 + 1; counter2 = counter2 + 1; }
Note – Overloaded operators will only work with objects of the class under which they are defined.
Example
Let us take an example wherein we want to –
- Overload the ++ operator to increment all the int variables in the class
- Increment data member variables by 100 not just simple 1 value
// Program to overload ++ operator to increment data member // values by 100 each, everytime ++obj is called using operator overloading #include <iostream> using namespace std; class PrepInsta { int x = 1, y = 2; int count1 = 3, count2 = 4; public: void print() { cout << x << " " << y << " " << count1 << " " << count2 << " respectively"<< endl; } // ++ operator overloading defined here void operator ++() { x = x + 100; y = y + 100; count1 = count1 + 100; count2 = count2 + 100; } }; int main() { PrepInsta obj; cout << "Values initially were -" << endl; obj.print(); // calling overloaded ++ operator on obj object ++obj; cout << "\nNew Values are -" << endl; obj.print(); return 0; }
Output
Values initially were - 1 2 3 4 respectively New Values are - 101 102 103 104 respectively
Operators for which overloading is allowed –
+ | – | * | / | % | ^ |
& | | | ~ | ! | , | = |
< | > | <= | >= | ++ | — |
<< | >> | == | != | && | || |
+= | -= | /= | %= | ^= | &= |
|= | *= | <<= | >>= | [] | () |
-> | ->* | new | new [] | delete | delete [] |
Operators for which Overloading is not allowed –
There are certain reasons because of which the writer of C++, Bjarne Stroustrup didn’t allow the same. Read it here on his official blog on why he didn’t allow overloading to certain operators.
:: | .* | . | ?: |
What this essentially does is, consider two objects obj1 and obj2 if we simply want to copy all the data values for obj1 into obj2 we can directly use the assignment operator as obj1 = obj2
It would work the same as copy constructor only it does shallow copy which may cause problems while working with pointers, you can read more about that here
Implicit Conversion via Conversion Constructors as assignment operator is overloaded
Now, the above line may be confusing for a few people. First, we suggest reading the following page –
- Conversion Constructors (read this and then proceed further)
- The conversion constructors are the ones that can be called with only a single argument.
- In such cases, if we directly use the assignment operator with an object and value.
- Can be used for implicit conversion.
Let us look at an example to understand this –
#include <iostream> using namespace std; class PrepInsta { int x; public: void display() { cout << "The value is: " << x << endl; } PrepInsta(int x1) { x = x1; } }; int main() { PrepInsta p1(50); p1.display(); // Default Operator Overloading happens here // Calls constructor again as single passing argument can call it p1 = 100; p1.display(); return 0; }
Output
The value is: 50 The value is: 100
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment