Upload
arhum-siddiqui
View
221
Download
0
Embed Size (px)
Citation preview
8/8/2019 C++.Session 10
1/13
TITLE
C++ & OOPS
SESSION 10
8/8/2019 C++.Session 10
2/13
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 isapplied to basic types. Provides the new definitions for most of the
C++ operators .
8/8/2019 C++.Session 10
3/13
We can overload all the C++ operatorsexcept
Class member access operators ( . , .* ).
Scope resolution operator ( : : )
Sizeof operator ( sizeof)
Conditional operator ( ? : )
8/8/2019 C++.Session 10
4/13
DEFINING OPERATOR OVERLOADINGReturn type class name :: operatorop (arg
list )
{ function body
}
return type is the type of the value returned
by the function
8/8/2019 C++.Session 10
5/13
op is the operator being overloaded.The op is preceded by the key word
operator
operatorop is the function name
Operator function must be either
member function or friend function
8/8/2019 C++.Session 10
6/13
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.
8/8/2019 C++.Session 10
7/13
That can not be overriddenWhen binary operators overloaded through
member function, the left hand operand
must be an object of the relevant class.
8/8/2019 C++.Session 10
8/13
They follow the syntax rules of originaloperators.
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 avalue.
They must not attempt to change their own
arguments.
8/8/2019 C++.Session 10
9/13
OVERLOADING UNARY OPERATOR
A unary operator takes just one operand.
Overloading unary minus operator.
A unary minus operator changes the signof an operand when applied to a basic
data type.
It should change the sign of data memberswhen applied to an object.
The function directly accesses the
members of the object.
8/8/2019 C++.Session 10
10/13
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 ;
8/8/2019 C++.Session 10
11/13
OVERLOADING BINARY OPERATORA binary operator takes two operands
Function overloading binary operator will
have one explicit argument.Ex: overloading binary + operator
testoperator +(testobj);
If test is name of the class and obj1 ,obj2
and obj3 are the objects oftestthen the expression
8/8/2019 C++.Session 10
12/13
obj3 = obj1 + obj2; invokes the functionoverloading 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.
8/8/2019 C++.Session 10
13/13
CONCLUSION
What we have learnt this session?Overloading unary and binary operators
using member function.
What we will discuss next session?Type conversions.