module class_Date ! filename: class_Date.f90
implicit none
public :: Date ! and everything not "private"
! private :: month, day, year
type Date
private
integer :: month, day, year ; end type Date
contains ! encapsulated functionality
function Date_ (m, d, y) result (x) ! public constructor
integer, intent(in) :: m, d, y ! month, day, year
type (Date) :: x ! from intrinsic constructor
x = Date (m, d, y) ; end function Date_
subroutine print_Date (x) ! check and pretty print a date
type (Date), intent(in) :: x
character (len=*),parameter :: month_Name(12) = &
(/ "January ", "February ", "March ", "April ",&
"May ", "June ", "July ", "August ",&
"September", "October ", "November ", "December "/)
if ( x%month < 1 .or. x%month > 12 ) print *, "Invalid month"
if ( x%day < 1 .or. x%day > 31 ) print *, "Invalid day "
print *, trim(month_Name(x%month)),' ', x%day, ", ", x%year;
end subroutine print_Date
subroutine read_Date (x) ! read month, day, and year
type (Date), intent(out) :: x ! into intrinsic constructor
read *, x ; end subroutine read_Date
function set_Date (m, d, y) result (x) ! manual constructor
integer, optional, intent(in) :: m, d, y ! month, day, year
type (Date) :: x
x = Date (1,1,1997) ! default, (or use current date)
if ( present(m) ) x%month = m ; if ( present(d) ) x%day = d
if ( present(y) ) x%year = y ; end function set_Date
end module class_Date