18
Introduction Summary References OVERLOADING Muhammad Adil Raja Roaming Researchers, Inc. cbna April 20, 2015

Overloading

Embed Size (px)

Citation preview

Page 1: Overloading

Introduction Summary References

OVERLOADING

Muhammad Adil Raja

Roaming Researchers, Inc.

cbna

April 20, 2015

Page 2: Overloading

Introduction Summary References

OUTLINE I

INTRODUCTION

SUMMARY

REFERENCES

Page 3: Overloading

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.

Page 4: Overloading

Introduction Summary References

A DEFINITION OF OVERLOADING

• We say a term is overloaded if it has two or moremeanings.

• Most words in natural languages are overloaded, andconfusion 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.

Page 5: Overloading

Introduction Summary References

OVERLOADING BASED ON SCOPES I

• A name scope defines the portion of a program in which aname can be used, or the way it can be used. Scopes areintroduced 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.

Page 6: Overloading

Introduction Summary References

OVERLOADING BASED ON SCOPES II

AN EXAMPLE

Procedure Fr iend . sendFlowersTo ( anAddress : address ) ;begin

go to f l o r i s t ;g ive 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 thenmake up f l ower arrangementt e l l d e l i v e r y person sendFlowersTo ( anAddress ) ;

elselook up f l o r i s t near anAddressphone f l o r i s tg ive f l o r i s t message sendFlowersTo ( anAddress )

end ;

Page 7: Overloading

Introduction Summary References

RESOLVING OVERLOADED NAMES

• This type of overloading is resolved by looking at the typeof 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.

Page 8: Overloading

Introduction Summary References

OVERLOADING BASED ON TYPE SIGNATURES

A different type of overloading allows multiple implementationsin the same scope to be resolved using type signatures.

TYPE SIGNATURES

class Example {/ / same name, th ree d i f f e r e n t methods

i n t sum ( i n t a ) { return a ; }i n t sum ( i n t a , i n t b ) { return a + b ; }i n t sum ( i n t a , i n t b , i n t c ) { return a + b + c ; }

}

• A type signature is the combination of argument types andreturn type.

• By looking at the signature of a call, can tell which versionis intended.

Page 9: Overloading

Introduction Summary References

RESOLUTION PERFORMED AT COMPPILE TIME

Note that resolution is almost always performed at compiletime, based on static types, and not dynamic values.

RESOLUTION

class Parent { . . . } ;

class Chi ld : public Parent { . . . } ;

void Test ( Parent ∗ p ) { cout << " i n parent " << endl ; }void Test ( Ch i ld ∗ c ) { cout << " i n c h i l d " << endl }

Parent ∗ value = new Chi ld ( ) ;

Test ( value ) ;

Example will, perhaps surprizingly, execute parent function.

Page 10: Overloading

Introduction Summary References

STREAM OUTPUT IN C++

Stream output in C++ is a good example of the power ofoverloading. Every primitive type has a different stream outputfunction.

STREAM IOostream & operator << ( ostream & des t i na t i on , i n t source ) ;

ostream & operator << ( ostream & des t i na t i on , short source ) ;ostream & operator << ( ostream & des t i na t i on , long source ) ;ostream & operator << ( ostream & des t i na t i on , char source ) ;ostream & operator << ( ostream & des t i na t i on , char ∗ source ) ;/ / . . . and so on

double d = 3 .14 ;cout << " The answer i s " << d << ’ \ n ’ ;

Page 11: Overloading

Introduction Summary References

EASY TO EXTEND

• Since output uses overloading, it is very easy to extend tonew types.

EXTEND

class Frac t i on {public :

F rac t i on ( i n t top , i n t bottom ) { t = top ; b = bottom ; }

i n t numerator ( ) { return t ; }i n t denominator ( ) { return b ; }

private :i n t t , b ;

} ;

ostream & operator << ( ostream & des t i na t i on , F rac t i on & source ){

d e s t i n a t i o n << source . numerator ( ) << " / " << source . denominator ( ) ;return d e s t i n a t i o n ;

}

F rac t i on f (3 , 4 ) ;cout << " The value o f f i s " << f << ’ \ n ’ ;

Page 12: Overloading

Introduction Summary References

CONVERSION AND COERCIONS

• When one adds conversions into the mix, resolvingoverloaded 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.

Page 13: Overloading

Introduction Summary References

REDEFINITIONS

• A redefinition occurs when a child class changes the typesignature 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.

Page 14: Overloading

Introduction Summary References

AN EXAMPLE ILLUSTRATING REDEFINITION MODELS

• The following example will illustrate the difference in thesetwo models.

REDEFINITION MODELS

class Parent {public void example ( i n t a )

{ System . out . p r i n t l n ( " i n parent method " ) ; }}

class Chi ld extends Parent {public void example ( i n t a , i n t b )

{ System . out . p r i n t l n ( " i n c h i l d method " ) ; }}

Ch i ld aChi ld = new Chi ld ( ) ;aChi ld . example ( 3 ) ;

Will execute parent method in Java and C# (Merge model) andgive error in C++ (hierarchical model). Delphi allowsprogrammer control over this.

Page 15: Overloading

Introduction Summary References

OPTIONAL PARAMETERS

• Some languages allow the programmer to create optionalparameters, usually only at the end of the parameter list:

PARAMETERS

f u n c t i o n Count (A, B : I n tege r ; C : I n tege r = 0 ; D : I n tege r = 0 ) ;begin

(∗ Resul t i s a pseudo−v a r i a b l e used ∗)(∗ to represent r e s u l t o f any f u n c t i o n ∗)

Resul t := A + B + C + D;end

beginWr i t e l n ( Count (2 , 3 , 4 , 5 ) ) ; / / can use fou r argumentsWr i te l n ( Count (2 , 3 , 4 ) ) ; / / or th reeWr i te l n ( Count (2 , 3 ) ) ; / / or two

end

Such a program will have more than one type signature.

Page 16: Overloading

Introduction Summary References

ARBITRARY NUMBER OF ARGUMENTS IN C#

• The language C# has an interesting way to includearbitrary number of arguments:

ARGUMENTS

class ParamsExample {public void Wri te ( i n t x ) {

/ / use t h i s w i th one argumentWr i t eS t r i ng ( " Example one " ) ;Wr i t eS t r i ng ( x . ToStr ing ( ) ) ;

}

public void Wri te ( double x , i n t y ) {/ / use t h i s w i th two argumentsWr i t eS t r i ng ( " Example two " ) ;Wr i t eS t r i ng ( x . ToStr ing ( ) ) ;Wr i t eS t r i ng ( y . ToStr ing ( ) ) ;

}

public void Wri te ( params ob jec t [ ] args ) {/ / use t h i s w i th any other combinat ionWr i t eS t r i ng ( " Example three " ) ;for ( i n t i = 0 ; i < args . GetLength ( 0 ) ; i ++)

Wr i t eS t r i ng ( args [ i ] . ToStr ing ( ) ) ;}

}

ParamsExample p ;p . Wr i te ( 4 2 ) ;

Example one 42p . Wr i te (3 .14 ,159 ) ;

Example two 3.14159p . Wr i te ( 1 , 2 , 3 , 4 ) ;

Example three 1234p . Wr i te ( 3 . 1 4 ) ;

Example three 3.14p . Wr i te (3 , " abc " ) ;

Example three 3abc

Page 17: Overloading

Introduction Summary References

SUMMARY

• In this chapter we have looked at various aspects ofoverloading.

• Overloading based on Scopes.• Overloading based on Type Signatures.• Redefinition.• Polyadicity (functions with variable number of arguments).

Page 18: Overloading

Introduction Summary References

REFERENCES

• Images and content for developing these slides have beentaken from the follwoing book with the permission of theauthor.

• An Introduction to Object Oriented Programming, TimothyBudd.

• This presentation is developed using Beamer:• Singapore, spruce.