next up previous contents
Next: Generic Procedures Up: Elements of object-oriented programming Previous: Public and Private Entities   Contents

Derived Type Constructors

Can support default values, optional and keyword arguments. Subsequent modifications of the type internal structure can be made invisible to the user.

module d3  
 type coords  
  real :: x,y,z  
 end type coords  
 type(coords) function init_coords(x,y,z)  
  real, intent(in), optional :: x,y,z  
  init\_coords = coords(0.0,0.0,0.0)  
  if(present(x)) init\_coords%x = x  
  if(present(y)) init\_coords%y = y  
  if(present(z)) init\_coords%z = z  
 end function init_coords  
 subroutine print_coords(c)  
  type(coords), intent(in) :: c  
  print*, c%x,c%y,c%z  
 end subroutine print_coords  
end module d3

The components of a Coords type object are not visible to the user; they can only be accessed through the functions contained in the module d3. In particular, the components cannot be printed in a normal IO statement; a module procedure Print_Coords is needed for this.

Adrian Sandu 2001-08-26