8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
1/32
Object-Oriented Programming
(OOP)Lecture No. 16
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
2/32
Operator overloadingConsider the following class:
class Complex{
private:
double real, img;
public:
Complex Add(const Complex &);
Complex Subtract(const Complex &);
Complex Multiply(const Complex &);
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
3/32
Operator overloadingFunction implementation:
Complex Complex::Add(
const Complex & c1){Complex t;
t.real = real + c1.real;
t.img = img + c1.img;
return t;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
4/32
Operator overloadingThe following statement:
Complex c3 = c1.Add(c2);
Adds the contents of c2to c1and
assigns it to c3(copy constructor)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
5/32
Operator overloadingTo perform operations in asingle mathematical statement
e.g:c1+c2+c3+c4
We have to explicitly write:c1.Add(c2.Add(c3.Add(c4)))
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
6/32
Operator overloadingAlternative way is:
t1 = c3.Add(c4);
t2 = c2.Add(t1);
t3 = c1.Add(t2);
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
7/32
Operator overloadingIf the mathematical expression isbig:
Converting it to C++ code will involvecomplicated mixture of function calls
Less readable
Chances of human mistakes are veryhigh
Code produced is very hard to maintain
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
8/32
Operator overloadingC++ provides a very elegantsolution:
Operator overloading
C++ allows you to overloadcommon operators like +, -or *etc
Mathematical statements dont haveto be explicitly converted into functioncalls
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
9/32
Operator overloadingAssume that operator + hasbeenoverloaded
Actual C++ code becomes:
c1+c2+c3+c4
The resultant code is very easyto read, write and maintain
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
10/32
Operator overloadingC++ automatically overloadsoperators for pre-defined types
Example of predefined types:
int
float
double
char
long
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
11/32
Operator overloadingExample:
float x;
int y;
x = 102.02 + 0.09;Y = 50 + 47;
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
12/32
Operator overloadingThe compiler probably calls the correctoverloaded low level function for additioni.e:
// for integer addition:
Add(int a, int b)
// for float addition:
Add(float a, float b)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
13/32
Operator overloadingOperator functions are not usuallycalled directly
They are automatically invoked toevaluate the operations theyimplement
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
14/32
Operator overloadingList of operators that can beoverloaded in C++:
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
15/32
Operator overloadingList of operators that cant beoverloaded:
Reason: They take name, ratherthan value in their argument except
for ?:
?:is the only ternary operator inC++ and cant be overloaded
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
16/32
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
17/32
Operator overloadingAssociativity is NOTchangeddue to overloading
Following arithmetic expressionalways is evaluated from left to
right:c1 + c2 + c3 + c4
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
18/32
Operator overloadingUnary operators and assignmentoperator are right associative, e.g:
a=b=cis same as a=(b=c)
All other operators are leftassociative:
c1+c2+c3is same as
(c1+c2)+c3
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
19/32
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
20/32
Operator overloadingCreating a new operator is a syntax
error (whether unary, binary orternary)
You cannot create $
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
21/32
Operator overloadingArity of an operator is NOT affected
by overloadingExample:
Division operator will take exactly two operandsin any case:
b = c / d
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
22/32
inary operatorsBinary operators act on twoquantities
Binary operators:
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
23/32
inary operatorsGeneral syntax:
Member function:
TYPE1CLASS::operator B_OP(
TYPE2rhs){
...
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
24/32
inary operatorsGeneral syntax:
Non-member function:
TYPE1operator B_OP(TYPE2lhs,TYPE3rhs){
...
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
25/32
inary operatorsThe operator OP must have at
least one formal parameter of typeclass (user defined type)
Following is an error:int operator + (int, int);
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
26/32
inary operatorsOverloading + operator:
class Complex{
private:
double real, img;
public:
Complex operator +(const
Complex & rhs);
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
27/32
inary operatorsComplex Complex::operator +( const Complex & rhs){
Complex t;t.real = real + rhs.real;
t.img = img + rhs.img;
return t;}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
28/32
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
29/32
inary operatorsIf the return type was void,
class Complex{
...
public:
void operator+(
const Complex & rhs);
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
30/32
inary operatorsvoid Complex::operator+(const
Complex & rhs){
real = real + rhs.real;
img = img + rhs.img;
};
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
31/32
inary operatorswe have to do the same operationc1+c2+c3as:
c1+c2
c1+c3
// final result is stored in c1
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 16
32/32
inary operatorsDrawback of void return type:
Assignments and cascaded expressionsare not possible
Code is less readable
Debugging is tough
Code is very hard to maintain