include 'class_Person.f90'
module class_Student ! filename class_Student.f90
use class_Person ! inherits class_Date
implicit none
public :: Student, set_DOM, print_DOM
type Student
private
type (Person) :: who ! name and sex
character (len=9) :: id ! ssn digits
type (Date) :: dom ! matriculation
integer :: credits
real :: gpa ! grade point average
end type Student
contains ! coupled functionality
function get_person (s) result (p)
type (Student), intent(in) :: s
type (Person) :: p ! name and sex
p = s % who ; end function get_person
function make_Student (w, n, d, c, g) result (x)
! Optional Constructor for a Student type
type (Person), intent(in) :: w ! who
character (len=*), optional, intent(in) :: n ! ssn
type (Date), optional, intent(in) :: d ! matriculation
integer, optional, intent(in) :: c ! credits
real, optional, intent(in) :: g ! grade point ave
type (Student) :: x ! new student
x = Student_(w, " ", Date_(1,1,1), 0, 0.) ! defaults
if ( present(n) ) x % id = n ! optional values
if ( present(d) ) x % dom = d
if ( present(c) ) x % credits = c
if ( present(g) ) x % gpa = g ; end function make_Student
subroutine print_DOM (who)
type (Student), intent(in) :: who
call print_Date(who%dom) ; end subroutine print_DOM
subroutine print_GPA (x)
type (Student), intent(in) :: x
print *, "My name is "; call print_Name (x % who)
print *, ", and my G.P.A. is ", x % gpa, "." ; end subroutine
subroutine set_DOM (who, m, d, y)
type (Student), intent(inout) :: who
integer, intent(in) :: m, d, y
who % dom = Date_( m, d, y) ; end subroutine set_DOM
function Student_ (w, n, d, c, g) result (x)
! Public Constructor for a Student type
type (Person), intent(in) :: w ! who
character (len=*), intent(in) :: n ! ssn
type (Date), intent(in) :: d ! matriculation
integer, intent(in) :: c ! credits
real, intent(in) :: g ! grade point ave
type (Student) :: x ! new student
x = Student (w, n, d, c, g) ; end function Student_
end module class_Student
fortran OOP(4) class_Student.f90
最新推荐文章于 2023-02-28 14:17:00 发布