Title: C++ & Oops

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

TITLE

C++ & OOPS

SESSION 10
OPERATOR OVERLOADING
 The mechanism of giving special meanings to
an operator is known as Operator Overloading.
 This is one of the exciting features of C++.
 This technique has enhanced the power of
extensibility of C++.
 This technique permits to add two user –
defined variables with the same syntax that is
applied to basic types.
 Provides the new definitions for most of the C+
+ operators .
We can overload all the C++ operators
except
Class member access operators ( . , .* ).
Scope resolution operator ( : : )
Sizeof operator ( sizeof )
Conditional operator ( ? : )
DEFINING OPERATOR OVERLOADING
Return type class name :: operator op (arg –
list )
{
function body
}
return type is the type of the value returned
by the function
op is the operator being overloaded.
The op is preceded by the key word
operator
operator op is the function name
Operator function must be either
member function or friend function
RULES FOR OVERLOADING OPERATORS

Only existing operators can be overloaded


The operator must have at least one
operand of user – defined type.
New operators cannot be created.
We cannot change the basic meaning of
an operator.
That can not be overridden
When binary operators overloaded through
member function, the left hand operand
must be an object of the relevant class.
They follow the syntax rules of original
operators.
Some operators cannot be overloaded.
Unary operators overloaded by a member
function takes no explicit arguments.
Binary operators overloaded by a member
function takes one explicit argument.
Binary operators must explicitly return a
value.
They must not attempt to change their own
arguments.
OVERLOADING UNARY OPERATOR
A unary operator takes just one operand.
Overloading unary minus operator.
A unary minus operator changes the sign
of an operand when applied to a basic
data type.
It should change the sign of data members
when applied to an object.
The function directly accesses the
members of the object.
Ex: class test
{ int x,y;
public:
void getdata(); void operator-();
void display (); };
//defining the function overloading
operator – void test :: operator-()
{ x = -x ; y = -y ; }
if S is an object of class test,
the statement -S;
changes the sign of data members of S ;
OVERLOADING BINARY OPERATOR
A binary operator takes two operands
Function overloading binary operator will
have one explicit argument.
Ex: overloading binary + operator
test operator +(test obj );
If test is name of the class and obj1 ,obj2
and obj3 are the objects of test
then the expression
obj3 = obj1 + obj2; invokes the function
overloading binary + operator.
obj1 invokes the function and obj2 will be
passed as an argument.
The function returns the sum of obj1 and
obj2 and is assigned to obj3.
CONCLUSION

What we have learnt this session?


Overloading unary and binary operators
using member function.
What we will discuss next session?
Type conversions.

You might also like