SlideShare a Scribd company logo
METHOD
OVERLOADING
Michael Heron
Introduction
• One of the facilities available in modern programming
languages is that of method overloading.
• The ability to have more than one method with the same name in a
particular code space.
• This facility can be use to great effect to permit
consistency of an interface across objects.
• We’ll talk about that today.
Method Overloading
• It is not the name of the method which uniquely identifies
a method in C++.
• It’s the method signature.
• It is the combination of:
• The name of the method
• The type and order of parameters.
• This allows us to reuse the same name for a function
many times.
• We’ve already seen this to a degree with constructor methods.
Method Overloading
• This allows us to re-use the same method name when
doing so makes logical sense.
• We don’t need two method names:
• add_two_ints (int x, int y);
• add_two_floats (float x, float y)
• We just need one:
• add_two_nums (int x, int y);
• add_two_floats (float x, float y);
Method Overloading
• This allows us to reduce the cognitive burden needed to
use our coded objects.
• The fewer methods people have to memorize, the better.
• This comes at a danger:
• Overload methods only when it makes sense to do so.
• When the output is the same, just the parameters differ.
• Don’t use it when there are side-effects to choosing particular
combinations of parameters.
• As far as possible, their execution should be identical.
Guidelines for Method Overloading
• Like anything in programming, you can do this well or you
can do it badly.
• We’re going to aim to do it well.
• There are some guidelines for overloading methods
properly.
• Use a consistent return type
• Ensure consistent parameter names
• Ensure consistent parameter orders
• Use internal function redirection
• Don’t overdo them
Use A Consistent Return Type
• One of the benefits of method overloading is that it
reduces cognitive burden.
• This benefit is lost when overloaded methods have inconsistent
return types.
• As with most guidelines, this is not an iron-cast rule.
• Sometimes it makes sense, such as methods that perform
arithmetic.
• You don’t want to get an int back when you passed in floats.
Ensure Consistent Parameter Names
• If you call a parameter ‘name’ in one overloaded method,
don’t call it ‘obName in another’
• This is something that is not such an issue for external parties, but
has an impact of ease of maintenance.
• Keep the names of your parameters the same.
• Refactor the code to ensure this if necessary.
Ensure Consistent Parameter Ordering
• The meaning of parameters should remain consistent
across overloaded methods:
• void do_stuff (int x, int y, int z);
• void do_stuff (int y, int x);
• Mixing and matching the order of parameters is a sure-fire
way to increase both cognitive burden and user
frustration.
• Including your own frustration later!
Use Internal Function Redirection
• As far as is possible, your overloaded methods should be
public interfaces only.
• Internally they should act as ‘wrappers’ around some internal
‘worker’ method.
• The wrappers do only the minimal work required to
redirect calls on to the proper method.
• Supplying default values for missing info, doing type conversion,
etc.
Don’t Overdo It
• Combinatorial explosion ensures we can’t provide
implementations for all combinations of all parameters.
• Or indeed, that we should attempt it.
• It would be crazy to overload all of the possible
combinations of provided and missing info.
• Instead, we provide overloaded methods for those
combinations that are most likely to make up the majority
of calls.
• We can provide a ‘all you can eat’ version too for those with more
specialised requirements.
Why Overload Methods?
• Provides a consistent interface for your objects.
• Reduces cognitive burden on learning new objects.
• Don’t need to learn five methods that do much the same thing.
• This is a feature in many older languages.
• Makes code more readable.
• Makes code more maintainable.
Method Overloading Vs Polymorphism
• Method overloading is often characterised as a kind of polymorphism.
• It’s not really polymorphic, but opinions vary.
• It’s closest match is to the idea of ad-hoc polymorphism
• The set of possible support options is finite.
• It is a tightly restricted version of parametric polymorphism.
• Treating all variables without any reference to a specific type.
• C#, Java and C++ all offer facilities to do this.
• But method overloading isn’t it.
Method Overloading Vs Polymorphism
• Method overloading can be written in such a way as to
make use of polymorphism.
• But at its core, it does not adapt to the type of parameters it is
given.
• The choice as to which method to invoke is done at
compile time.
• Think of overloading not as a polymorphic facility but as
syntactical sleight of hand.
Variadic Functions
• There are several functions in C and C++ that make use of
variable argument lists.
• The printf function in C is probably the best example of this.
• As is the format method of Strings in Java.
• In general this is not a good idea…
• But it does allow for some functionality that is otherwise awkward to
implement.
• This isn’t quite method overloading…
• … but close enough to discuss anyway.
Varargs Function
using namespace std;
int add_nums (int count, ...) {
va_list arg;
int sum;
va_start (arg, count);
for (int i = 0; i < count; i++) {
sum += va_arg (arg, int);
}
va_end (arg);
return sum;
}
int main(int argc, char** argv) {
cout << add_nums (2, 2, 3) << endl;
}
Why Use Varargs?
• Syntactically nicer to user.
• You don’t need to create arrays when you invoke a function.
• When overloading doesn’t meet our needs.
• Overloading does not always play well with varargs.
• Easier to read and understand the code.
• No messy collection manipulation.
Why Not Use Varargs?
• Badly designed functions lead to insecure code.
• Type conversions must be handled manually.
• Type checking can only be done at runtime in many
cases.
• Ideally you want problems to be flagged up by the compiler at
compile-time.
• Doesn’t play nicely with overloading.
Varargs in Java
• Java 1.5 made available the varargs system for the first
time.
• It’s syntatically much easier to do.
• Works in largely the same way.
• It’s an autoboxing feature – it creates the array for you.
• Varargs can be used only in the final argument position.
Varargs in Java
void sum_numbers (object ... nums) {
int total;
Integer num;
for (Object o : nums) {
num = (Integer)o;
total += o.intValue();
}
return total;
}
Summary
• Functions have unique signatures that identify them.
• The signature is the name, and then types and order of parameters.
• Overloading functions means that you can produce more
readable and more maintainable code.
• Variadic functions can be created to deal with situations where
array manipulation would be awkward or costly.
Ad

More Related Content

What's hot (20)

Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
Jussi Pohjolainen
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
Compilers
CompilersCompilers
Compilers
Bense Tony
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
Kevlin Henney
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
PHP Basic & Variables
PHP Basic & VariablesPHP Basic & Variables
PHP Basic & Variables
M.Zalmai Rahmani
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
Pritom Chaki
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
Kuppusamy P
 
Data structures & problem solving unit 1 ppt
Data structures & problem solving unit 1 pptData structures & problem solving unit 1 ppt
Data structures & problem solving unit 1 ppt
aviban
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in Compiler
Akhil Kaushik
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)
Jay Patel
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
satvirsandhu9
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
Kevlin Henney
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
Pritom Chaki
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
Kuppusamy P
 
Data structures & problem solving unit 1 ppt
Data structures & problem solving unit 1 pptData structures & problem solving unit 1 ppt
Data structures & problem solving unit 1 ppt
aviban
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in Compiler
Akhil Kaushik
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)
Jay Patel
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
satvirsandhu9
 

Viewers also liked (20)

Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
 
Socket programming in C#
Socket programming in C#Socket programming in C#
Socket programming in C#
Nang Luc Vu
 
Intro.net
Intro.netIntro.net
Intro.net
singhadarsh
 
Visual Studio Enterprise 2015 Overview atidan
Visual Studio Enterprise 2015 Overview   atidanVisual Studio Enterprise 2015 Overview   atidan
Visual Studio Enterprise 2015 Overview atidan
David J Rosenthal
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
Michael Heron
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
JavaTportal
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
Jussi Pohjolainen
 
c#.Net Windows application
c#.Net Windows application c#.Net Windows application
c#.Net Windows application
veera
 
C# simplified
C#  simplifiedC#  simplified
C# simplified
Mohd Manzoor Ahmed
 
The Psychology of C# Analysis
The Psychology of C# AnalysisThe Psychology of C# Analysis
The Psychology of C# Analysis
Coverity
 
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Xamarin
 
Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
prabhu rajendran
 
Microsoft Dynamics NAV 2013 R2 Overview and NAV Roadmap
Microsoft Dynamics NAV 2013 R2 Overview and NAV RoadmapMicrosoft Dynamics NAV 2013 R2 Overview and NAV Roadmap
Microsoft Dynamics NAV 2013 R2 Overview and NAV Roadmap
SociusPartner
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Ahmed Za'anin
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Raffaele Doti
 
Method overloading
Method overloadingMethod overloading
Method overloading
Azaz Maverick
 
Overloading in java
Overloading in javaOverloading in java
Overloading in java
774474
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
baabtra.com - No. 1 supplier of quality freshers
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
bhavesh prakash
 
Socket programming in C#
Socket programming in C#Socket programming in C#
Socket programming in C#
Nang Luc Vu
 
Visual Studio Enterprise 2015 Overview atidan
Visual Studio Enterprise 2015 Overview   atidanVisual Studio Enterprise 2015 Overview   atidan
Visual Studio Enterprise 2015 Overview atidan
David J Rosenthal
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
Michael Heron
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
JavaTportal
 
c#.Net Windows application
c#.Net Windows application c#.Net Windows application
c#.Net Windows application
veera
 
The Psychology of C# Analysis
The Psychology of C# AnalysisThe Psychology of C# Analysis
The Psychology of C# Analysis
Coverity
 
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Intro to Xamarin for Visual Studio: Native iOS, Android, and Windows Apps in C#
Xamarin
 
Microsoft Dynamics NAV 2013 R2 Overview and NAV Roadmap
Microsoft Dynamics NAV 2013 R2 Overview and NAV RoadmapMicrosoft Dynamics NAV 2013 R2 Overview and NAV Roadmap
Microsoft Dynamics NAV 2013 R2 Overview and NAV Roadmap
SociusPartner
 
Overloading in java
Overloading in javaOverloading in java
Overloading in java
774474
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Ad

Similar to 2CPP11 - Method Overloading (20)

CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
Michael Heron
 
2CPP19 - Summation
2CPP19 - Summation2CPP19 - Summation
2CPP19 - Summation
Michael Heron
 
CPP19 - Revision
CPP19 - RevisionCPP19 - Revision
CPP19 - Revision
Michael Heron
 
DAA Unit 1.pdf
DAA Unit 1.pdfDAA Unit 1.pdf
DAA Unit 1.pdf
Nirmalavenkatachalam
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method Overriding
Michael Heron
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
Heartin Jacob
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
Metin Ogurlu
 
overloading
overloadingoverloading
overloading
cpsivaku
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programming
Juggernaut Liu
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
Amr Abd El Latief
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
ssusere336f4
 
12.6-12.9.pptx
12.6-12.9.pptx12.6-12.9.pptx
12.6-12.9.pptx
WinterSnow16
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
luqman bawany
 
Clean code
Clean codeClean code
Clean code
Simon Sönnby
 
Refactoring: Improving the design of existing code. Chapter 6.
Refactoring: Improving the design of existing code. Chapter 6.Refactoring: Improving the design of existing code. Chapter 6.
Refactoring: Improving the design of existing code. Chapter 6.
Andrés Callejas González
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
Martin Odersky
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
Michael Heron
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
lokeshpappaka10
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method Overriding
Michael Heron
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
Heartin Jacob
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
Metin Ogurlu
 
overloading
overloadingoverloading
overloading
cpsivaku
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programming
Juggernaut Liu
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
ssusere336f4
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
luqman bawany
 
Refactoring: Improving the design of existing code. Chapter 6.
Refactoring: Improving the design of existing code. Chapter 6.Refactoring: Improving the design of existing code. Chapter 6.
Refactoring: Improving the design of existing code. Chapter 6.
Andrés Callejas González
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
Martin Odersky
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
Michael Heron
 
Complete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept itComplete PPT about the Java lokesh kept it
Complete PPT about the Java lokesh kept it
lokeshpappaka10
 
Ad

More from Michael Heron (20)

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
Michael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
Michael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
Michael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
Michael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
Michael Heron
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
Michael Heron
 
SAD04 - Inheritance
SAD04 - InheritanceSAD04 - Inheritance
SAD04 - Inheritance
Michael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
Michael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
Michael Heron
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
Michael Heron
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
Michael Heron
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
Michael Heron
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
Michael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
Michael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
Michael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
Michael Heron
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
Michael Heron
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
Michael Heron
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
Michael Heron
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
Michael Heron
 
Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
Michael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
Michael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
Michael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
Michael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
Michael Heron
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
Michael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
Michael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
Michael Heron
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
Michael Heron
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
Michael Heron
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
Michael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
Michael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
Michael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
Michael Heron
 

Recently uploaded (20)

Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...Exploring Code Comprehension  in Scientific Programming:  Preliminary Insight...
Exploring Code Comprehension in Scientific Programming: Preliminary Insight...
University of Hawai‘i at Mānoa
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRYLEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
LEARN SEO AND INCREASE YOUR KNOWLDGE IN SOFTWARE INDUSTRY
NidaFarooq10
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 

2CPP11 - Method Overloading

  • 2. Introduction • One of the facilities available in modern programming languages is that of method overloading. • The ability to have more than one method with the same name in a particular code space. • This facility can be use to great effect to permit consistency of an interface across objects. • We’ll talk about that today.
  • 3. Method Overloading • It is not the name of the method which uniquely identifies a method in C++. • It’s the method signature. • It is the combination of: • The name of the method • The type and order of parameters. • This allows us to reuse the same name for a function many times. • We’ve already seen this to a degree with constructor methods.
  • 4. Method Overloading • This allows us to re-use the same method name when doing so makes logical sense. • We don’t need two method names: • add_two_ints (int x, int y); • add_two_floats (float x, float y) • We just need one: • add_two_nums (int x, int y); • add_two_floats (float x, float y);
  • 5. Method Overloading • This allows us to reduce the cognitive burden needed to use our coded objects. • The fewer methods people have to memorize, the better. • This comes at a danger: • Overload methods only when it makes sense to do so. • When the output is the same, just the parameters differ. • Don’t use it when there are side-effects to choosing particular combinations of parameters. • As far as possible, their execution should be identical.
  • 6. Guidelines for Method Overloading • Like anything in programming, you can do this well or you can do it badly. • We’re going to aim to do it well. • There are some guidelines for overloading methods properly. • Use a consistent return type • Ensure consistent parameter names • Ensure consistent parameter orders • Use internal function redirection • Don’t overdo them
  • 7. Use A Consistent Return Type • One of the benefits of method overloading is that it reduces cognitive burden. • This benefit is lost when overloaded methods have inconsistent return types. • As with most guidelines, this is not an iron-cast rule. • Sometimes it makes sense, such as methods that perform arithmetic. • You don’t want to get an int back when you passed in floats.
  • 8. Ensure Consistent Parameter Names • If you call a parameter ‘name’ in one overloaded method, don’t call it ‘obName in another’ • This is something that is not such an issue for external parties, but has an impact of ease of maintenance. • Keep the names of your parameters the same. • Refactor the code to ensure this if necessary.
  • 9. Ensure Consistent Parameter Ordering • The meaning of parameters should remain consistent across overloaded methods: • void do_stuff (int x, int y, int z); • void do_stuff (int y, int x); • Mixing and matching the order of parameters is a sure-fire way to increase both cognitive burden and user frustration. • Including your own frustration later!
  • 10. Use Internal Function Redirection • As far as is possible, your overloaded methods should be public interfaces only. • Internally they should act as ‘wrappers’ around some internal ‘worker’ method. • The wrappers do only the minimal work required to redirect calls on to the proper method. • Supplying default values for missing info, doing type conversion, etc.
  • 11. Don’t Overdo It • Combinatorial explosion ensures we can’t provide implementations for all combinations of all parameters. • Or indeed, that we should attempt it. • It would be crazy to overload all of the possible combinations of provided and missing info. • Instead, we provide overloaded methods for those combinations that are most likely to make up the majority of calls. • We can provide a ‘all you can eat’ version too for those with more specialised requirements.
  • 12. Why Overload Methods? • Provides a consistent interface for your objects. • Reduces cognitive burden on learning new objects. • Don’t need to learn five methods that do much the same thing. • This is a feature in many older languages. • Makes code more readable. • Makes code more maintainable.
  • 13. Method Overloading Vs Polymorphism • Method overloading is often characterised as a kind of polymorphism. • It’s not really polymorphic, but opinions vary. • It’s closest match is to the idea of ad-hoc polymorphism • The set of possible support options is finite. • It is a tightly restricted version of parametric polymorphism. • Treating all variables without any reference to a specific type. • C#, Java and C++ all offer facilities to do this. • But method overloading isn’t it.
  • 14. Method Overloading Vs Polymorphism • Method overloading can be written in such a way as to make use of polymorphism. • But at its core, it does not adapt to the type of parameters it is given. • The choice as to which method to invoke is done at compile time. • Think of overloading not as a polymorphic facility but as syntactical sleight of hand.
  • 15. Variadic Functions • There are several functions in C and C++ that make use of variable argument lists. • The printf function in C is probably the best example of this. • As is the format method of Strings in Java. • In general this is not a good idea… • But it does allow for some functionality that is otherwise awkward to implement. • This isn’t quite method overloading… • … but close enough to discuss anyway.
  • 16. Varargs Function using namespace std; int add_nums (int count, ...) { va_list arg; int sum; va_start (arg, count); for (int i = 0; i < count; i++) { sum += va_arg (arg, int); } va_end (arg); return sum; } int main(int argc, char** argv) { cout << add_nums (2, 2, 3) << endl; }
  • 17. Why Use Varargs? • Syntactically nicer to user. • You don’t need to create arrays when you invoke a function. • When overloading doesn’t meet our needs. • Overloading does not always play well with varargs. • Easier to read and understand the code. • No messy collection manipulation.
  • 18. Why Not Use Varargs? • Badly designed functions lead to insecure code. • Type conversions must be handled manually. • Type checking can only be done at runtime in many cases. • Ideally you want problems to be flagged up by the compiler at compile-time. • Doesn’t play nicely with overloading.
  • 19. Varargs in Java • Java 1.5 made available the varargs system for the first time. • It’s syntatically much easier to do. • Works in largely the same way. • It’s an autoboxing feature – it creates the array for you. • Varargs can be used only in the final argument position.
  • 20. Varargs in Java void sum_numbers (object ... nums) { int total; Integer num; for (Object o : nums) { num = (Integer)o; total += o.intValue(); } return total; }
  • 21. Summary • Functions have unique signatures that identify them. • The signature is the name, and then types and order of parameters. • Overloading functions means that you can produce more readable and more maintainable code. • Variadic functions can be created to deal with situations where array manipulation would be awkward or costly.