10.07.2015 Views

PGI Fortran Reference manual - The Portland Group

PGI Fortran Reference manual - The Portland Group

PGI Fortran Reference manual - The Portland Group

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Object Oriented ProgrammingA type-bound procedure's passed-object dummy must match the type of the derived type thatdefined it. Other than differing passed-object dummy arguments, the interface for the child'soverriding type-bound procedure is identical with the interface for the parent's type-boundprocedure. Both type-bound procedures are invoked in the same manner:type(shape) :: shp! declare an instance of shapetype(rectangle) :: rect! declare an instance of rectangletype(square) :: sq! declare an instance of squarecall shp%initialize(1, .true., 10, 20) ! calls initShapecall rect%initialize(2, .false., 100, 200, 11, 22) ! calls initRectanglecall sq%initialize(3, .false., 400, 500) ! calls initRectanglesq is declared square but its initialize type-bound procedure invokes initRectangle becausesq inherits the rectangle version of initialize.Although a type may override a type-bound procedure, it is still possible to invoke the versiondefined by a parent type. Each type extension contains an implicit parent object of the same nameand type as the parent. You can use this implicit parent object to access components specific to aparent, say, a parent's version of a type-bound procedure, as illustrated here:call rect%shape%initialize(2, .false., 100, 200) ! calls initShapecall sq%rectangle%shape%initialize(3, .false., 400, 500) ! calls initShapeIf you do not want a child to override a parent's type-bound procedure, you can use theNON_OVERRIDABLE binding-attribute to prevent any type extensions from overriding aparticular type-bound procedure:type shapeinteger :: colorlogical :: filledinteger :: xinteger :: ycontainsprocedure, non_overridable :: initializeend type shape7.2.8. Functions as Type-Bound ProceduresIn the preceding examples, subroutines implement type-bound procedures. You can alsoimplement type-bound procedures with functions. <strong>The</strong> following example uses a function thatqueries the status of the filled component in shape.module shape_modtype shapeinteger :: colorlogical :: filledinteger :: xinteger :: ycontainsprocedure :: isFilledend type shapecontainslogical function isFilled(this)class(shape) :: thisisFilled = this%filledend functionend module<strong>PGI</strong> <strong>Fortran</strong> <strong>Reference</strong> Guide 132

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!