1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
| !****************************************************
! Fortran 2003 でのオブジェクト指向のテスト
!
! date name version
! 2019.06.03 mk-mode.com 1.00 新規作成
!
! Copyright(C) 2019 mk-mode.com All Rights Reserved.
!****************************************************
!
module const
! SP: 単精度(4), DP: 倍精度(8)
integer, parameter :: SP = kind(1.0)
integer(SP), parameter :: DP = selected_real_kind(2 * precision(1.0_SP))
end module const
module mod_class
use const
implicit none
! クラス comp の宣言
type comp
private
real(DP) :: a = 0.0_DP
real(DP) :: b = 0.0_DP
contains
private
procedure, public :: comp_oblique
end type comp
! コンストラクタの宣言
interface comp
module procedure init_comp
end interface comp
contains
! コンストラクタ
! * インスタンス化時に取得した a, b をインスタンス変数に格納
!
! :param(in) real(8) a: 辺 A の長さ
! :param(in) real(8) b: 辺 B の長さ
! :return type(comp): インスタンス
type(comp) function init_comp(a, b) result(this)
implicit none
real(DP), intent(in) :: a, b
this%a = a
this%b = b
end function init_comp
! 斜辺の計算
! * 計算式: c = sqrt(a^2 + b^2)
!
! :param(in) type(comp) self: インスタンス
! :param(in) real(8) c: 斜辺の長さ
subroutine comp_oblique(self, c)
implicit none
class(comp) :: self
real(DP) :: c
c = sqrt(self%a * self%a + self%b * self%b)
end subroutine comp_oblique
end module mod_class
program class_test
use const
use mod_class
implicit none
type(comp) :: obj
real(DP) :: a, b, c
write (*, '(A)', advance="no") "2 real numbers ? "
read (*, *) a, b
obj = comp(a, b)
call obj%comp_oblique(c)
print '(A, F13.8)', "a = ", a
print '(A, F13.8)', "b = ", b
print '(A, F13.8)', "c = ", c
end program class_test
|