5 ! Started to fail when finalization was added.
7 ! Contributed by Ian Chivers in PR fortran/44465
15 procedure
, pass(this
) :: getx
16 procedure
, pass(this
) :: gety
17 procedure
, pass(this
) :: setx
18 procedure
, pass(this
) :: sety
19 procedure
, pass(this
) :: moveto
20 procedure
, pass(this
) :: draw
23 interface assignment(=)
24 module procedure generic_shape_assign
29 integer function getx(this
)
31 class (shape_type
) , intent(in
) :: this
35 integer function gety(this
)
37 class (shape_type
) , intent(in
) :: this
41 subroutine setx(this
,x
)
43 class (shape_type
), intent(inout
) :: this
44 integer , intent(in
) :: x
48 subroutine sety(this
,y
)
50 class (shape_type
), intent(inout
) :: this
51 integer , intent(in
) :: y
55 subroutine moveto(this
,newx
,newy
)
57 class (shape_type
), intent(inout
) :: this
58 integer , intent(in
) :: newx
59 integer , intent(in
) :: newy
66 class (shape_type
), intent(in
) :: this
67 print *,' x = ' , this
%x_
68 print *,' y = ' , this
%y_
71 subroutine generic_shape_assign(lhs
,rhs
)
73 class (shape_type
) , intent(out
) , allocatable
:: lhs
74 class (shape_type
) , intent(in
) :: rhs
75 print *,' In generic_shape_assign'
76 if ( allocated(lhs
) ) then
79 allocate(lhs
,source
=rhs
)
80 end subroutine generic_shape_assign
82 end module shape_module
90 type , extends(shape_type
) :: circle_type
96 procedure
, pass(this
) :: getradius
97 procedure
, pass(this
) :: setradius
98 procedure
, pass(this
) :: draw
=> draw_circle
104 integer function getradius(this
)
106 class (circle_type
) , intent(in
) :: this
107 getradius
=this
%radius_
108 end function getradius
110 subroutine setradius(this
,radius
)
112 class (circle_type
) , intent(inout
) :: this
113 integer , intent(in
) :: radius
115 end subroutine setradius
117 subroutine draw_circle(this
)
119 class (circle_type
), intent(in
) :: this
120 print *,' x = ' , this
%x_
121 print *,' y = ' , this
%y_
122 print *,' radius = ' , this
%radius_
123 end subroutine draw_circle
125 end module circle_module
130 module rectangle_module
134 type , extends(shape_type
) :: rectangle_type
141 procedure
, pass(this
) :: getwidth
142 procedure
, pass(this
) :: setwidth
143 procedure
, pass(this
) :: getheight
144 procedure
, pass(this
) :: setheight
145 procedure
, pass(this
) :: draw
=> draw_rectangle
147 end type rectangle_type
151 integer function getwidth(this
)
153 class (rectangle_type
) , intent(in
) :: this
155 end function getwidth
157 subroutine setwidth(this
,width
)
159 class (rectangle_type
) , intent(inout
) :: this
160 integer , intent(in
) :: width
162 end subroutine setwidth
164 integer function getheight(this
)
166 class (rectangle_type
) , intent(in
) :: this
167 getheight
=this
%height_
168 end function getheight
170 subroutine setheight(this
,height
)
172 class (rectangle_type
) , intent(inout
) :: this
173 integer , intent(in
) :: height
175 end subroutine setheight
177 subroutine draw_rectangle(this
)
179 class (rectangle_type
), intent(in
) :: this
180 print *,' x = ' , this
%x_
181 print *,' y = ' , this
%y_
182 print *,' width = ' , this
%width_
183 print *,' height = ' , this
%height_
185 end subroutine draw_rectangle
187 end module rectangle_module
200 class (shape_type
) , allocatable
:: shape_v
203 type (shape_w
) , dimension(3) :: p
207 p(1)%shape_v
=shape_type(10,20)
208 call p(1)%shape_v
%draw()
212 p(2)%shape_v
=circle_type(100,200,300)
213 call p(2)%shape_v
%draw()
215 print *,' rectangle '
217 p(3)%shape_v
=rectangle_type(1000,2000,3000,4000)
218 call p(3)%shape_v
%draw()
220 end program polymorphic