Operator overloading allows existing operators like + and - to be used with user-defined types by associating them with member functions. There are unary operators that operate on one operand and binary operators that operate on two operands. Common operators that can be overloaded include arithmetic, comparison, compound assignment, and increment/decrement. Overloading is done by defining member functions that match the operator's syntax and semantics.
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Operator Overloading
1.
2. Operator Overloading- It is one of the interesting
and useful feature of OOP.
Definition - The ability to associate an existing operator
with a member function and use it with objects of its
classes as its operand is called operator overloading.
Format of the member function using the operator keyword:
return_ type operator op (arguments/parameters)
Description of operator overloading-
Urinary operators.
Binary operators.
o Arithmetic operators
o Compound assignment operators
o Comparison operators.
3. Unary operator-
The unary operators operate on a single operand
and following are the examples of Unary
operators:
The increment (++) and decrement (--) operator.
The unary minus (-) operator.
The logical not (!) operator.
The unary operators operate on the object for
which they were called and normally, this operator
appears on the left side of the object, as in ‘!’obj ,
‘-’obj , and ‘++’obj but sometime they can be
used as postfix as well like obj ‘++’ or obj ‘--’ .
4. /* Simple example to demonstrate the working of operator overloading*/
#include <iostream>
using namespace std;
class temp
{
private:
int count;
public:
temp():count(5) {}
void operator +()
{ count=count*2; }
void Display()
{
cout<<"Count: "<<count;
}}
;
int main()
{
temp t;
+t; /* operator function void operator +() is called */
t.Display();
return 0;
}
12. Rules for overloading operator-
It looks simple to redefine the operators,
but
there are some restriction in over loading
them.
1. Only existing operators can be
overloaded.
2. One operand must be of user defined
type.
3. Syntax rule is same for both original
operators and overloaded operators.
4. Some operators cannot be overloaded.
5. Friend function cannot be used to
13. 6. Unary operator, overloaded by the member
function take no explicit argument and return no
value. however friend function require one
argument
7. Binary operators require one argument and
those which are overloaded through a friend
function require two arguments.
8. When using a binary operator overloaded
through the member function, the left hand
operator must be an object of the relevant class.
9. Binary operators such as +,-,* and / must
return a value. They must not attempt to change
there own argument.