Operator Overloading in C++
In C++, operator overloading is needed when you want to customize the behavior of operators like +,-,* etc for objects of user-defined classes. By default, operators (like +) work on built-in types like int, float, etc. For example, you can easily add two integers using the + operator:
| int a = 10; int b = 20; int c = a + b; // This works fine without any operator overloading |
However, when you’re working with objects of a class (like MyClass), you can’t directly add two objects using the + operator unless you overload it. Without operator overloading, trying to use + on class objects (ClassObject3 = ClassObject1 + ClassObject2;) will result in a compile-time error.
- Operator overloading allows you to redefine the behavior of operators for your own custom classes.
- By overloading the
+operator forMyClass, you define how twoMyClassobjects should be added (by adding theirvaluemembers). - After overloading the
+operator, you can use it just like any built-in operator (such as adding integers), but it will now work withMyClassobjects.
Example of “+ Operator” Overloading
Here is an example of operator overloading using the + operator to enable object addition for class (ClassObject3 = ClassObject1 + ClassObject2).
| #include <iostream> using namespace std;class MyClass { private: int value; public: // Function to input the value // Function to display the value // Overload the ‘+’ operator int main() { // Input values for ClassObject1 and ClassObject2 cout << “Enter the value for ClassObject2:\n”; // Add aa and bb using overloaded + operator // Display the result return 0; |
Explanation of (operator+) Function
In C++, operator overloading allows you to define custom behavior for operators like +, -, *, etc., when they are used with user-defined types (classes). In this case, we are overloading the + operator for the MyClass class, which lets us add two MyClass objects together. Let’s explain the following code
| MyClass operator+(MyClass ClassObject2) { MyClass ClassObject3; // Create a temporary object ClassObject3 ClassObject3.value = value + ClassObject2.value; // Add values of both objects return ClassObject3; // Return the temporary object ClassObject3 } |
1. The Function Signature:
| MyClass ClassObject3; |
- This defines the function that overloads the
+operator forMyClassobjects. ClassObject2is the parameter that represents the second object being added to the calling object (the object on the left-hand side of the+operator).- The return type of this function is
MyClass, which means this function will return an object of typeMyClassafter adding the two objects.
2. The Temporary Object ClassObject3:
| MyClass ClassObject3; |
- We create a temporary
MyClassobject calledClassObject3. This object will hold the result of the addition of thevaluefrom the calling object (the one on the left of+) andClassObject2(the one on the right of+). - It starts with no initialized value; it will later store the sum of the values.
3. Adding the Values:
| ClassObject3.value = value + ClassObject2.value; |
- Here, the actual addition happens:
valuerefers to thevalueof the calling object (the object on the left-hand side of the+operator).ClassObject2.valuerefers to thevalueof the second object passed to the function (the object on the right-hand side of the+operator).
- The
valueof the calling object is added to thevalueofClassObject2, and the result is stored inClassObject3.value.
4. Returning the Result:
| return ClassObject3; |
- After performing the addition, the
operator+function returnsClassObject3, which contains the result of the addition. This object is now aMyClassobject with the sum of thevalueof the calling object andClassObject2.
Summary of the Flow:
ClassObject1 + ClassObject2invokes the overloaded+operator.- Inside
operator+, the values ofClassObject1andClassObject2are added together and stored in a temporary objectClassObject3. - The
operator+function returnsClassObject3, which is assigned to theClassObject3in themain()function. - The result is that
ClassObject3now holds the sum of the values ofClassObject1andClassObject2.
Cannot Overload Operators in C++
Here’s a table that summarizes the operators that cannot be overloaded in C++ along with a short description of each:
| Operator | Description |
|---|---|
:: (Scope Resolution) |
Used to define the scope of a function, class, or variable (e.g., Class::member, namespace::function). Cannot be overloaded. |
. (Dot) |
Used for accessing members (functions or variables) of an object (e.g., obj.member). Cannot be overloaded. |
.* (Pointer-to-Member Access) |
Used to access members through a pointer to an object (e.g., obj.*member). Cannot be overloaded. |
->* (Pointer-to-Member Access) |
Similar to .*, used for accessing members through a pointer (e.g., ptr->*member). Cannot be overloaded. |
?: (Ternary Conditional) |
Used for conditional expressions (e.g., condition ? expr1 : expr2). Cannot be overloaded. |
sizeof |
Used to get the size of a variable or type (e.g., sizeof(int)). Cannot be overloaded. |
typeid |
Used for runtime type identification (e.g., typeid(obj)). Cannot be overloaded. |
new and delete |
Used for memory allocation (new) and deallocation (delete). Can be overloaded for a class, but not globally. |
, (Comma) |
Used to separate expressions (e.g., expr1, expr2). Technically overloadable, but not recommended due to potential confusion. |