With the help of operator overloading, you can redefine the majority of the C++ operators. You can also use operator overloading to perform different operations using one operator.
What is Operator Overloading?
Syntax
Different Approaches to Operator Overloading in C++
Can all C++ Operators be Overloaded?
Things to Remember:
Rules for Operator Overloading:
How to Overload Operator:
Syntax
To overload a C++ operator, you should define a special function inside the Class as follows: Here is an explanation for the above syntax:
The return_type is the return type for the function. Next, you mention the operator keyword. The symbol denotes the operator symbol to be overloaded. For example, +, -, <, ++. The argument(s) can be passed to the operator function in the same way as functions.
Example 1:
Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code to use its functions. Include the std namespace in our program to use its classes without calling it. Create a class named TestClass. Use the private access modifier, which marks a class member as privately accessible. Create an integer variable count. This variable will be privately accessible. Use the public access modifier, which marks a class member as privately accessible. Use a class constructor to initialize the variable counter to 5. Overload the meaning of the — operator. The operator will decrement the value of the variable x by 1. End of the operator overloading section. The operator has been given a new name. Defining a function named Display() function. Print the value of variable count alongside other text on the console when the Display() function is called. The } marks the end of the body of Display() function. End of the class body. Call the main() function. The program logic should be added within this function. Create an instance of the class TestClass and give it the name tc. This will call the void operator –() function. Use the stance of TestClass Class to call the Display() function. The function must return value upon successful completion. End of the body of the function main().
Different Approaches to Operator Overloading in C++
You can perform operator overloading by implementing any of the following types of functions:
Member Function Non-Member Function Friend Function
The operator overloading function may be a member function when a Left operand is an object of the Class. When the Left operand is different, the Operator overloading function should be a non-member function.
You can make the operator overloading function a friend function if it needs to access the private and protected class members.
Can all C++ Operators be Overloaded?
No. There are C++ operators that can’t be overloaded. They include:
:: -Scope resolution operator ?: -ternary operator. . -member selector Sizeof operator
- -member pointer selector
Things to Remember:
With operator overloading, you can redefine the way an operator works only for the user-defined types (objects, structures). You cannot use it for built-in types (float, char, int, etc.). The = and & C++ operators are overloaded by default. For example, you can copy the objects of the same Class directly using the = operator. Operator precedence doesn’t change the associatively and precedence of operators. However, you can change the order of evaluation using parenthesis. There are four operators that you cannot overload in C++. They include the scope resolution operator (::), member selection operator (.), member selection through a pointer to function operator (.*), and the ternary operator (?:).
Rules for Operator Overloading:
Here are rules for Operator Overloading:
For it to work, at least one operand must be a user-defined class object. You can only overload existing operators. You can’t overload new operators. Some operators cannot be overloaded using a friend function. However, such operators can be overloaded using member function.
How to Overload Operator:
Example 1:
Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in our code to use its functions. Include the std namespace in our program to use its classes without calling it. Create a class named OperatorOverload. Use the private access modifier, which marks a class member as privately accessible. Create an integer variable x. This variable will be privately accessible. Use the public access modifier, which marks a class member as privately accessible. Use a class constructor to initialize variable x to 10. Overload the meaning of the ++ operator. The operator will increment the value of variable x with 2. End of the operator overloading section. The operator has been given a new name. Calling the Print() function. Print the value of variable x alongside other text on the console when the Print() function is called. End of the body of the Print() function. End of the class body. Call the main() function. The program logic should be added within this function. Create an instance of the OperatorOverload Class named ov. This will call the void operator ++() function. Use the stance of OperatorOverload class to call the Print() function. The function must return value upon successful completion. End of the body of the function main().
Example 2:
Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file into our program in order to use its functions. Include the std namespace into our program in order to use its classes without calling it. Create a class named TestClass. The { marks the beginning of the class body. Use the private access modifier to mark variables as private, meaning they can only be accessed from within the Class. Define two integer variables, real and over. Use the public access modifier to mark the constructor as public, meaning that it will be accessible even outside the Class. Creating the class constructor and initializing the variables. Initialize the value of variable real. Initialize the value of the variable over. End of the constructor body. Override the meaning of the + operator. Create the data type result of type TestClass. Use the + operator with complex numbers. This line will add the real part of a number to the real part of another number. Use the + operator with complex numbers. This line will add the imaginary part of a number to the imaginary part of another number. The program will return the value of the variable result upon successful execution. End of the definition of the new meaning of + operator, that is, overloading. Call the print() method. Print the new complex number after addition on the console. End of the body of print() function. End of the body of TestClass Class. Calling the main() function. Passing the values of both real and complex parts to be added. The first part of c1 will be added to the first part of c2, that is, 9+4. The second part of c1 will be added to the second part of c, that is, 5+3. Performing an operation using the overloaded + operator and storing the result in variable c3. Printing the value of variable c3 on the console. End of the body of main() function.
Summary:
You can specify more than one meaning for a C++ operator in one scope. This is called operator overloading. Operator overloading provides a special meaning of an operator for a user-defined data type. You can redefine the majority of C++ operators through operator overloading. Not all C++ operators can be overloaded. For an operator to be overloaded, at least one of the operands must be a user-defined object. Only existing operators can be overloaded. You cannot overload new operators.