SlideShare a Scribd company logo
1 of 27
Presentation Topics
• Polymorphism
• Compile Time
• Run Time
• Function Overloading
• Operator Overloading
Polymorphism
Polymorphism is crucial feature of Object Oriented
Programming.
Polymorphism simply means one name having multiple
forms.
Polymorphism - Cont.
Definition:
Polymorphism is the ability to create a variable, a
function or an object that has more than one form.
Example:
For example:
The + (plus) operator in C++:
4 + 5 <-- Integer addition
3.14 + 2.0 <-- Floating point addition
s1 + "bar" <-- String concatenation!
Types of Polymorphism
• In compile time polymorphism, compiler is able to select
the appropriate function a particular call at the compile
time.
• In run time polymorphism, an appropriate member
function is selected while the program is running.
Benefits of Polymorphism
Simplicity
Extensibility
Kinds of Polymorphism
• Runtime/Dynamic polymorphism (Based on virtual
methods)
• Compile time/Static polymorphism (Based on
templates)
Runtime polymorphism
• Runtime binding between abstract type and concrete type
• Enforces a common interface for all derived types via the
base class
• Enforces an ‘Is-A’ relationship
• Used extensively in OO frameworks
• Templates eating into parts of its territory
Compile time Polymorphism
• Compile time binding between abstract type and concrete
type
• Concrete types need not belong to the same inheritance
hierarchy. They just need to meet the ‘Constraints’
imposed by the generic type. So, more generic than the
virtuals
• Foundation of ‘Generic programming’ or programming
based on ‘Concepts’ (Eg: STL)
• A very ‘Happening area’ in C++ right now (See TR1,
Boost, C++0x...)
Introduction
• Operator overloading
– Enabling C++’s operators to work with class objects
– Using traditional operators with user-defined objects
– Requires great care; when overloading is misused, program difficult to
understand
– Examples of already overloaded operators
• Operator << is both the stream-insertion operator and the bitwise left-
shift operator
• + and -, perform arithmetic on multiple types
Introduction
• Overloading an operator
–Write function definition as normal
–Function name is keyword operator followed by the
symbol for the operator being overloaded
–operator+ used to overload the addition operator (+)
Restrictions on Operator Overloading
• Restrictions on Operator Overloading
Operators that can be overloaded
+ - * / % ^ & |
~ ! = < > += -= *=
/= %= ^= &= |= << >> >>=
<<= == != <= >= && || ++
-- ->* , -> [] () new delete
new[] delete[]
C++ Operators that cannot be overloaded
Operators that cannot be overloaded
. .* :: ?: sizeof
Operators that cannot be overloaded
. .* :: ?: sizeof
Restrictions on Operator Overloading
• Overloading restrictions
– Precedence of an operator cannot be changed
– Associativity of an operator cannot be changed
– Arity (number of operands) cannot be changed
• Unary operators remain unary, and binary operators remain
binary
• Operators &, *, + and - each have unary and binary versions
• Unary and binary versions can be overloaded separately
• Thank You !!
Restrictions on Operator Overloading
• No new operators can be created
– Use only existing operators
• No overloading operators for built-in types
– Cannot change how two integers are added
– Produces a syntax error
In C++, two or more functions can share
the same name as long as their parameter
declarations are different.
In this situation, the functions that share
the same name are said to be overloaded,
and the process is referred to as function
overloading
To see why function overloading is important,
first consider three functions defined by the C subset:
abs(), labs(), and fabs().
abs() returns the absolute value of an integer,
labs() returns the absolute value of a long, and
fabs() returns the absolute value of a double.
Function Overloading – DEFINITION
• It is the process of using the same name for two or
more functions.
The secret to overloading is that each redefinition of
the function must use
either-
• different types of parameters
• different number of parameters.
The key to function overloading is a function’s argument list.
A function’s argument list is known as its signature.
Example :
Different types of arguments
void print(int a);
void print (double b);
void print(char c);
Different number of
parameters
void area(float r);
void area(float l, float b);
void area(float a, float b, float
c);
STEPS FOR FINDING BEST MATCH
• The actual arguments are compared with formal
arguments of the overloaded functions and the best
match is found through a number of steps.
•
In order to find the best match, the compiler follows
the steps given below:
STEP - 1 : Search for an exact match
•
If the type of the actual and formal argument is
exactly the same, the compiler invokes that function.
void print(int x); // function #1
void print(float p); // function #2
void print(char c); // function #3
------------
-------------
print(5.3); //invokes the function #2
STEP- 2 : A match through promotion
•
If no exact match is found, an attempt is made to
achieve a match through promotion.
void print(int x); // function #1
void print(float p); // function #2
void print(double f); // function #3
------------
-------------
print(‘c’); //matches the function #1 through
promotion
THANKS....

More Related Content

What's hot

Function class in c++
Function class in c++Function class in c++
Function class in c++Kumar
 
Inline assembly language programs in c
Inline assembly language programs in cInline assembly language programs in c
Inline assembly language programs in cTech_MX
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIADheeraj Kataria
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFaisal Shehzad
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11Jadavsejal
 
Code optimisation presnted
Code optimisation presntedCode optimisation presnted
Code optimisation presntedbhavanatmithun
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Jenish Patel
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 

What's hot (18)

Loop optimization
Loop optimizationLoop optimization
Loop optimization
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Function class in c++
Function class in c++Function class in c++
Function class in c++
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Inline assembly language programs in c
Inline assembly language programs in cInline assembly language programs in c
Inline assembly language programs in c
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
 
Optimization
OptimizationOptimization
Optimization
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11
 
Code optimisation presnted
Code optimisation presntedCode optimisation presnted
Code optimisation presnted
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 

Similar to Polymorphism

Similar to Polymorphism (20)

OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
 
Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptx
 
OODP UNIT 2 Function overloading
OODP UNIT 2 Function overloadingOODP UNIT 2 Function overloading
OODP UNIT 2 Function overloading
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
C++_overloading.ppt
C++_overloading.pptC++_overloading.ppt
C++_overloading.ppt
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
 
oops.pptx
oops.pptxoops.pptx
oops.pptx
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
OOP_UnitIII.pdf
OOP_UnitIII.pdfOOP_UnitIII.pdf
OOP_UnitIII.pdf
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01Polymorphism 140527082302-phpapp01
Polymorphism 140527082302-phpapp01
 

More from sana younas

7 habits of highly effective people
7 habits of highly effective people7 habits of highly effective people
7 habits of highly effective peoplesana younas
 
Connectivity of graphs
Connectivity of graphsConnectivity of graphs
Connectivity of graphssana younas
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithmsana younas
 
circular linklist
circular linklistcircular linklist
circular linklistsana younas
 
Enterpise system
Enterpise systemEnterpise system
Enterpise systemsana younas
 
Database administration
Database administrationDatabase administration
Database administrationsana younas
 
Universal logic gate
Universal logic gateUniversal logic gate
Universal logic gatesana younas
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingsana younas
 
Memory management
Memory managementMemory management
Memory managementsana younas
 

More from sana younas (16)

7 habits of highly effective people
7 habits of highly effective people7 habits of highly effective people
7 habits of highly effective people
 
Connectivity of graphs
Connectivity of graphsConnectivity of graphs
Connectivity of graphs
 
Shortest path algorithm
Shortest path algorithmShortest path algorithm
Shortest path algorithm
 
Binary search
Binary searchBinary search
Binary search
 
circular linklist
circular linklistcircular linklist
circular linklist
 
Link list 2
Link list 2Link list 2
Link list 2
 
Link list 1
Link list 1Link list 1
Link list 1
 
Heapsort 1
Heapsort 1Heapsort 1
Heapsort 1
 
Arrays
ArraysArrays
Arrays
 
Enterpise system
Enterpise systemEnterpise system
Enterpise system
 
Database administration
Database administrationDatabase administration
Database administration
 
Encoders
EncodersEncoders
Encoders
 
Universal logic gate
Universal logic gateUniversal logic gate
Universal logic gate
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Memory management
Memory managementMemory management
Memory management
 
Parallel adders
Parallel addersParallel adders
Parallel adders
 

Recently uploaded

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 

Recently uploaded (20)

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 

Polymorphism

  • 1.
  • 2. Presentation Topics • Polymorphism • Compile Time • Run Time • Function Overloading • Operator Overloading
  • 3.
  • 4. Polymorphism Polymorphism is crucial feature of Object Oriented Programming. Polymorphism simply means one name having multiple forms.
  • 5. Polymorphism - Cont. Definition: Polymorphism is the ability to create a variable, a function or an object that has more than one form.
  • 6. Example: For example: The + (plus) operator in C++: 4 + 5 <-- Integer addition 3.14 + 2.0 <-- Floating point addition s1 + "bar" <-- String concatenation!
  • 7. Types of Polymorphism • In compile time polymorphism, compiler is able to select the appropriate function a particular call at the compile time. • In run time polymorphism, an appropriate member function is selected while the program is running.
  • 9. Kinds of Polymorphism • Runtime/Dynamic polymorphism (Based on virtual methods) • Compile time/Static polymorphism (Based on templates)
  • 10. Runtime polymorphism • Runtime binding between abstract type and concrete type • Enforces a common interface for all derived types via the base class • Enforces an ‘Is-A’ relationship • Used extensively in OO frameworks • Templates eating into parts of its territory
  • 11. Compile time Polymorphism • Compile time binding between abstract type and concrete type • Concrete types need not belong to the same inheritance hierarchy. They just need to meet the ‘Constraints’ imposed by the generic type. So, more generic than the virtuals • Foundation of ‘Generic programming’ or programming based on ‘Concepts’ (Eg: STL) • A very ‘Happening area’ in C++ right now (See TR1, Boost, C++0x...)
  • 12.
  • 13. Introduction • Operator overloading – Enabling C++’s operators to work with class objects – Using traditional operators with user-defined objects – Requires great care; when overloading is misused, program difficult to understand – Examples of already overloaded operators • Operator << is both the stream-insertion operator and the bitwise left- shift operator • + and -, perform arithmetic on multiple types
  • 14. Introduction • Overloading an operator –Write function definition as normal –Function name is keyword operator followed by the symbol for the operator being overloaded –operator+ used to overload the addition operator (+)
  • 15. Restrictions on Operator Overloading • Restrictions on Operator Overloading Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[]
  • 16. C++ Operators that cannot be overloaded Operators that cannot be overloaded . .* :: ?: sizeof Operators that cannot be overloaded . .* :: ?: sizeof
  • 17. Restrictions on Operator Overloading • Overloading restrictions – Precedence of an operator cannot be changed – Associativity of an operator cannot be changed – Arity (number of operands) cannot be changed • Unary operators remain unary, and binary operators remain binary • Operators &, *, + and - each have unary and binary versions • Unary and binary versions can be overloaded separately • Thank You !!
  • 18. Restrictions on Operator Overloading • No new operators can be created – Use only existing operators • No overloading operators for built-in types – Cannot change how two integers are added – Produces a syntax error
  • 19.
  • 20. In C++, two or more functions can share the same name as long as their parameter declarations are different. In this situation, the functions that share the same name are said to be overloaded, and the process is referred to as function overloading
  • 21. To see why function overloading is important, first consider three functions defined by the C subset: abs(), labs(), and fabs(). abs() returns the absolute value of an integer, labs() returns the absolute value of a long, and fabs() returns the absolute value of a double.
  • 22. Function Overloading – DEFINITION • It is the process of using the same name for two or more functions. The secret to overloading is that each redefinition of the function must use either- • different types of parameters • different number of parameters.
  • 23. The key to function overloading is a function’s argument list. A function’s argument list is known as its signature. Example : Different types of arguments void print(int a); void print (double b); void print(char c); Different number of parameters void area(float r); void area(float l, float b); void area(float a, float b, float c);
  • 24. STEPS FOR FINDING BEST MATCH • The actual arguments are compared with formal arguments of the overloaded functions and the best match is found through a number of steps. • In order to find the best match, the compiler follows the steps given below:
  • 25. STEP - 1 : Search for an exact match • If the type of the actual and formal argument is exactly the same, the compiler invokes that function. void print(int x); // function #1 void print(float p); // function #2 void print(char c); // function #3 ------------ ------------- print(5.3); //invokes the function #2
  • 26. STEP- 2 : A match through promotion • If no exact match is found, an attempt is made to achieve a match through promotion. void print(int x); // function #1 void print(float p); // function #2 void print(double f); // function #3 ------------ ------------- print(‘c’); //matches the function #1 through promotion