SlideShare a Scribd company logo
Operator Overloading
Data abstraction
 Data abstraction refers to, providing only essential
information to the outside world and hiding their
background details.
 Data abstraction is a programming (and design)
technique that relies on the separation of interface
and implementation.
 C++ classes provides great level of data
abstraction.
Operator Overloading
 C++ allows you to specify more than one definition
for an operator in the same scope, which is called
operator overloading .
 Operators may not look like functions but can hide
function invocations.
 You cannot overload the meaning of operators if all
arguments are primitive data types, nor can you change
the precedence or associativity of operators.
Over loadable Operators in C++
+ - * / %
^ & | ~ ! &&
|| ++ -- << >> ,
< <= == != > >=
= += -= *= /= %=
&= |= ^= <<= >>=
[] () -> new delete
Operator Overloading
 It simplifies the program
 d3.addobject(d1,d2)
 Or the similar but equally obscure
 d3=d1.addobject(d2)
 Can be changed into much readable form
 d3=d1+d2; (legal when d1, d2, d3 are of basic
types)
Simple Example
class complex {
double real, imag;
public:
complex(double r, double i) :
real(r), imag(i) {}
}
 Would like to be able to write:–
complex a = complex(1, 3.0);
complex b = complex(1.2, 2);
complex c = b;
a = b + c;
b = b+c*a;
c = a*b + complex(1,2);
I.e., would like to write
ordinary arithmetic
expressions
on this user-defined class.
With Operator Overloading, We Can
class complex {
double real, imag;
public:
complex(double r, double i) :
real(r), imag(i) {}
complex operator+(complex a, complex b);
complex operator*(complex a, complex b);
complex& operator=(complex a, complex b);
...
}
General Format
returnType operator*(parameters);
  
any type keyword operator symbol
 Return type may be whatever the operator
returns
 Including a reference to the object of the operand
 Operator symbol may be any overloadable
operator from the list.
C++ Philosophy
 All operators have context
 Even the simple “built-in” operators of basic types
 E.g., '+', '-', '*', '/' for numerical types
 Compiler generators different code depending upon type of
operands
 Operator overloading is a generalization of this
feature to non-built-in types
 E.g., '<<', '>>' for bit-shift operations and also for
stream operations
C++ Philosophy (continued)
 Operators retain their precedence and
associativity, even when overloaded
 Operators retain their number of operands
 Cannot redefine operators on built-in types
 Not possible to define new operators
 Only (a subset of) the built-in C++ operators can be
overloaded
Operators that Can and Cannot be
Overloaded
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]
Operators that cannot be overloaded
. .* :: ?:
Operator Overload Function
 Either
 a non-static member function definition
or
 a global function definition
 Usually a friend of the class
 Function “name” is keyword operator followed by
the symbol for the operation being overloaded
 E.g. operator+, operator=
Operator Overload Function (continued)
 Operator overload function is a function just like
any other
 Can be called like any other – e.g.,
a.operator+(b)
 C++ provides the following short-hand
a+b
Operator Overload Function (continued)
 If operator overload function is declared global
then
operator+(a, b)
 also reduces to the following short-hand
a+b
Operator Functions as Class Members
 Leftmost operand must be of same class as
operator function.
 Use this keyword to implicitly get left operand
argument.
 Operators (), [], -> or any assignment
operator must be overloaded as a class
member function.
 Called when
 Left operand of binary operator is of this class.
 Single operand of unary operator is of this class.
Operator Functions as Global Members
 Need parameters for both operands.
 Can have object of different class than operator.
 Can be made a friend to access private or
protected data.
Stream Insertion and Extraction Operators
as Global Functions
 Overload << operator used where
 Left operand of type ostream &
 Such as cout object in cout << classObject
 Overload >> has left operand of istream &
 Left operand of type istream &
 Such as cin object in cout >> classObject
 Reason:–
 These operators are associated with class of right
operand
Commutative operators
 May need + to be commutative
 So both “a + b” and “b + a” work as expected.
 Suppose we have two different classes
 Overloaded operator can only be member function
when its class is on left.
 HugeIntClass + long int
 Can be member function
 For the other way, you need a global overloaded
function.
 long int + HugeIntClass
19
Unary Operators
 Operator attached to single operand.
 e.g –a, +a, --a, ++a, a--, a++,
 The unary operators operate on the object for which
they were called and normally, this operator appears
on the left side of the object.
Example
Output
21
Overloading Binary Operators
 The binary operators take two arguments and
following are the examples of Binary operators.
 You use binary operators very frequently like addition
(+) operator, subtraction (-) operator and division (/)
operator.
Example
Example
Example
 Output
Thank You

More Related Content

What's hot

operator overloading in C++
operator overloading in C++operator overloading in C++
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKamal Acharya
 
operator overloading
operator overloadingoperator overloading
operator overloading
Sorath Peetamber
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
Northeastern University
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
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 overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
Amogh Kalyanshetti
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
Docent Education
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
BalajiGovindan5
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
Md. Ashraful Islam
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ramya marichamy
 
c++
c++c++
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
sanya6900
 
#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 OverloadingHadziq Fabroyir
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
Ilio Catallo
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingabhay singh
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 

What's hot (20)

operator overloading in C++
operator overloading in C++operator overloading in C++
operator overloading in C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
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 overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
c++
c++c++
c++
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Lecture5
Lecture5Lecture5
Lecture5
 
#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
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 

Similar to Operator overloading

Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
zindadili
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
Jay Patel
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
Rithiga6
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
esuEthopi
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563Youth For Peace
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
MOHIT DADU
 
Overloading
OverloadingOverloading
Overloading
Mukhtar_Hunzai
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
Lahiru Dilshan
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
BalajiGovindan5
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Juginder Pal Singh
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
Sabi995708
 
C++ language
C++ languageC++ language
C++ language
Hamza Asif
 
C++_overloading.ppt
C++_overloading.pptC++_overloading.ppt
C++_overloading.ppt
Satyanandaram Nandigam
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
BalajiGovindan5
 
operator overloading
operator overloadingoperator overloading
operator overloading
Nishant Joshi
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
Nikul4470
 

Similar to Operator overloading (20)

Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Oops
OopsOops
Oops
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 
Overloading
OverloadingOverloading
Overloading
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
C++ language
C++ languageC++ language
C++ language
 
C++_overloading.ppt
C++_overloading.pptC++_overloading.ppt
C++_overloading.ppt
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 

More from Garima Singh Makhija

Bridging marke- credit risk-Modelling the Incremental Risk Charge.pptx
Bridging marke- credit risk-Modelling the  Incremental Risk Charge.pptxBridging marke- credit risk-Modelling the  Incremental Risk Charge.pptx
Bridging marke- credit risk-Modelling the Incremental Risk Charge.pptx
Garima Singh Makhija
 
2.Financial Statements.pptx
2.Financial Statements.pptx2.Financial Statements.pptx
2.Financial Statements.pptx
Garima Singh Makhija
 
4. Working Capital.pptx
4. Working Capital.pptx4. Working Capital.pptx
4. Working Capital.pptx
Garima Singh Makhija
 
6. Debt & Equity.pptx
6. Debt & Equity.pptx6. Debt & Equity.pptx
6. Debt & Equity.pptx
Garima Singh Makhija
 
5. Fixed Assets.pptx
5. Fixed Assets.pptx5. Fixed Assets.pptx
5. Fixed Assets.pptx
Garima Singh Makhija
 
3. Revenue & Expenses.pptx
3. Revenue & Expenses.pptx3. Revenue & Expenses.pptx
3. Revenue & Expenses.pptx
Garima Singh Makhija
 

More from Garima Singh Makhija (6)

Bridging marke- credit risk-Modelling the Incremental Risk Charge.pptx
Bridging marke- credit risk-Modelling the  Incremental Risk Charge.pptxBridging marke- credit risk-Modelling the  Incremental Risk Charge.pptx
Bridging marke- credit risk-Modelling the Incremental Risk Charge.pptx
 
2.Financial Statements.pptx
2.Financial Statements.pptx2.Financial Statements.pptx
2.Financial Statements.pptx
 
4. Working Capital.pptx
4. Working Capital.pptx4. Working Capital.pptx
4. Working Capital.pptx
 
6. Debt & Equity.pptx
6. Debt & Equity.pptx6. Debt & Equity.pptx
6. Debt & Equity.pptx
 
5. Fixed Assets.pptx
5. Fixed Assets.pptx5. Fixed Assets.pptx
5. Fixed Assets.pptx
 
3. Revenue & Expenses.pptx
3. Revenue & Expenses.pptx3. Revenue & Expenses.pptx
3. Revenue & Expenses.pptx
 

Recently uploaded

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 

Recently uploaded (20)

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 

Operator overloading

  • 2. Data abstraction  Data abstraction refers to, providing only essential information to the outside world and hiding their background details.  Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation.  C++ classes provides great level of data abstraction.
  • 3. Operator Overloading  C++ allows you to specify more than one definition for an operator in the same scope, which is called operator overloading .  Operators may not look like functions but can hide function invocations.  You cannot overload the meaning of operators if all arguments are primitive data types, nor can you change the precedence or associativity of operators.
  • 4. Over loadable Operators in C++ + - * / % ^ & | ~ ! && || ++ -- << >> , < <= == != > >= = += -= *= /= %= &= |= ^= <<= >>= [] () -> new delete
  • 5. Operator Overloading  It simplifies the program  d3.addobject(d1,d2)  Or the similar but equally obscure  d3=d1.addobject(d2)  Can be changed into much readable form  d3=d1+d2; (legal when d1, d2, d3 are of basic types)
  • 6. Simple Example class complex { double real, imag; public: complex(double r, double i) : real(r), imag(i) {} }  Would like to be able to write:– complex a = complex(1, 3.0); complex b = complex(1.2, 2); complex c = b; a = b + c; b = b+c*a; c = a*b + complex(1,2); I.e., would like to write ordinary arithmetic expressions on this user-defined class.
  • 7. With Operator Overloading, We Can class complex { double real, imag; public: complex(double r, double i) : real(r), imag(i) {} complex operator+(complex a, complex b); complex operator*(complex a, complex b); complex& operator=(complex a, complex b); ... }
  • 8. General Format returnType operator*(parameters);    any type keyword operator symbol  Return type may be whatever the operator returns  Including a reference to the object of the operand  Operator symbol may be any overloadable operator from the list.
  • 9. C++ Philosophy  All operators have context  Even the simple “built-in” operators of basic types  E.g., '+', '-', '*', '/' for numerical types  Compiler generators different code depending upon type of operands  Operator overloading is a generalization of this feature to non-built-in types  E.g., '<<', '>>' for bit-shift operations and also for stream operations
  • 10. C++ Philosophy (continued)  Operators retain their precedence and associativity, even when overloaded  Operators retain their number of operands  Cannot redefine operators on built-in types  Not possible to define new operators  Only (a subset of) the built-in C++ operators can be overloaded
  • 11. Operators that Can and Cannot be Overloaded Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[] Operators that cannot be overloaded . .* :: ?:
  • 12. Operator Overload Function  Either  a non-static member function definition or  a global function definition  Usually a friend of the class  Function “name” is keyword operator followed by the symbol for the operation being overloaded  E.g. operator+, operator=
  • 13. Operator Overload Function (continued)  Operator overload function is a function just like any other  Can be called like any other – e.g., a.operator+(b)  C++ provides the following short-hand a+b
  • 14. Operator Overload Function (continued)  If operator overload function is declared global then operator+(a, b)  also reduces to the following short-hand a+b
  • 15. Operator Functions as Class Members  Leftmost operand must be of same class as operator function.  Use this keyword to implicitly get left operand argument.  Operators (), [], -> or any assignment operator must be overloaded as a class member function.  Called when  Left operand of binary operator is of this class.  Single operand of unary operator is of this class.
  • 16. Operator Functions as Global Members  Need parameters for both operands.  Can have object of different class than operator.  Can be made a friend to access private or protected data.
  • 17. Stream Insertion and Extraction Operators as Global Functions  Overload << operator used where  Left operand of type ostream &  Such as cout object in cout << classObject  Overload >> has left operand of istream &  Left operand of type istream &  Such as cin object in cout >> classObject  Reason:–  These operators are associated with class of right operand
  • 18. Commutative operators  May need + to be commutative  So both “a + b” and “b + a” work as expected.  Suppose we have two different classes  Overloaded operator can only be member function when its class is on left.  HugeIntClass + long int  Can be member function  For the other way, you need a global overloaded function.  long int + HugeIntClass
  • 19. 19 Unary Operators  Operator attached to single operand.  e.g –a, +a, --a, ++a, a--, a++,  The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object.
  • 21. 21 Overloading Binary Operators  The binary operators take two arguments and following are the examples of Binary operators.  You use binary operators very frequently like addition (+) operator, subtraction (-) operator and division (/) operator.