SlideShare a Scribd company logo
CSC241:
Object Oriented
Programming
Spring 2013
1. Operator Overloading
Please turn OFF your Mobile Phones!
Tuesday, July 4, 2023
Farhan Aadil
July
4,
2023
COMSATS
Intitute
of
Information
Technology
1
Operator overloading
►Consider the following class:
class Complex{
private:
double real, img;
public:
Complex Add(const Complex &);
Complex Subtract(const Complex &);
Complex Multiply(const Complex &);
…
}
July
4,
2023
COMSATS
Intitute
of
Information
Technology
2
Operator overloading
►Function implementation:
Complex Complex::Add(
const Complex & c1){
Complex t;
t.real = real + c1.real;
t.img = img + c1.img;
return t;
}
July
4,
2023
COMSATS
Intitute
of
Information
Technology
3
Operator overloading
►The following statement:
Complex c3 = c1.Add(c2);
Adds the contents of c2 to c1 and assigns
it to c3 (copy constructor)
July
4,
2023
COMSATS
Intitute
of
Information
Technology
4
Operator overloading
►To perform operations in a single
mathematical statement e.g:
c1+c2+c3+c4
►We have to explicitly write:
►c1.Add(c2.Add(c3.Add(c4)))
July
4,
2023
COMSATS
Intitute
of
Information
Technology
5
Operator overloading
►Alternative way is:
► t1 = c3.Add(c4);
► t2 = c2.Add(t1);
► t3 = c1.Add(t2);
July
4,
2023
COMSATS
Intitute
of
Information
Technology
6
Operator overloading
►If the mathematical expression is big:
• Converting it to C++ code will involve
complicated mixture of function calls
• Less readable
• Chances of human mistakes are very high
• Code produced is very hard to maintain
July
4,
2023
COMSATS
Intitute
of
Information
Technology
7
Operator overloading
►C++ provides a very elegant solution:
►“Operator overloading”
►C++ allows you to overload common operators like +, - or * etc…
►Mathematical statements don’t have to be explicitly converted
into function calls
July
4,
2023
COMSATS
Intitute
of
Information
Technology
8
Operator overloading
►Assume that operator + has been
overloaded
►Actual C++ code becomes:
c1+c2+c3+c4
►The resultant code is very easy to read,
write and maintain
July
4,
2023
COMSATS
Institute
of
Information
Technology
9
Operator overloading
►C++ automatically overloads operators for
pre-defined types
►Example of predefined types:
int
float
double
char
long
July
4,
2023
COMSATS
Intitute
of
Information
Technology
10
Operator overloading
►Example:
float x;
int y;
x = 102.02 + 0.09;
Y = 50 + 47;
July
4,
2023
COMSATS
Intitute
of
Information
Technology
11
Operator overloading
►The compiler probably calls the correct
overloaded low level function for addition i.e:
// for integer addition:
Add(int a, int b)
// for float addition:
Add(float a, float b)
July
4,
2023
COMSATS
Intitute
of
Information
Technology
12
Operator overloading
►Operator functions are not usually called
directly
►They are automatically invoked to evaluate
the operations they implement
July
4,
2023
COMSATS
Institute
of
Information
Technology
13
Operator overloading
►List of operators that can be
overloaded in C++:
July
4,
2023
COMSATS
Intitute
of
Information
Technology
14
Operator overloading
►List of operators that can’t be
overloaded:
►Reason: They take name, rather than
value in their argument except for ?:
►?: is the only ternary operator in C++
and can’t be overloaded
July
4,
2023
COMSATS
Intitute
of
Information
Technology
15
Operator overloading
►The precedence of an operator is NOT
affected due to overloading
►Example:
c1*c2+c3
c3+c2*c1
both yield the same answer
July
4,
2023
COMSATS
Intitute
of
Information
Technology
16
Operator overloading
►Associativity is NOT changed
due to overloading
►Following arithmetic
expression always is evaluated
from left to right:
c1 + c2 + c3 + c4
July
4,
2023
COMSATS
Intitute
of
Information
Technology
17
Operator overloading
►Unary operators and assignment
operator are right associative, e.g:
a=b=c is same as a=(b=c)
►All other operators are left
associative:
c1+c2+c3 is same as
(c1+c2)+c3
July
4,
2023
COMSATS
Intitute
of
Information
Technology
18
Operator overloading
►Always write code representing
the operator
►Example:
Adding subtraction code
inside the + operator
will create chaos
July
4,
2023
COMSATS
Intitute
of
Information
Technology
19
Operator overloading
►Creating a new operator is a
syntax error (whether unary, binary
or ternary)
► You cannot create $
July
4,
2023
COMSATS
Intitute
of
Information
Technology
20
Operator overloading
►Arity of an operator is NOT affected by
overloading
►Example:
Division operator will take
exactly two operands in any
case:
b = c / d
July
4,
2023
COMSATS
Intitute
of
Information
Technology
21
Binary operators
►Binary operators act on two quantities
►Binary operators:
July
4,
2023
COMSATS
Intitute
of
Information
Technology
22
Binary operators
►General syntax:
Member function:
TYPE1 CLASS::operator B_OP(
TYPE2 rhs){
...
}
July
4,
2023
COMSATS
Intitute
of
Information
Technology
23
Binary operators
►General syntax:
Non-member function:
TYPE1 operator B_OP(TYPE2 lhs,
TYPE3 rhs){
...
}
July
4,
2023
COMSATS
Intitute
of
Information
Technology
24
Binary operators
►The “operator OP” must have at least
one formal parameter of type class (user
defined type)
►Following is an error:
int operator + (int, int);
July
4,
2023
COMSATS
Intitute
of
Information
Technology
25
Binary operators
►Overloading + operator:
class Complex{
private:
double real, img;
public:
…
Complex operator +(const
Complex & rhs);
};
July
4,
2023
COMSATS
Institute
of
Information
Technology
26
Binary operators
Complex Complex::operator +(
const Complex & rhs){
Complex t;
t.real = real + rhs.real;
t.img = img + rhs.img;
return t;
}
July
4,
2023
COMSATS
Intitute
of
Information
Technology
27
Binary operators
►The return type is Complex so as to facilitate complex
statements like:
Complex t = c1 + c2 + c3;
►The above statement is automatically converted by
the compiler into appropriate function calls:
(c1.operator +(c2)).operator +(c3);
July
4,
2023
COMSATS
Intitute
of
Information
Technology
28
Binary operators
►If the return type was void,
class Complex{
...
public:
void operator+(const Complex & rhs);
};
July
4,
2023
COMSATS
Intitute
of
Information
Technology
29
Binary operators
void Complex::operator+(const
Complex & rhs){
real = real + rhs.real;
img = img + rhs.img;
};
July
4,
2023
COMSATS
Intitute
of
Information
Technology
30
Binary operators
►we have to do the same operation c1+c2+c3 as:
c1+c2
c1+c3
// final result is stored in c1
July
4,
2023
COMSATS
Intitute
of
Information
Technology
31
Binary operators
►Drawback of void return type:
Assignments and cascaded expressions are not possible
Code is less readable
Debugging is tough
Code is very hard to maintain
July
4,
2023
COMSATS
Intitute
of
Information
Technology
32
References
• “Object Oriented Programming in C++”, by “Robert Lafore”,
published by Sams Publishing (The Waite Group). 4th ed. available
in soft form.
• “Object Oriented Programming Using C++” by “Joyce Farrell” ,
published by Course Technology, Cengage Learning. 4th ed. available
in soft form
• National University of Computer & Emerging Sciences
[www.nu.edu.pk]
• Virtual University of Pakistan [ocw.vu.edu.pk]
• Open Courseware Consortium
[http://www.ocwconsortium.org/en/courses]
July
4,
2023
COMSATS
Intitute
of
Information
Technology
33
Thanks
July
4,
2023
COMSATS
Intitute
of
Information
Technology
34

More Related Content

Similar to Lecture 14.pptx

M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
NabeelaNousheen
 
Overloading
OverloadingOverloading
Overloading
poonamchopra7975
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 
CAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptxCAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptx
Surajgroupsvideo
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
esuEthopi
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
Saket Pathak
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
pratikbakane
 
I really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdfI really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdf
wasemanivytreenrco51
 
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
AKSHAY SACHAN
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
Docent Education
 
Implementing Physical Units Library for C++
Implementing Physical Units Library for C++Implementing Physical Units Library for C++
Implementing Physical Units Library for C++
Mateusz Pusz
 
COW
COWCOW
operator overloading
operator overloadingoperator overloading
operator overloading
Nishant Joshi
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloading
yatinnarula
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
Rai University
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
bunnykhan
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
Rai University
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
zindadili
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
IIUM
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
IIUM
 

Similar to Lecture 14.pptx (20)

M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
 
Overloading
OverloadingOverloading
Overloading
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
CAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptxCAP444-Unit-3-Polymorphism.pptx
CAP444-Unit-3-Polymorphism.pptx
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
I really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdfI really need help with my C++ assignment. The following is the info.pdf
I really need help with my C++ assignment. The following is the info.pdf
 
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Implementing Physical Units Library for C++
Implementing Physical Units Library for C++Implementing Physical Units Library for C++
Implementing Physical Units Library for C++
 
COW
COWCOW
COW
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloading
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
Lecture 5 Inheritance
Lecture 5 InheritanceLecture 5 Inheritance
Lecture 5 Inheritance
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 

Recently uploaded

ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 

Recently uploaded (20)

ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 

Lecture 14.pptx

  • 1. CSC241: Object Oriented Programming Spring 2013 1. Operator Overloading Please turn OFF your Mobile Phones! Tuesday, July 4, 2023 Farhan Aadil July 4, 2023 COMSATS Intitute of Information Technology 1
  • 2. Operator overloading ►Consider the following class: class Complex{ private: double real, img; public: Complex Add(const Complex &); Complex Subtract(const Complex &); Complex Multiply(const Complex &); … } July 4, 2023 COMSATS Intitute of Information Technology 2
  • 3. Operator overloading ►Function implementation: Complex Complex::Add( const Complex & c1){ Complex t; t.real = real + c1.real; t.img = img + c1.img; return t; } July 4, 2023 COMSATS Intitute of Information Technology 3
  • 4. Operator overloading ►The following statement: Complex c3 = c1.Add(c2); Adds the contents of c2 to c1 and assigns it to c3 (copy constructor) July 4, 2023 COMSATS Intitute of Information Technology 4
  • 5. Operator overloading ►To perform operations in a single mathematical statement e.g: c1+c2+c3+c4 ►We have to explicitly write: ►c1.Add(c2.Add(c3.Add(c4))) July 4, 2023 COMSATS Intitute of Information Technology 5
  • 6. Operator overloading ►Alternative way is: ► t1 = c3.Add(c4); ► t2 = c2.Add(t1); ► t3 = c1.Add(t2); July 4, 2023 COMSATS Intitute of Information Technology 6
  • 7. Operator overloading ►If the mathematical expression is big: • Converting it to C++ code will involve complicated mixture of function calls • Less readable • Chances of human mistakes are very high • Code produced is very hard to maintain July 4, 2023 COMSATS Intitute of Information Technology 7
  • 8. Operator overloading ►C++ provides a very elegant solution: ►“Operator overloading” ►C++ allows you to overload common operators like +, - or * etc… ►Mathematical statements don’t have to be explicitly converted into function calls July 4, 2023 COMSATS Intitute of Information Technology 8
  • 9. Operator overloading ►Assume that operator + has been overloaded ►Actual C++ code becomes: c1+c2+c3+c4 ►The resultant code is very easy to read, write and maintain July 4, 2023 COMSATS Institute of Information Technology 9
  • 10. Operator overloading ►C++ automatically overloads operators for pre-defined types ►Example of predefined types: int float double char long July 4, 2023 COMSATS Intitute of Information Technology 10
  • 11. Operator overloading ►Example: float x; int y; x = 102.02 + 0.09; Y = 50 + 47; July 4, 2023 COMSATS Intitute of Information Technology 11
  • 12. Operator overloading ►The compiler probably calls the correct overloaded low level function for addition i.e: // for integer addition: Add(int a, int b) // for float addition: Add(float a, float b) July 4, 2023 COMSATS Intitute of Information Technology 12
  • 13. Operator overloading ►Operator functions are not usually called directly ►They are automatically invoked to evaluate the operations they implement July 4, 2023 COMSATS Institute of Information Technology 13
  • 14. Operator overloading ►List of operators that can be overloaded in C++: July 4, 2023 COMSATS Intitute of Information Technology 14
  • 15. Operator overloading ►List of operators that can’t be overloaded: ►Reason: They take name, rather than value in their argument except for ?: ►?: is the only ternary operator in C++ and can’t be overloaded July 4, 2023 COMSATS Intitute of Information Technology 15
  • 16. Operator overloading ►The precedence of an operator is NOT affected due to overloading ►Example: c1*c2+c3 c3+c2*c1 both yield the same answer July 4, 2023 COMSATS Intitute of Information Technology 16
  • 17. Operator overloading ►Associativity is NOT changed due to overloading ►Following arithmetic expression always is evaluated from left to right: c1 + c2 + c3 + c4 July 4, 2023 COMSATS Intitute of Information Technology 17
  • 18. Operator overloading ►Unary operators and assignment operator are right associative, e.g: a=b=c is same as a=(b=c) ►All other operators are left associative: c1+c2+c3 is same as (c1+c2)+c3 July 4, 2023 COMSATS Intitute of Information Technology 18
  • 19. Operator overloading ►Always write code representing the operator ►Example: Adding subtraction code inside the + operator will create chaos July 4, 2023 COMSATS Intitute of Information Technology 19
  • 20. Operator overloading ►Creating a new operator is a syntax error (whether unary, binary or ternary) ► You cannot create $ July 4, 2023 COMSATS Intitute of Information Technology 20
  • 21. Operator overloading ►Arity of an operator is NOT affected by overloading ►Example: Division operator will take exactly two operands in any case: b = c / d July 4, 2023 COMSATS Intitute of Information Technology 21
  • 22. Binary operators ►Binary operators act on two quantities ►Binary operators: July 4, 2023 COMSATS Intitute of Information Technology 22
  • 23. Binary operators ►General syntax: Member function: TYPE1 CLASS::operator B_OP( TYPE2 rhs){ ... } July 4, 2023 COMSATS Intitute of Information Technology 23
  • 24. Binary operators ►General syntax: Non-member function: TYPE1 operator B_OP(TYPE2 lhs, TYPE3 rhs){ ... } July 4, 2023 COMSATS Intitute of Information Technology 24
  • 25. Binary operators ►The “operator OP” must have at least one formal parameter of type class (user defined type) ►Following is an error: int operator + (int, int); July 4, 2023 COMSATS Intitute of Information Technology 25
  • 26. Binary operators ►Overloading + operator: class Complex{ private: double real, img; public: … Complex operator +(const Complex & rhs); }; July 4, 2023 COMSATS Institute of Information Technology 26
  • 27. Binary operators Complex Complex::operator +( const Complex & rhs){ Complex t; t.real = real + rhs.real; t.img = img + rhs.img; return t; } July 4, 2023 COMSATS Intitute of Information Technology 27
  • 28. Binary operators ►The return type is Complex so as to facilitate complex statements like: Complex t = c1 + c2 + c3; ►The above statement is automatically converted by the compiler into appropriate function calls: (c1.operator +(c2)).operator +(c3); July 4, 2023 COMSATS Intitute of Information Technology 28
  • 29. Binary operators ►If the return type was void, class Complex{ ... public: void operator+(const Complex & rhs); }; July 4, 2023 COMSATS Intitute of Information Technology 29
  • 30. Binary operators void Complex::operator+(const Complex & rhs){ real = real + rhs.real; img = img + rhs.img; }; July 4, 2023 COMSATS Intitute of Information Technology 30
  • 31. Binary operators ►we have to do the same operation c1+c2+c3 as: c1+c2 c1+c3 // final result is stored in c1 July 4, 2023 COMSATS Intitute of Information Technology 31
  • 32. Binary operators ►Drawback of void return type: Assignments and cascaded expressions are not possible Code is less readable Debugging is tough Code is very hard to maintain July 4, 2023 COMSATS Intitute of Information Technology 32
  • 33. References • “Object Oriented Programming in C++”, by “Robert Lafore”, published by Sams Publishing (The Waite Group). 4th ed. available in soft form. • “Object Oriented Programming Using C++” by “Joyce Farrell” , published by Course Technology, Cengage Learning. 4th ed. available in soft form • National University of Computer & Emerging Sciences [www.nu.edu.pk] • Virtual University of Pakistan [ocw.vu.edu.pk] • Open Courseware Consortium [http://www.ocwconsortium.org/en/courses] July 4, 2023 COMSATS Intitute of Information Technology 33