150 likes | 237 Vues
Support for OOP in Fortran (90 and some 95). By Samuel Robinson. Support for OOP in Fortran(90). ~General Characteristics -Backwards compatible with Fortran 77 -Classes are supported (keyword: module) -Supports static and dynamic binding (bdstatic, bdynamic) and heap dynamic
E N D
Support for OOP in Fortran (90 and some 95) By Samuel Robinson
Support for OOP in Fortran(90) ~General Characteristics -Backwards compatible with Fortran 77 -Classes are supported (keyword: module) -Supports static and dynamic binding (bdstatic, bdynamic) and heap dynamic variables -Supports polymorphism /dynamic binding (Fortran 95)
Support for OOP in Fortran (90) Inheritance -Single – only one member inherits -Selective Single – specifies what to inherit -Single with Local Renaming -Multiple Selective
Single Inheritance Module new_class_name Use base_class_name !make new attribute declarations Contains !declare new member definitions End module new_class_name
Selective Single Inheritance Module new_class_name Use base_class_name; only: list_of_entities !declare new attributes Contains !declare new member definitions End module new_class_name
Single Inheritance with Local Renaming Module new_class_name Use base_class name; local_name=> base_entity_name !new attribute declarations Contains !new member declarations End module new_class_name
Multiple Selective Inheritance with Renaming Module new_class_name Use base1_class_name Use base2_class_name Use base3_class_name; list of entities Use base4_class_name, local_name => base entity name !new member definitions Contains !new member definitions End module new_class_name
Examples module student !declares student class INTEGER idnumber end module student module class !declares student class INTEGER classnumber CHARACTER description end module class module student_information !new class use student use class, only: classnumber end module student_information
Examples module student INTEGER idnumber contains subroutine grade grade = average*4 end subroutine grade end module student module class use student, classnumber =>idnumber end module class
Public and Private Source: IncyWincy.com module Room_class implicit none public type Room private integer :: roomNumber logical, dimension(4) :: walls end type Room integer, parameter :: NORTH =1, SOUTH = 2.. contains
This Source: IncyWincy.com To make sure that a type is given the correct procedure, an object of that type needs to be passed. To do this, give the object the name “this” and make it first argument Ex: Subroutine new_Room (this, roomNumber, N,S, E, W) type(Room) :: this ...
Support for OOP in Fortran(95) Dynamic Binding – lets a single object refer to any member of an inheritance hierarchy and allows a procedure to resolve at run time whch object is being referred to Necessary : id mechanism to keep track of the class of the object and method lookup to select the right procedure to execute
Polymorphic Type Source: rsimplifiedRT.cwk type poly_stopwatch private type(stopwatch), pointer :: s Type (parallel_stopwatch), pointer :: p end type poly_stopwatch
Use poly_stopwatch_class type(stopwatch) s type(parallel_stopwatch) p type(poly_stopwatch) sw call new_stopwatch(s) call new_parallel_stopwatch(p) sw = poly(s) call split(sw, 'bar') call bar() call split(sw, 'bar') call report(sw, 6) sw = poly(p) call split(sw, 'foo') call foo() call split(sw, 'foo') call report(sw, 6)
Evaluation ~Fortran is object based, but not object oriented. -Inheritance and run time polymorphism are not directly supported, rather they are emulated. -Different, yet the similar.