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 ProgrammingCode Using Private and Publicmodule shape_modprivate ! hide the type-bound procedure implementation procedurespublic :: shape, constructor ! allow access to shape & constructor proceduretype shapeprivate ! hide the underlying detailsinteger :: colorlogical :: filledinteger :: xinteger :: ycontainsprivate! hide the type bound procedures by defaultprocedure :: initShape ! private type-bound procedureprocedure, public :: isFilled ! allow access to isFilled type-boundprocedureprocedure, public :: print ! allow access to print type-bound procedureend type shapecontainslogical function isFilled(this)class(shape) :: thisisFilled = this%filledend functionfunction constructor(color, filled, x, y)type(shape) :: constructorinteger :: colorlogical :: filledinteger :: xinteger :: ycall constructor%initShape(color, filled, x, y)end functionsubroutine initShape(this, color, filled, x, y)! initialize shape objectsclass(shape) :: thisinteger :: colorlogical :: filledinteger :: xinteger :: ythis%color = colorthis%filled = filledthis%x = xthis%y = yend subroutinesubroutine print(this)class(shape) :: thisprint *, this%color, this%filled, this%x, this%yend subroutineend module<strong>The</strong> preceding example uses information hiding in the host module as well as in the shape type.<strong>The</strong> private statement, located at the top of the module, enables information hiding on all moduledata and procedures. <strong>The</strong> isFilled module procedure, which is not to be confused with theisFilled type-bound procedure, is hidden as a result of the private statement at the top of themodule. <strong>The</strong> public :: constructor allows the user to invoke the constructor moduleprocedure. <strong>The</strong>re is also a private statement on the data components of shape. Now, the only waya user can query the filled component is through the isFilled type-bound procedure, which isdeclared public.<strong>PGI</strong> <strong>Fortran</strong> <strong>Reference</strong> Guide 134

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

Saved successfully!

Ooh no, something went wrong!