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 ProgrammingAlthough you could easily create a linked list with just the preceding link object, the realpower of Object Oriented Programming lies in its ability to create flexible and reusablecomponents. However, the user must understand how the list is constructed with the linkobject; in this example, the link constructor assigns its result to the linkList pointer.4. To take advantage of OOP, create another object called list that acts as the "ApplicationProgram Interface" or API to the linked list data structure.type listclass(link),pointer :: firstLink => null() ! first link in listclass(link),pointer :: lastLink => null() ! last link in listcontainsprocedure :: addInteger ! add integer to listprocedure :: addChar ! add character to listprocedure :: addReal ! add real to listprocedure :: addValue ! add class(*) to listgeneric :: add => addInteger, addChar, addReal, addValueend type list<strong>The</strong> list derived type has two data components, firstlink, which points to the first link in itslist and lastLink which points to the last link in the list. <strong>The</strong> lastLink pointer allows theuser to easily add values to the end of the list.<strong>The</strong>re are four type-bound procedures called addInteger(), addChar(), addReal(), andaddValue(). You use the first three procedures to add an integer, a character, and a real to thelinked list respectively. <strong>The</strong> addValue() procedure adds class(*) values to the list and isthe main add routine. <strong>The</strong> addInteger(), addChar(), and addReal() procedures areactually just wrappers to the addValue() procedure.<strong>The</strong> addInteger() procedure takes an integer value and allocates a class(*) with that valueusing sourced allocation.subroutine addInteger(this, value)class(list) :: thisinteger valueclass(*), allocatable :: vallocate(v,source=value)call this%addValue(v)end subroutine addInteger<strong>The</strong> only difference between addInteger(), addChar(), and addReal() is the data typedummy argument, value.<strong>The</strong> value from the procedure is passed to the addValue() procedure:subroutine addValue(this, value)class(list) :: thisclass(*), valueclass(link), pointer :: newLinkif (.not. associated(this%firstLink)) thenthis%firstLink => link(value, this%firstLink)this%lastLink => this%firstLinkelsenewLink => link(value, this%lastLink%nextLink())call this%lastLink%setNextLink(newLink)this%lastLink => newLinkend ifend subroutine addValue<strong>PGI</strong> <strong>Fortran</strong> <strong>Reference</strong> Guide 143

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

Saved successfully!

Ooh no, something went wrong!