14-02-2013, 03:46 PM
Object Oriented Programming Concepts
1Object Oriented.pdf (Size: 119.01 KB / Downloads: 84)
Introduction
The use of Object Oriented (OO) design and Object Oriented Programming (OOP) are becoming increasingly
popular. Thus, it is useful to have an introductory understanding of OOP and some of the
programming features of OO languages. You can develop OO software in any high level language, like
C or Pascal. However, newer languages such as Ada, C++, and F90 have enhanced features that make
OOP much more natural, practical, and maintainable. C++ appeared before F90 and currently, is probably
the most popular OOP language, yet F90 was clearly designed to have almost all of the abilities of
C++ . However, rather than study the new standards many authors simply refer to the two decades old
F77 standard and declare that Fortran can not be used for OOP. Here we will overcome that misinformed
point of view.
Modern OO languages provide the programmer with three capabilities that improve and simplify
the design of such programs: encapsulation, inheritance, and polymorphism (or generic functionality).
Related topics involve objects, classes, and data hiding. An object combines various classical data types
into a set that defines a new variable type, or structure. A class unifies the new entity types and supporting
data that represents its state with routines (functions and subroutines) that access and/or modify those
data. Every object created from a class, by providing the necessary data, is called an instance of the
class. In older languages like C and F77, the data and functions are separate entities. An OO language
provides a way to couple or encapsulate the data and its functions into a unified entity. This is a more
natural way to model real-world entities which have both data and functionality. The encapsulation is
done with a “module” block in F90, and with a “class” block in C++. This encapsulation also includes
a mechanism whereby some or all of the data and supporting routines can be hidden from the user. The
accessibility of the specifications and routines of a class is usually controlled by optional “public” and
“private” qualifiers. Data hiding allows one the means to protect information in one part of a program
from access, and especially from being changed in other parts of the program. In C++ the default is
that data and functions are “private” unless declared “public,” while F90 makes the opposite choice for
its default protection mode. In a F90 “module” it is the “contains” statement that, among other things,
couples the data, specifications, and operators before it to the functions and subroutines that follow it.
Encapsulation, Inheritance, and Polymorphism
We often need to use existing classes to define new classes. The two ways to do this are called composition
and inheritance. We will use both methods in a series of examples. Consider a geometry program
that uses two different classes: class Circle and class Rectangle, as represented graphically in
Figs. 3.1 and 3.2. and as partially implemented in F90 as shown in Fig. 3.3. Each class shown has the
data types and specifications to define the object and the functionality to compute their respective areas
(lines 3–22). The operator % is employed to select specific components of a defined type. Within the
geometry (main) program a single routine, compute area, is invoked (lines 38 and 44) to return the
area for any of the defined geometry classes. That is, a generic function name is used for all classes
of its arguments and it, in turn, branches to the corresponding functionality supplied with the argument
class. To accomplish this branching the geometry program first brings in the functionality of the desired
classes via a “use” statement for each class module (lines 25 and 26). Those “modules” are coupled to
the generic function by an “interface” block which has the generic function name compute area (lines
28, 29). There is included a “module procedure” list which gives one class routine name for each of the
classes of argument(s) that the generic function is designed to accept. The ability of a function to respond
differently when supplied with arguments that are objects of different types is called polymorphism.
In this example we have employed different names, rectangular area and circle area, in their
respective class modules, but that is not necessary. The “use” statement allows one to rename the class
routines and/or to bring in only selected members of the functionality.
Example Date, Person, and Student Classes
Before moving to some mathematical examples we will introduce the concept of data hiding and combine
a series of classes to illustrate composition and inheritancey. First, consider a simple class to define dates
and to print them in a pretty fashion, as shown in Figs. 3.5 and 3.6. While other modules will have
access to the Date class they will not be given access to the number of components it contains (3),
nor their names (month, day, year), nor their types (integers) because they are declared “private” in the
defining module (lines 5 and 6). The compiler will not allow external access to data and/or routines
declared as private. The module, class Date, is presented as a source “include” file in Fig. 3.6, and
in the future will be reference by the file name class Date.f90. Since we have chosen to hide all
the user defined components we must decide what functionality we will provide to the users, who may
have only executable access. The supporting documentation would have to name the public routines and
describe their arguments and return results.
Object Oriented Numerical Calculations
OOP is often used for numerical computation, especially when the standard storage mode for arrays is
not practical or efficient. Often one will find specialized storage modes like linked lists, or tree structures
used for dynamic data structures. Here we should note that many matrix operators are intrinsic to F90,
so one is more likely to define a class sparse matrix than a class matrix. However, either
class would allow us to encapsulate several matrix functions and subroutines into a module that could be
reused easily in other software.