Operator Overloading C++ Chapter-5 BE 3rd Sem - Bikal Adhikati

Operator Overloading C++ Chapter-5 BE 3rd Sem - Bikal Adhikati

Operator Overloading  C++  Chapter-5 BE 3rd Sem - Bikal Adhikati


 


CHAPTER OUTLINE

CHAPTER FIVE

OPERATOR OVERLOADING

-5 HOURS

5.1 Overloadable Operators
5.2 Syntax of Operator Overloading
5.3 Rules of Operator Overloading
5.4 Unary Operator Overloading
5.5 Binary Operator Overloading
5.6 Operator Overloading with Member and Non Member Functions
5.7 Data Conversion: Basic – User Defined and User Defined – User

Defined

5.8 Explicit Constructors

3

Review of some

concepts learnt in C

4

Types of operator based on no. of operands
Unary Operator:
Takes only one operand.
Examples: ++ -- unary + unary - &

Binary Operator:
Takes two operands.
Examples: + - * /

Ternary Operator:
Takes three operands.
Example: Conditional operator(?:)

5

Increment and decrement operator

Isolated Form:-
int a = 5; int a = 5;
++a; //pre-increment or prefix a++; //post-increment or postfix
Effect: Effect:
a←a+1 a←a+1
Value of a = 6 Value of a = 6

Mixed Form:-
int a = 5,b; int a = 5,b;
b=++a; //pre-increment or prefix b=a++; //post-increment or postfix
Effect: Effect:
a←a+1 b←a
b←a a←a+1
Value of a = 6 Value of a = 6
Value of b = 6 Value of b = 5

6

Introduction

Operator overloading is a feature of C++ in which we can extend the functionalities
of the existing operator for user defined data types. It is a type of polymorphism.
For example:

int a=5,b=3,c;
c=a+b;

In the above example, the binary addition operator(+) can perform the addition
operation on the operands a and b to yield the value of expression (a+b) which is
then assigned to c.
Suppose we have declared three objects of type complex as:

complex c1,c2,c3;
c3=c1+c2;

Since c1 and c2 are the objects of complex type, the compiler could not determine
how the binary addition operator(+) would operate on these objects. In such situation
we can extend the function of the operator (+) using operator overloading for adding
complex objects c1 and c2. Since the same + can be used for adding basic data types
as well as user defined data types: object, operator overloadingis also a type of
polymorphism.

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel

Copyright © 2020