A methodology for object-oriented programming in Fortran 90 has been developed. The methodology provides for emulation of the most important object-oriented concepts of C++; namely, classes (including abstract data types, encapsulation, and function overloading), inheritance, and dynamic dispatching. Previously, object-oriented programming in Fortran 90 was not considered possible.
This methodology makes the capabilities afforded by object-oriented techniques available to developers of complex Fortran-based scientific programs; thus, it becomes possible to combine some of the power and flexibility of C++ with the maturity and established scientific-computing capability of Fortran. An important advantage of this methodology is that Fortran programmers need not learn a new, complex language — viz. C++.
The most fundamental concept in C++ that must be implemented in Fortran 90 is the concept of classes. Each class contains a distinct data type and the procedures that can be performed by the class. The elements (or components) of the data type are the class data members, and the procedures are the class member functions (or methods). In Fortran 90, one defines a class as a module that contains exactly one abstract data type (called a "derived type") and the procedures that work exclusively on that type. A module in Fortran can also contain data that correspond to static class members in C++.
Another C++ concept that is essential to object-orientation and that must be implemented by hand in Fortran 90 is that of inheritance. Inheritance makes it possible to create a hierarchy of classes in which a base class contains the common properties of the hierarchy and derived classes can modify and specialize these properties. A derived class contains all the class data members of the base class and can add new class data members. Further, a derived class contains all the class member functions of the base class and can modify these functions or add new ones.
A third important C++ concept that must be implemented manually in Fortran 90 is that of dynamic dispatching or run-time polymorphism. By virtue of inheritance, a single method name can be made to respond differently to different objects in an inheritance hierarchy. Dynamic dispatching enables a single object name to refer to any member of an inheritance hierarchy and enables a procedure to resolve, at run time, which object is the referent. This ability is useful because it enables one to write a generic program for a whole class of related objects, yet make the program behave differently, depending on which object is in use.
To implement dynamic dispatching in Fortran 90, it is necessary to construct two features: (1) a pointer object that can point to any member in an inheritance hierarchy and (2) a dispatch mechanism (or method lookup) that can select, on the basis of the class referred to in the pointer object, the appropriate procedure (or method) to execute. In C++, these features are incorporated automatically by use of virtual functions. In Fortran 90, they are constructed by implementing a subtype class. Although the details of dynamic dispatching are thereby exposed to the programmer, they can be hidden from the procedures that make use of the subtype class, as in C++.
Multiple inheritance in C++ enables one to create composite classes that have the properties of their bases classes. In Fortran 90, one can implement a composite class by including the base classes as components in a module, along with the USE operator to bring the base classes into scope.
Unlike in C++, templates and parameterized types are not supported in Fortran 90, and no effective way to emulate them has been found. A Fortran 90 programmer must explicitly provide some features that are automatically available in C++. Fortran 90 allows only a limited number of operators to be overloaded.
In efforts to write object-oriented plasma-simulation computer codes in both Fortran 90 and C++, each of these languages was found to offer both advantages and disadvantages, some in addition to those mentioned above. In the C++ version of the code, it was necessary to create special classes to utilize self-describing multidimensional arrays, which were automatically available in Fortran 90. On the other hand, it was necessary to create special subtype classes in Fortran 90 to emulate the dynamic dispatching that was automatically available in C++. Because type checking in Fortran 90 was stricter than in C++, more errors were caught by the compiler and debugging proceeded more quickly. It was necessary to write more code in Fortran 90. The execution time for the Fortran 90 version was about half that of the C++ version.
This work was done by Viktor K. Decyk and Charles D. Norton of Caltech and Boleslaw K. Szymanski of RPI for NASA's Jet Propulsion Laboratory. For further information, access the Technical Support Package (TSP) free on-line at www.techbriefs.com under the Mathematics and Information Sciences category, or circle no. 112on the TSP Order Card in this issue to receive a copy by mail ($5 charge).NPO-20180
This Brief includes a Technical Support Package (TSP).
Expressing object-oriented concepts in Fortran 90
(reference NPO20180) is currently available for download from the TSP library.
Don't have an account? Sign up here.