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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Object Oriented ProgrammingType-Bound Procedure using an External Procedure<strong>The</strong> following example is a type-bound procedure that uses an external procedure with an explicitinterface:module shape_modtype shapeinteger :: colorlogical :: filledinteger :: xinteger :: ycontainsprocedure :: initializeend type shapetype, extends(shape) :: rectangleinteger :: lengthinteger :: widthend type rectangletype, extends(rectangle) :: squareend type squareinterfacesubroutine initialize(sh, color, filled, x, y, length, width)import shapeclass(shape) :: shinteger :: colorlogical :: filledinteger :: xinteger :: yinteger, optional :: lengthinteger, optional :: widthend subroutineend interfaceend moduleUsing the preceding examples, we can invoke the type-bound procedure in the following manner:use shape_modtype(shape) :: shp! declare an instance of shapecall shp%initialize(1, .true., 10, 20) ! initialize shape<strong>The</strong> syntax for invoking a type-bound procedure is very similar to accessing a data componentin a derived type. <strong>The</strong> name of the component is preceded by the variable name separated by apercent (%) sign. In this case, the name of the component is initialize and the name of thevariable is shp. To access the initialize type-bound procedure, type shp%initialize. Usingthe preceding invocation calls the initialize subroutine and passes in 1 for color, .true.for filled, 10 for x, and 20 for y.But what about the first dummy argument, sh, in initialize? This dummy argument is knownas the passed-object dummy argument. By default, the passed-object dummy is the first dummyargument in the type-bound procedure. It receives the object that invoked the type-boundprocedure. In our example, sh is the passed-object dummy and the invoking object is shp.<strong>The</strong>refore, the shp object gets assigned to sh when initialize is invoked.<strong>The</strong> passed-object dummy argument must be declared CLASS and of the same type as thederived type that defined the type-bound procedure. For example, a type bound proceduredeclared in shape must have a passed-object dummy argument declared "class(shape)".<strong>PGI</strong> <strong>Fortran</strong> <strong>Reference</strong> Guide 128

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

Saved successfully!

Ooh no, something went wrong!