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 for MyClass, you define how two MyClass objects should be added (by adding their value members).
  • After overloading the + operator, you can use it just like any built-in operator (such as adding integers), but it will now work with MyClass objects.

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
void input() {
cout << “Enter a value: “;
cin >> value;
}

// Function to display the value
void display() const {
cout << “Value: ” << value << endl;
}

// Overload the ‘+’ operator
MyClass operator+(MyClass ClassObject2) {
MyClass ClassObject3;
ClassObject3.value = value + ClassObject2 .value; // Add the values of the two objects
return ClassObject3;
}
};

int main() {
MyClass ClassObject1 , ClassObject2, ClassObject3 ;

// Input values for ClassObject1 and ClassObject2
cout << “Enter the value for ClassObject1 :\n”;
ClassObject1.input();

cout << “Enter the value for ClassObject2:\n”;
ClassObject2.input();

// Add aa and bb using overloaded + operator
ClassObject3 = ClassObject1 + ClassObject2;

// Display the result
cout << “Result of ClassObject1+ ClassObject2: “;
ClassObject3.display();

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 for MyClass objects.
  • ClassObject2 is 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 type MyClass after adding the two objects.

2. The Temporary Object ClassObject3:

MyClass ClassObject3;
  • We create a temporary MyClass object called ClassObject3. This object will hold the result of the addition of the value from the calling object (the one on the left of +) and ClassObject2 (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:
    • value refers to the value of the calling object (the object on the left-hand side of the + operator).
    • ClassObject2.value refers to the value of the second object passed to the function (the object on the right-hand side of the + operator).
  • The value of the calling object is added to the value of ClassObject2, and the result is stored in ClassObject3.value.

4. Returning the Result:

return ClassObject3;
  • After performing the addition, the operator+ function returns ClassObject3, which contains the result of the addition. This object is now a MyClass object with the sum of the value of the calling object and ClassObject2.

Summary of the Flow:

  • ClassObject1 + ClassObject2 invokes the overloaded + operator.
  • Inside operator+, the values of ClassObject1 and ClassObject2 are added together and stored in a temporary object ClassObject3.
  • The operator+ function returns ClassObject3, which is assigned to the ClassObject3 in the main() function.
  • The result is that ClassObject3 now holds the sum of the values of ClassObject1 and ClassObject2.

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.