! Areas of shapes of different ADTs, using different ! function names in each ADT module ADT_Rectangle ! define the first object ADT implicit none type Rectangle real :: base, height ; end type Rectangle contains ! Computation of area for rectangles. function rectangle_area ( r ) result ( area ) type ( Rectangle ), intent(in) :: r real :: area area = r%base * r%height ; end function rectangle_area end module ADT_Rectangle module ADT_Circle ! define the second object ADT implicit none real :: pi = 3.1415926535897931d0 ! a circle constant type Circle real :: radius ; end type Circle contains ! Computation of area for circles. function circle_area ( c ) result ( area ) type ( Circle ), intent(in) :: c real :: area area = pi * c%radius**2 ; end function circle_area end module ADT_Circle program geometry ! for both types in a single function use ADT_Circle use ADT_Rectangle implicit none ! Interface to generic routine to compute area for any type interface compute_area module procedure rectangle_area, circle_area ; end interface ! Declare a set geometric objects. type ( Rectangle ) :: four_sides type ( Circle ) :: two_sides ! inside, outside real :: area = 0.0 ! the result ! Initialize a rectangle and compute its area. four_sides = Rectangle ( 2.1, 4.3 ) ! implicit constructor area = compute_area ( four_sides ) ! generic function write ( 6,100 ) four_sides, area ! implicit components list 100 format ("Area of ",f3.1," by ",f3.1," rectangle is ",f5.2) ! Initialize a circle and compute its area. two_sides = Circle ( 5.4 ) ! implicit constructor area = compute_area ( two_sides ) ! generic function write ( 6,200 ) two_sides, area 200 format ("Area of circle with ",f3.1," radius is ",f9.5 ) end program geometry ! Running gives: ! Area of 2.1 by 4.3 rectangle is 9.03 ! Area of circle with 5.4 radius is 91.60885