C++ Operator Overloading - Learn C++ - C++ Tutorial - C++ programming
Learn c++ - c++ tutorial - operator-overloading-in-c++ - c++ examples - c++ programs
Operator Overloading in C++
- In programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments.
- Operator overloading is generally defined by a programming language, a programmer, or both.
- This feature in C++ programming that allows programmer to redefine the meaning of an operator (when they operate on class objects) is known as operator overloading.
learn c++ tutorials - operator overloading in c++ Example
Learn C++ , C++ Tutorial , C++ programming - C++ Language -Cplusplus
Why is operator overloading used?
- You can write any C++ program without the knowledge of operator overloading. However, operator operating is profoundly used by programmers to make program intuitive. For example,
- You can replace the code like:
- To
learn c++ tutorials - operator overloading in c++ Example
How to overload operators in C++ programming?
- To overload an operator, a special operator function is defined inside the class as:
- Here, returnType is the return type of the function.
- The returnType of the function is followed by operator keyword.
- Symbol is the operator symbol you want to overload. Like: +, <, -, ++
- You can pass arguments to the operator function in similar way as functions.
Example: Operator overloading in C++ Programming
Output
- This function is called when ++ operator operates on the object of Test class (object t in this case).
- In the program,void operator ++ () operator function is defined (inside Test class).
- This function increments the value of count by 1 for t object.
learn c++ tutorials - operator overloading in c++ Example