Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28496 Discussions

understanding extends types and override

FortCpp
Beginner
920 Views

Dear FORTRAN users and developers,

I am trying to understand the object-oriented concepts in FORTRAN 2003 standards (or later). I have some knowledge in C++ so I think there are some common ideas between these two languages which can help me understand them better. Please help me to clarify a little bit since I don't find too many helpful explanation on the web.

In C++, the polymorphism is done through class derivation and member function overriding. One defines a "abstract" base class where almost all the virtual functions are defined. Different derived classes contains the actual implementation of them. So other functions just need to program based on the "abstract" class. Then they works for all derived classes.

I think in FORTRAN, the OOP is done in a similar way but there are some differences. In my opinion, it is needed to define a base type with some virtual functions just like C++. And other functions/subroutines should follow the member function definition in the base type. That's the way of resolving the reuse of the function/subroutines for all extends types.

I don't have a better idea on how to program the idea. Here is my first attempt:
 

type Basis
        integer                                 :: NBasis
    contains
        private
        procedure                               :: DoNothing
        generic, public                         :: Constructor => DoNothing
        generic, public                         :: AllocateBasis => DoNothing

endtype Basis

    type, extends(Basis) :: GridBasis
        private
        integer                                 :: NGrid
    contains
        private
        procedure                               :: ConstructorGrid1
        procedure                               :: ConstructorGrid2
        generic, public                         :: Constructor => ConstructorGrid1, ConstructorGrid2, ConstructorGrid3
        procedure                               :: AllocateGridReal
        procedure                               :: AllocateGridCplx
        generic, public                         :: AllocateBasis => AllocateGridReal, AllocateGridCplx
endtype GridBasis

 


First, how can I define the "AllocateBasis" in type Basis such that it works like the "virtual function" and all extended types must define their own version of "AllocateBasis"?

Second, how can I define "AllocateBasis" in type GridBasis? The definition here contains the real implementation of it.

Third, how can I make "AllocateBasis" in type GridBasis a overload function? i.e. there are real version and complex version and both of them are named "AllocateBasis" with real or complex input allocatable arrays.

Fourth, NOPASS vs PASS. As I understand, if PASS is set then there is a explicit pointer to the object. But when NOPASS is set, there is no such a thing. So PASS is simplify for clarification?

I appreciate any comment.

 

0 Kudos
2 Replies
IanH
Honored Contributor II
920 Views
0 Kudos
FortCpp
Beginner
920 Views

Yes. I post it there after I post it here. Problem solved thanks.

 

0 Kudos
Reply