SlideShare a Scribd company logo
1 of 18
Download to read offline
Introduction Summary References
OVERLOADING
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 20, 2015
Introduction Summary References
OUTLINE I
INTRODUCTION
SUMMARY
REFERENCES
Introduction Summary References
INTRODUCTION
• In this chapter we will investigate the idea of overloading.
• Overloading based on scopes.
• Overloading based on type signatures.
• Coercison, Conversion and Casts.
• Redefinition.
• Polyadicity.
Introduction Summary References
A DEFINITION OF OVERLOADING
• We say a term is overloaded if it has two or more
meanings.
• Most words in natural languages are overloaded, and
confusion is resolved by means of context.
• Same is true of OO languages.
• There are two important classes of context that are used to
resolve overloaded names.
• Overloading based on Scopes.
• Overloading based on Type Signatures.
Introduction Summary References
OVERLOADING BASED ON SCOPES I
• A name scope defines the portion of a program in which a
name can be used, or the way it can be used. Scopes are
introduced using lots of different mechanisms:
• Classes or interfaces
• Packages or Units
• Procedures or Functions in some languages, even Blocks
• An advantage of scopes is that the same name can appear
in two or more scopes with no ambiguity.
Introduction Summary References
OVERLOADING BASED ON SCOPES II
AN EXAMPLE
Procedure Friend . sendFlowersTo ( anAddress : address ) ;
begin
go to f l o r i s t ;
give f l o r i s t message sendFlowersTo ( anAddress ) ;
end ;
Procedure F l o r i s t . sendFlowersTo ( anAddress : address ) ;
begin
i f address i s nearby then
make up flower arrangement
t e l l d el iv e ry person sendFlowersTo ( anAddress ) ;
else
look up f l o r i s t near anAddress
phone f l o r i s t
give f l o r i s t message sendFlowersTo ( anAddress )
end ;
Introduction Summary References
RESOLVING OVERLOADED NAMES
• This type of overloading is resolved by looking at the type
of the receiver.
• Allows the same name to be used in unrelated classes.
• Since names need not be distinct, allows short, easy to
remember, meaningful names.
Introduction Summary References
OVERLOADING BASED ON TYPE SIGNATURES
A different type of overloading allows multiple implementations
in the same scope to be resolved using type signatures.
TYPE SIGNATURES
class Example {
/ / same name, three d i f f e r e n t methods
int sum ( int a ) { return a ; }
int sum ( int a , int b ) { return a + b ; }
int sum ( int a , int b , int c ) { return a + b + c ; }
}
• A type signature is the combination of argument types and
return type.
• By looking at the signature of a call, can tell which version
is intended.
Introduction Summary References
RESOLUTION PERFORMED AT COMPPILE TIME
Note that resolution is almost always performed at compile
time, based on static types, and not dynamic values.
RESOLUTION
class Parent { . . . } ;
class Child : public Parent { . . . } ;
void Test ( Parent ∗ p ) { cout << " in parent " << endl ; }
void Test ( Child ∗ c ) { cout << " in c h i l d " << endl }
Parent ∗ value = new Child ( ) ;
Test ( value ) ;
Example will, perhaps surprizingly, execute parent function.
Introduction Summary References
STREAM OUTPUT IN C++
Stream output in C++ is a good example of the power of
overloading. Every primitive type has a different stream output
function.
STREAM IO
ostream & operator << ( ostream & destination , int source ) ;
ostream & operator << ( ostream & destination , short source ) ;
ostream & operator << ( ostream & destination , long source ) ;
ostream & operator << ( ostream & destination , char source ) ;
ostream & operator << ( ostream & destination , char ∗ source ) ;
/ / . . . and so on
double d = 3.14;
cout << "The answer i s " << d << ’  n ’ ;
Introduction Summary References
EASY TO EXTEND
• Since output uses overloading, it is very easy to extend to
new types.
EXTEND
class Fraction {
public :
Fraction ( int top , int bottom ) { t = top ; b = bottom ; }
int numerator ( ) { return t ; }
int denominator ( ) { return b ; }
private :
int t , b ;
} ;
ostream & operator << ( ostream & destination , Fraction & source )
{
destination << source . numerator ( ) << " / " << source . denominator ( ) ;
return destination ;
}
Fraction f (3 , 4 ) ;
cout << "The value of f i s " << f << ’  n ’ ;
Introduction Summary References
CONVERSION AND COERCIONS
• When one adds conversions into the mix, resolving
overloaded function or method calls can get very complex.
Many different types of conversions:
• Implicit value changing conversion (such as integer to real).
• Implicit conversion that does not change value (pointer to
child class converted into pointer to parent).
• Explicit conversions (casts).
• Conversion operators (C++ and the like).
• See text for illustration of the complex rules.
Introduction Summary References
REDEFINITIONS
• A redefinition occurs when a child class changes the type
signature of a method in the parent class.
• Two different types of rules are used to resolve name:
• The merge model.
• The scope of the child is merged with the scope of the
parent.
• The hierarchical model.
• Scopes are separate.
• Search is made for first scope containing name, then for
best fit within the scope.
Introduction Summary References
AN EXAMPLE ILLUSTRATING REDEFINITION MODELS
• The following example will illustrate the difference in these
two models.
REDEFINITION MODELS
class Parent {
public void example ( int a )
{ System . out . p r i n t l n ( " in parent method " ) ; }
}
class Child extends Parent {
public void example ( int a , int b )
{ System . out . p r i n t l n ( " in c h i l d method " ) ; }
}
Child aChild = new Child ( ) ;
aChild . example ( 3 ) ;
Will execute parent method in Java and C# (Merge model) and
give error in C++ (hierarchical model). Delphi allows
programmer control over this.
Introduction Summary References
OPTIONAL PARAMETERS
• Some languages allow the programmer to create optional
parameters, usually only at the end of the parameter list:
PARAMETERS
function Count (A, B : Integer ; C : Integer = 0; D : Integer = 0 ) ;
begin
(∗ Result i s a pseudo−variable used ∗)
(∗ to represent r e s u l t of any function ∗)
Result := A + B + C + D;
end
begin
Writeln ( Count (2 , 3 , 4 , 5 ) ) ; / / can use four arguments
Writeln ( Count (2 , 3 , 4 ) ) ; / / or three
Writeln ( Count (2 , 3 ) ) ; / / or two
end
Such a program will have more than one type signature.
Introduction Summary References
ARBITRARY NUMBER OF ARGUMENTS IN C#
• The language C# has an interesting way to include
arbitrary number of arguments:
ARGUMENTS
class ParamsExample {
public void Write ( int x ) {
/ / use t h i s with one argument
WriteString ( " Example one " ) ;
WriteString ( x . ToString ( ) ) ;
}
public void Write ( double x , int y ) {
/ / use t h i s with two arguments
WriteString ( " Example two " ) ;
WriteString ( x . ToString ( ) ) ;
WriteString ( y . ToString ( ) ) ;
}
public void Write ( params object [ ] args ) {
/ / use t h i s with any other combination
WriteString ( " Example three " ) ;
for ( int i = 0; i < args . GetLength ( 0 ) ; i ++)
WriteString ( args [ i ] . ToString ( ) ) ;
}
}
ParamsExample p ;
p . Write ( 4 2 ) ;
Example one 42
p . Write (3.14 ,159);
Example two 3.14159
p . Write (1 ,2 ,3 ,4);
Example three 1234
p . Write ( 3 . 1 4 ) ;
Example three 3.14
p . Write (3 , " abc " ) ;
Example three 3abc
Introduction Summary References
SUMMARY
• In this chapter we have looked at various aspects of
overloading.
• Overloading based on Scopes.
• Overloading based on Type Signatures.
• Redefinition.
• Polyadicity (functions with variable number of arguments).
Introduction Summary References
REFERENCES
• Images and content for developing these slides have been
taken from the follwoing book with the permission of the
author.
• An Introduction to Object Oriented Programming, Timothy
Budd.
• This presentation is developed using Beamer:
• Singapore, spruce.

More Related Content

What's hot

C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
Mca ii dfs u-2 array records and pointer
Mca ii dfs u-2 array records and pointerMca ii dfs u-2 array records and pointer
Mca ii dfs u-2 array records and pointerRai University
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++HalaiHansaika
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Dushmanta Nath
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP ImplementationFridz Felisco
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
Chapter 13.1.8
Chapter 13.1.8Chapter 13.1.8
Chapter 13.1.8patcha535
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and stringsRai University
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++Rokonuzzaman Rony
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)Sami Said
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01Zafor Iqbal
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bisonvip_du
 

What's hot (20)

C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
C++ language
C++ languageC++ language
C++ language
 
Mca ii dfs u-2 array records and pointer
Mca ii dfs u-2 array records and pointerMca ii dfs u-2 array records and pointer
Mca ii dfs u-2 array records and pointer
 
Interpreter Case Study - Design Patterns
Interpreter Case Study - Design PatternsInterpreter Case Study - Design Patterns
Interpreter Case Study - Design Patterns
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP Implementation
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Chapter 13.1.8
Chapter 13.1.8Chapter 13.1.8
Chapter 13.1.8
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
3.5
3.53.5
3.5
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
 
Swift
SwiftSwift
Swift
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
C++ for beginners
C++ for beginnersC++ for beginners
C++ for beginners
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
 
Pointers
PointersPointers
Pointers
 

Viewers also liked

Object Interconnections
Object InterconnectionsObject Interconnections
Object Interconnectionsadil raja
 
Polymorphism and Software Reuse
Polymorphism and Software ReusePolymorphism and Software Reuse
Polymorphism and Software Reuseadil raja
 
Distributed Computing
Distributed ComputingDistributed Computing
Distributed Computingadil raja
 
The Polymorphic Variable
The Polymorphic VariableThe Polymorphic Variable
The Polymorphic Variableadil raja
 
Implementation
ImplementationImplementation
Implementationadil raja
 
Messages, Instances and Initialization
Messages, Instances and InitializationMessages, Instances and Initialization
Messages, Instances and Initializationadil raja
 
Implications of Substitution
Implications of SubstitutionImplications of Substitution
Implications of Substitutionadil raja
 
Container Classes
Container ClassesContainer Classes
Container Classesadil raja
 
Object-Oriented Design
Object-Oriented DesignObject-Oriented Design
Object-Oriented Designadil raja
 
Classes And Methods
Classes And MethodsClasses And Methods
Classes And Methodsadil raja
 
Thinking Object-Oriented
Thinking Object-OrientedThinking Object-Oriented
Thinking Object-Orientedadil raja
 
Static and Dynamic Behavior
Static and Dynamic BehaviorStatic and Dynamic Behavior
Static and Dynamic Behavioradil raja
 
Inheritance and Substitution
Inheritance and SubstitutionInheritance and Substitution
Inheritance and Substitutionadil raja
 
Subclasses and Subtypes
Subclasses and SubtypesSubclasses and Subtypes
Subclasses and Subtypesadil raja
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspectionadil raja
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swingadil raja
 
Software Frameworks
Software FrameworksSoftware Frameworks
Software Frameworksadil raja
 

Viewers also liked (20)

Generics
GenericsGenerics
Generics
 
Object Interconnections
Object InterconnectionsObject Interconnections
Object Interconnections
 
Polymorphism and Software Reuse
Polymorphism and Software ReusePolymorphism and Software Reuse
Polymorphism and Software Reuse
 
Distributed Computing
Distributed ComputingDistributed Computing
Distributed Computing
 
The STL
The STLThe STL
The STL
 
The Polymorphic Variable
The Polymorphic VariableThe Polymorphic Variable
The Polymorphic Variable
 
Implementation
ImplementationImplementation
Implementation
 
Overriding
OverridingOverriding
Overriding
 
Messages, Instances and Initialization
Messages, Instances and InitializationMessages, Instances and Initialization
Messages, Instances and Initialization
 
Implications of Substitution
Implications of SubstitutionImplications of Substitution
Implications of Substitution
 
Container Classes
Container ClassesContainer Classes
Container Classes
 
Object-Oriented Design
Object-Oriented DesignObject-Oriented Design
Object-Oriented Design
 
Classes And Methods
Classes And MethodsClasses And Methods
Classes And Methods
 
Thinking Object-Oriented
Thinking Object-OrientedThinking Object-Oriented
Thinking Object-Oriented
 
Static and Dynamic Behavior
Static and Dynamic BehaviorStatic and Dynamic Behavior
Static and Dynamic Behavior
 
Inheritance and Substitution
Inheritance and SubstitutionInheritance and Substitution
Inheritance and Substitution
 
Subclasses and Subtypes
Subclasses and SubtypesSubclasses and Subtypes
Subclasses and Subtypes
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspection
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swing
 
Software Frameworks
Software FrameworksSoftware Frameworks
Software Frameworks
 

Similar to Overloading

Similar to Overloading (20)

Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Bc0037
Bc0037Bc0037
Bc0037
 
Presentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptxPresentation on polymorphism in c++.pptx
Presentation on polymorphism in c++.pptx
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Polymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh SarkarPolymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh Sarkar
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
structure,pointerandstring
structure,pointerandstringstructure,pointerandstring
structure,pointerandstring
 
3 Function Overloading
3 Function Overloading3 Function Overloading
3 Function Overloading
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Python
 
Maintainable go
Maintainable goMaintainable go
Maintainable go
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Function Overloading.ppt
Function Overloading.pptFunction Overloading.ppt
Function Overloading.ppt
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 

More from adil raja

A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specificationadil raja
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehiclesadil raja
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystifiedadil raja
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)adil raja
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Researchadil raja
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocoladil raja
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Socketsadil raja
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Executionadil raja
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistanadil raja
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousingadil raja
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...adil raja
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...adil raja
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPadil raja
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specificationsadil raja
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...adil raja
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...adil raja
 

More from adil raja (20)

ANNs.pdf
ANNs.pdfANNs.pdf
ANNs.pdf
 
A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specification
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystified
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Research
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocol
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Sockets
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Execution
 
Thesis
ThesisThesis
Thesis
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistan
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
 
VoIP
VoIPVoIP
VoIP
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specifications
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 
ULMAN-GUI
ULMAN-GUIULMAN-GUI
ULMAN-GUI
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 

Recently uploaded

Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 

Recently uploaded (20)

Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 

Overloading

  • 1. Introduction Summary References OVERLOADING Muhammad Adil Raja Roaming Researchers, Inc. cbna April 20, 2015
  • 2. Introduction Summary References OUTLINE I INTRODUCTION SUMMARY REFERENCES
  • 3. Introduction Summary References INTRODUCTION • In this chapter we will investigate the idea of overloading. • Overloading based on scopes. • Overloading based on type signatures. • Coercison, Conversion and Casts. • Redefinition. • Polyadicity.
  • 4. Introduction Summary References A DEFINITION OF OVERLOADING • We say a term is overloaded if it has two or more meanings. • Most words in natural languages are overloaded, and confusion is resolved by means of context. • Same is true of OO languages. • There are two important classes of context that are used to resolve overloaded names. • Overloading based on Scopes. • Overloading based on Type Signatures.
  • 5. Introduction Summary References OVERLOADING BASED ON SCOPES I • A name scope defines the portion of a program in which a name can be used, or the way it can be used. Scopes are introduced using lots of different mechanisms: • Classes or interfaces • Packages or Units • Procedures or Functions in some languages, even Blocks • An advantage of scopes is that the same name can appear in two or more scopes with no ambiguity.
  • 6. Introduction Summary References OVERLOADING BASED ON SCOPES II AN EXAMPLE Procedure Friend . sendFlowersTo ( anAddress : address ) ; begin go to f l o r i s t ; give f l o r i s t message sendFlowersTo ( anAddress ) ; end ; Procedure F l o r i s t . sendFlowersTo ( anAddress : address ) ; begin i f address i s nearby then make up flower arrangement t e l l d el iv e ry person sendFlowersTo ( anAddress ) ; else look up f l o r i s t near anAddress phone f l o r i s t give f l o r i s t message sendFlowersTo ( anAddress ) end ;
  • 7. Introduction Summary References RESOLVING OVERLOADED NAMES • This type of overloading is resolved by looking at the type of the receiver. • Allows the same name to be used in unrelated classes. • Since names need not be distinct, allows short, easy to remember, meaningful names.
  • 8. Introduction Summary References OVERLOADING BASED ON TYPE SIGNATURES A different type of overloading allows multiple implementations in the same scope to be resolved using type signatures. TYPE SIGNATURES class Example { / / same name, three d i f f e r e n t methods int sum ( int a ) { return a ; } int sum ( int a , int b ) { return a + b ; } int sum ( int a , int b , int c ) { return a + b + c ; } } • A type signature is the combination of argument types and return type. • By looking at the signature of a call, can tell which version is intended.
  • 9. Introduction Summary References RESOLUTION PERFORMED AT COMPPILE TIME Note that resolution is almost always performed at compile time, based on static types, and not dynamic values. RESOLUTION class Parent { . . . } ; class Child : public Parent { . . . } ; void Test ( Parent ∗ p ) { cout << " in parent " << endl ; } void Test ( Child ∗ c ) { cout << " in c h i l d " << endl } Parent ∗ value = new Child ( ) ; Test ( value ) ; Example will, perhaps surprizingly, execute parent function.
  • 10. Introduction Summary References STREAM OUTPUT IN C++ Stream output in C++ is a good example of the power of overloading. Every primitive type has a different stream output function. STREAM IO ostream & operator << ( ostream & destination , int source ) ; ostream & operator << ( ostream & destination , short source ) ; ostream & operator << ( ostream & destination , long source ) ; ostream & operator << ( ostream & destination , char source ) ; ostream & operator << ( ostream & destination , char ∗ source ) ; / / . . . and so on double d = 3.14; cout << "The answer i s " << d << ’ n ’ ;
  • 11. Introduction Summary References EASY TO EXTEND • Since output uses overloading, it is very easy to extend to new types. EXTEND class Fraction { public : Fraction ( int top , int bottom ) { t = top ; b = bottom ; } int numerator ( ) { return t ; } int denominator ( ) { return b ; } private : int t , b ; } ; ostream & operator << ( ostream & destination , Fraction & source ) { destination << source . numerator ( ) << " / " << source . denominator ( ) ; return destination ; } Fraction f (3 , 4 ) ; cout << "The value of f i s " << f << ’ n ’ ;
  • 12. Introduction Summary References CONVERSION AND COERCIONS • When one adds conversions into the mix, resolving overloaded function or method calls can get very complex. Many different types of conversions: • Implicit value changing conversion (such as integer to real). • Implicit conversion that does not change value (pointer to child class converted into pointer to parent). • Explicit conversions (casts). • Conversion operators (C++ and the like). • See text for illustration of the complex rules.
  • 13. Introduction Summary References REDEFINITIONS • A redefinition occurs when a child class changes the type signature of a method in the parent class. • Two different types of rules are used to resolve name: • The merge model. • The scope of the child is merged with the scope of the parent. • The hierarchical model. • Scopes are separate. • Search is made for first scope containing name, then for best fit within the scope.
  • 14. Introduction Summary References AN EXAMPLE ILLUSTRATING REDEFINITION MODELS • The following example will illustrate the difference in these two models. REDEFINITION MODELS class Parent { public void example ( int a ) { System . out . p r i n t l n ( " in parent method " ) ; } } class Child extends Parent { public void example ( int a , int b ) { System . out . p r i n t l n ( " in c h i l d method " ) ; } } Child aChild = new Child ( ) ; aChild . example ( 3 ) ; Will execute parent method in Java and C# (Merge model) and give error in C++ (hierarchical model). Delphi allows programmer control over this.
  • 15. Introduction Summary References OPTIONAL PARAMETERS • Some languages allow the programmer to create optional parameters, usually only at the end of the parameter list: PARAMETERS function Count (A, B : Integer ; C : Integer = 0; D : Integer = 0 ) ; begin (∗ Result i s a pseudo−variable used ∗) (∗ to represent r e s u l t of any function ∗) Result := A + B + C + D; end begin Writeln ( Count (2 , 3 , 4 , 5 ) ) ; / / can use four arguments Writeln ( Count (2 , 3 , 4 ) ) ; / / or three Writeln ( Count (2 , 3 ) ) ; / / or two end Such a program will have more than one type signature.
  • 16. Introduction Summary References ARBITRARY NUMBER OF ARGUMENTS IN C# • The language C# has an interesting way to include arbitrary number of arguments: ARGUMENTS class ParamsExample { public void Write ( int x ) { / / use t h i s with one argument WriteString ( " Example one " ) ; WriteString ( x . ToString ( ) ) ; } public void Write ( double x , int y ) { / / use t h i s with two arguments WriteString ( " Example two " ) ; WriteString ( x . ToString ( ) ) ; WriteString ( y . ToString ( ) ) ; } public void Write ( params object [ ] args ) { / / use t h i s with any other combination WriteString ( " Example three " ) ; for ( int i = 0; i < args . GetLength ( 0 ) ; i ++) WriteString ( args [ i ] . ToString ( ) ) ; } } ParamsExample p ; p . Write ( 4 2 ) ; Example one 42 p . Write (3.14 ,159); Example two 3.14159 p . Write (1 ,2 ,3 ,4); Example three 1234 p . Write ( 3 . 1 4 ) ; Example three 3.14 p . Write (3 , " abc " ) ; Example three 3abc
  • 17. Introduction Summary References SUMMARY • In this chapter we have looked at various aspects of overloading. • Overloading based on Scopes. • Overloading based on Type Signatures. • Redefinition. • Polyadicity (functions with variable number of arguments).
  • 18. Introduction Summary References REFERENCES • Images and content for developing these slides have been taken from the follwoing book with the permission of the author. • An Introduction to Object Oriented Programming, Timothy Budd. • This presentation is developed using Beamer: • Singapore, spruce.