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.