operator overloading in oop
Operator Overloading
In C++, operators are already defined to add integer and float values. When we want to use these operators with objects or user-defined data types, it's called operator overloading.
For example, if we have two integer variables 'n' and 'm', we can perform the addition operation using the '+' operator like 'n + m'. Similarly, if we have two objects, 'object1' and 'object2', we can use the '+' operator to perform addition on these objects like 'object1 + object2'.
we can say operator overloading provide additional meaning to this operator +, *, <=, and +=, when they are applied to user-defined data types.
In operator overloading, we use different operators with user-defined data types.
like function overloading, where we used the same name and passed different arguments and data types etc.
It takes complicated code and makes it easier to understand. For instance, instead of writing confusing lines like
d3.addobjects(d1, d2) or d3 = d1.addobjects(d2),
we can use the "+" symbol to add objects together,
like "d3 = d1 + d2".
It makes the code much simpler and clearer.
For a unary operator (like '+'), the method that handles it should have no parameters because it only works on the object itself. But for a binary operator (like '+'), you only need one parameter because the current object will be on the left side of the operator, and the method parameter will be the item on the right side
Overloading Unary Operators
Unary operators work with just one operand, a variable the operator does something to. In unary operators, examples like the increment (++) and decrement (--) operators are used.
I have a class containing an integer variable 'c', and I want to increment its value. So, I create a method and call it using an object, like 'object.incrementC();'. However, this seems ambiguous. If I used 'object++' instead, it would be easier to understand.
complete code unary operator example
The operator Keyword
How does operator overloading work?
void operator ++ ()
In this prototype, there is no return type specified, followed by the keyword operator then the ++ operator, and finally, parentheses for parameters, but in this example, they are empty.
When we use the ++ operator, the compiler invokes this method.
In function overloading, the compiler differentiates functions by data types and the number of arguments. Similarly, in operator overloading, the compiler checks the data type of their operands. When an operand is a basic type, i.e., int, the compiler calls the built-in method. Otherwise, if the operand is an object, the compiler uses the user-defined operator++().
Operator Return Values
Task: write a program to use return value instead of void.
Nameless Temporary Objects
To provide a return value, we create a temporary object of the current class and return that temp.
Counter temp; // make a temporary Counter object
temp.c= c; // give it the same value as this object
return temp; // return it
instead of this, there is another convenient way to return the temp object, using the unnamed temporary object.
Counter operator ++ () //increment count
{
++c; // increment c, then return
return Counter(c); // an unnamed temporary object
} // initialized to this count
Postfix Notation
We've only used the increment operator in its prefix form ++c1
.
But what about postfix, where the variable is incremented after its value is used in the expression?
That's where c1++
comes into play.
Complete example of prefix and postfix
For post-fix notation, the function prototype is like this.
"Counter operator ++ (int)".
Inside the parenthesis, the int is not an argument of this function but only an indicator of post-fix to the compiler.
Overloading Binary Operators
A binary operator overloading happens when you have an operation that requires two operands, like adding or subtracting. It's like when you add two numbers together using the '+' sign - that's a binary operator doing its job.
Instead of writing out obj3.add(obj1,obj2);, we can simplify things by overloading the '+' operator. This way, we can just write obj3 = obj1 + obj2;, making the code much cleaner and easier to understand. let's understand the following example.
In the expression obj1+ obj2
, obj1
is the object that owns the operator, while dist2
is the object provided as an argument to the operator. After performing its task, the operator produces a value that can be assigned to a variable or used in other expressions.
In the operator+() function, the left operand is accessed directly—since this is the object of which the operator is a member—using 'x' and 'y'. The right operand is accessed as the function’s argument, as obj2.x and obj2.y.
Comparison Operators
bool operator<();
bool operator==();
Arithmetic Assignment Operators
+= operator
This operator combines assignment and addition into one step.
Task: write code for this obj1=obj2+=obj3; obj2(3), obj3(5)
Concatenating Strings
In C++, you can define your own string class and overload the + operator to concatenate strings like you would with built-in types. This allows you to concatenate strings using a syntax similar to built-in types, making your code more intuitive and readable.
Comparison of two string object
When we talk about comparing strings, it's like asking if two words are the same or not. Just like we compare numbers to see if they're equal, we can also compare strings.
In programming, we can compare strings too. Imagine we have our own special way of representing words called "String objects". Now, we want to see if two of these special word representations are the same or not.
We can do this by using something called the "equal to" (==) operator. Normally, this operator is used for numbers to see if they're the same. But we can also teach our program to use it with our special words.
So, in simple words, when we compare two String objects using the == operator, we're asking the computer if these two special words are exactly the same or not. If they are the same, it will say "yes", otherwise, it will say "no". That's all there is to comparing strings in programming!
Subscript Operator ([])
putel()
for inserting a value into the array and getel()
for retrieving the value of an array element. Both functions verify that the provided index number is within valid bounds.Single access() Function Returning by Reference
Interestingly, we can use a single-member function for both inserting data into the safe array and reading it. The key is to return the value by reference. This allows us to use the function on the left side of an assignment statement, so the value on the right side gets assigned to the variable returned by the function.
Overloaded [] Operator Returning by Reference
To access the safe array using the same subscript ([]
) operator as normal C++ arrays, we overload the subscript operator in the safearray
class. Since this operator is often used on the left side of an assignment, the overloaded function must return by reference.
Not All Operators Can Be Overloaded
Can not overload this operator: dot operator (.), scope resolution operator (::), and conditional operator (?:). Last but not least pointer-to-member operator (->).
The existing operator can overloaded, and not create a new operator to overload like this (*&).
Post a Comment