9.8-10.2 - 9.8 Generic Subprograms software reuse ->...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
1 9.8 Generic Subprograms software reuse -> increased productivity It is efficient to have one sort routine that works for many parameter types i.e., a polymorphic subprogram one that takes parameters of different types on different activations
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 kinds of subprogram polymorphism ad hoc polymorphism: provided by overloaded subprograms general polymorphism: provided by subprograms in Ruby and Python, since formal param's do not have types parametric polymorphism : where a subprogram has generic parameters that accept various types
Background image of page 2
3 Examples of parametric polymorphism C++ uses templates Java 5.0 uses generic parameters Ada uses generic units that support construction of multiple versions of the subprogram to accept different types
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 9.8.1 Generic Supprog.s in Ada Ada provides parametric polymorphism by a kind of template that allows construction of multiple versions of program units the different versions are instantiated by the compiler on request from the user program the different versions have the same name, giving the illusion that a single subprogram does the job how is this different from overloading?
Background image of page 4
5 differences with overloading: the compiler does the writing the compiler-produced versions necessarily do exactly the same thing flexibility . .. you only generate the versions you need at this time. the number of parameters could be different between two overloaded subprograms . .. not so with generic subprograms
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 generic type Index_Type is (<>) ; type Element_Type is private ; type Vector is array (Integer range <> ) of Element_Type ; procedure Generic_Sort(List : in out Vector) ; procedure Generic_Sort(List : in out Vector) is Temp : Element_Type ; begin for Top in Index_Type'Succ(Top). .List'Last loop for Bottom in Index_Type'Succ(Top). .List'Last loop if List(Top) > List(Bottom) then Temp := List(Top) ; List(Top) := List(Bottom) ; List(Bottom) := Temp ; end if ; end loop ; - for Bottom end loop ; - for Top end Generic_Sort ; 3 generic parameters indicated by keyword type procedure Integer_Sort is new Generic_Sort( Index_Type => Integer ; Element_Type => Integer ; Vector => Int_Array) ; this call asks the compiler to generate specific code What is the range for Top?
Background image of page 6
7 parameters of the generic unit in the previous generic sort algorithm, the formal parameters to the generic unit were types: type Index_Type is (<>) ; type Element_Type is private ; type Vector is array (Integer range <> ) of Element_Type ; also possible is a parameter which is a function type
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Ada's function parameters Ada does not allow subprograms to be passed as parameters to other subprograms instead you can pass a function name to the generic unit, which gets the compiler to instantiate the template using your function name
Background image of page 8
9 generic
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/29/2010 for the course COMPUTER S cps615 taught by Professor Pro during the Spring '10 term at Randolph College.

Page1 / 34

9.8-10.2 - 9.8 Generic Subprograms software reuse ->...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online