File: classes2.f90

package info (click to toggle)
lfortran 0.58.0-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 54,508 kB
  • sloc: cpp: 162,179; f90: 68,251; python: 17,476; ansic: 6,278; yacc: 2,334; sh: 1,317; fortran: 892; makefile: 34; javascript: 15
file content (46 lines) | stat: -rw-r--r-- 949 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
! Source - Page 44 of https://personalpages.manchester.ac.uk/staff/david.d.apsley/lectures/fortran/fortranB.pdf
module Defs
   implicit none
   private
   public point, point2d

    type, abstract :: point
        contains
        procedure(func), deferred :: radius
    end type point

    abstract interface
        real function func( this )
            import point
            class(point) this
        end function func
    end interface

    type, extends(point) :: point2d
        real x, y
    contains
        procedure :: radius => r2d
    end type point2d

contains

    real function r2d( this )
        class(point2d) this
        r2d = sqrt( this%x ** 2 + this%y ** 2 )
    end function r2d

end module Defs

program main
use Defs
implicit none

    real :: res
    class(point), pointer :: ptr

    type(point2d), target :: p2d = point2d( 3, 4 )

    ptr => p2d
    res = ptr%radius()
    if (res /= 5.0) error stop
end program main