- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi there,
I am trying to make best use of Fortran's OOP features and I have a question regarding the overriding of type-bound procedures.
Is there a way in which I can define a type-bound procedure that can be overridden in child objects (objects which extend the base class) and have different dummy arguments?
At the moment I can override type-bound procedures but I have to have exactly the same number and type of dummy arguments. I had been thinking about generic procedures, but I couldn't figure out to tackle the problem.
Thanks,
Ewan
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You may want to refer to useful books suggested in Dr Fortran blog here.
Here's a simple example - the above mentioned books offer a lot of further details surrounding such techniques:
module m implicit none private type, public :: a integer :: i contains private procedure, pass(this) :: set_a generic, public :: set => set_a end type a type, extends(a), public :: b real, allocatable :: r(:) contains private procedure, pass(this) :: set_b generic, public :: set => set_b end type b contains subroutine set_a(this, val) class(a), intent(inout) :: this integer, intent(in) :: val this%i = val end subroutine set_a subroutine set_b(this, ival, rval) class(b), intent(inout) :: this integer, intent(in) :: ival real, intent(in) :: rval(:) this%i = ival this%r = rval end subroutine set_b end module m
program p use m, only : a, b implicit none type(a) :: foo type(b) :: bar call foo%set(42) call bar%set(42, [ 42.0, 84.0 ]) print *, " base a: foo%i = ", foo%i print *, " child b: bar%i, bar%r = ", bar%i, bar%r stop end program p
Upon execution,
base a: foo%i = 42 child b: bar%i, bar%r = 42 42.0000000 84.0000000
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks! I knew it would be simple in the end.
Apologies in advance for diverging on the topic but do you have to make all object variables in the parent class (the type 'a' in the above example) public in order for any child (extended class) to be able to access and use it?
Typically in my coding style, I use separate modules for each class (type).
Thanks again,
Ewan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ewan T. wrote:
..
Apologies in advance for diverging on the topic but do you have to make all object variables in the parent class (the type 'a' in the above example) public in order for any child (extended class) to be able to access and use it?
Typically in my coding style, I use separate modules for each class (type).
..
Ah, the rules for component accessibility with derived types in Fortran do tend to be somewhat confusing and, some may argue, limiting: the thing to note is the scope of private/public attributes of derived type components is modules. So if you have separate modules (generally a good design), private components will not be accessible and "getter/setter" methods will be needed to work with such data.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks again, I thought as much but wanted to check my thinking was correct.
Turns out I actually have one of the books recommended by Dr Fortran, the book by Arjen Markus. I will need to read this book a little bit more closely the next time!
I am surprised that he hasn't included Chapman's texts on Fortran, although there hasn't been one released for Fortran 2008 yet I do find the 2003 edition very helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My recommendation will be to first examine these two books:
- Modern Fortran Explained - Michael Metcalf, John Reid and Malcolm Cohen, Oxford University Press, 2011, ISBN 978-0199601417
- Modern Fortran - Norman Clerman and Walter Spector - Cambridge University Press, 2012, ISBN 978-0521514538
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I will pick up a copy of these books, learning the new Fortran 2008 standard is proving to be a bit challenging.
Thanks for all your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My post was about books with "Modern Fortran" in the name. There are several other good Fortran books out there, including Chapman's, which I often recommend to people. Perhaps I should add these to the post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ewan T. wrote:
.. learning the new Fortran 2008 standard is proving to be a bit challenging.
..
Once you go through the books in Quote #6, I think you'll find Fortran language itself quite straightfoward, even if verbose.
The challenge you'll find is more in efficient and extensible object-oriented design and its robust implementation using Fortran; the former is inherently difficult, regardless of the programming language, and the latter is presently an issue because of its relatively nascent state, both in terms of compiler capabilities as well as user applications.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page