fortran OOP(4) class_Student.f90

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=*), optionalintent(in) :: n ! ssn
   
type (Date),       optionalintent(in) :: d ! matriculation
   
integer,           optionalintent(in) :: c ! credits
   
real,              optionalintent(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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yueliang2100

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值