Fortran Wiki
Object-oriented programming2

Objects example:

module my_classes
implicit none
        type, public :: class_c
                integer :: c

        end type class_c        

        type :: class_a
                type(class_c) :: objc
                integer :: a
                contains
                        procedure :: suba

        end type
contains
        subroutine suba(this,n)
                class(class_a),intent(INOUT) :: this
                integer n
                print *,"this is sub input value:",n
                print *,"this is attribute a from class a:",this%a
                print *,"this is attribute c from object c: ",this%objc%c

        end subroutine suba

end module my_classes

Program myprog
        use my_classes
        implicit none
        type(class_a) :: my_a
        my_a%a=11
        my_a%objc%c=111
        call my_a%suba(10)

end program myprog

output:
this is sub input value: 10
this is attribute a from class a: 11
this is attribute c from object c: 111

this program is valid for intel fortran 12.0.3

in order to make an attribute or a procedure private
you have to declare it as
integer,private :: attr
or
procedure,private :: sub

Simple inheritance:


module my_classes
implicit none
        type, public :: class_c
                integer :: c
                contains
                        procedure,private :: subc

        end type class_c

        type,extends(class_c) :: class_a
                integer :: a
                contains
                        procedure :: suba

        end type
contains
        subroutine suba(this,n)
                class(class_a),intent(INOUT) :: this
                integer n
                print *,"this is sub input value:",n
                print *,"this is attribute c from inherit class c:",this%c

        end subroutine suba

        subroutine subc(this)
                class(class_c),intent(INOUT)::this
                print *,"private subc"
        end subroutine subc



end module my_classes

Program myprog
        use my_classes
        implicit none
        type(class_a) :: my_a
        my_a%a=12
        my_a%c=456
        call my_a%suba(10)

end program myprog

output:
this is sub input value: 10
this is attribute c from inherit class c: 456

class_a inherits class_c and we can see the attribute c from
suba by printing this%c

Author Nikolaos J. Hatzopoulos