Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: oops concept
Description: it is given oops concept in this pdf file.here you can learn and understand easily.
Description: it is given oops concept in this pdf file.here you can learn and understand easily.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
Chapter 3
Object Oriented Programming Concepts
3
...
Thus, it is useful to have an introductory understanding of OOP and some of the
programming features of OO languages
...
However, newer languages such as Ada, C++, and F90 have enhanced features that make
OOP much more natural, practical, and maintainable
...
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
...
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)
...
An object combines various classical data types
into a set that defines a new variable type, or structure
...
Every object created from a class, by providing the necessary data, is called an instance of the
class
...
An OO language
provides a way to couple or encapsulate the data and its functions into a unified entity
...
The encapsulation is
done with a “module” block in F90, and with a “class” block in C++
...
The
accessibility of the specifications and routines of a class is usually controlled by optional “public” and
“private” qualifiers
...
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
...
Class hierarchies can be visualized when we realize that we can employ one or more previously
defined classes (of data and functionality) to organize additional classes
...
This mechanism is called
inheritance
...
We would then only be required to add only
the totally new data and functions needed for a manager
...
By using the concept of a class
hierarchy, less programming effort is required to create the final enhanced program
...
Polymorphism allows different classes of objects that share some common functionality to be used in
code that requires only that common functionality
...
E
...
This is useful in class hierarchies where a small number of meaningful function names can be used to
manipulate different, but related object classes
...
In the later sections we will demonstrate by example additional F90 implementations
of these concepts
...
2 Encapsulation, Inheritance, and Polymorphism
We often need to use existing classes to define new classes
...
We will use both methods in a series of examples
...
3
...
2
...
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)
...
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
...
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)
...
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
...
In this example we have employed different names, rectangular area and circle area, in their
respective class modules, but that is not necessary
...
Circle Class
real
real
radius
pi
Circle
real
Circle
make_Circle
Circle_Area
Circle
Figure 3
...
An intrinsic
constructor is a system function that is automatically invoked when an object is declared with all of its
possible components in the defined order (see lines 37 and 43)
...
One is illustrated in the statement
four sides = Rectangle (2
...
3)
where previously we declared
type (Rectangle) :: four sides
which, in turn, was coupled to the class Rectangle which had two components, base and height,
defined in that order, respectively
...
E
...
2: Representation of a Rectangle Class
[ 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]
!
Areas of shapes of different classes, using different
!
function names in each class
module class Rectangle
! define the first object class
implicit none
type Rectangle
real :: base, height ; end type Rectangle
contains ! Computation of area for rectangles
...
1415926535897931d0 ! a circle constant
type Circle
real :: radius ; end type Circle
contains !
Computation of area for circles
...
:: four sides
:: two sides
! inside, outside
:: area = 0
...
four sides = Rectangle ( 2
...
3 )
! implicit constructor
! generic function
area = compute area ( four sides )
write ( 6,100 ) four sides, area
! implicit components list
100 format ("Area of ",f3
...
1," rectangle is ",f5
...
two sides = Circle ( 5
...
1," radius is ",f9
...
1 by 4
...
03
! Area of circle with 5
...
60885
Figure 3
...
1 and component height = 4
...
This intrinsic construction is possible because all the expected components of the type were supplied
...
E
...
,1
...
type ( Rectangle ) :: four sides, square, unit sq
!
Test manual constructors
four sides = make Rectangle (2
...
3) ! manual constructor, 1
area = compute area ( four sides)
! generic function
write ( 6,100 ) four sides, area
!
Make a square
square = make Rectangle (2
...
! Running gives:
! Area of 2
...
3 rectangle is 9
...
1 by 2
...
41
! Area of 1
...
0 rectangle is 1
...
4: A Manual Constructor for Rectangles
class is expanded by the programmer to accept a different number of arguments
...
That is, we would use height = base in that case
...
Such a manual
constructor, named make Rectangle, is illustrated in Fig
...
4 (see lines 5, 6)
...
Note that the last two arguments were declared to have the additional type
attributes of “optional” (line 3), and that an associated logical function “present” is utilized (lines 6 and 8)
to determine if the calling program supplied the argument in question
...
In the next section we will illustrate the concept of data hiding by using the private attribute
...
In that case a manual constructor must be provided to deal with any hidden components
...
3
...
1 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 inheritanceÝ
...
3
...
6
...
The compiler will not allow external access to data and/or routines
declared as private
...
3
...
f90
...
The supporting documentation would have to name the public routines and
describe their arguments and return results
...
”
Ý These examples mimic those given in Chapter 11 and 8 of the J
...
Hubbard book “Programming with C++,” McGraw-Hill,
1994, and usually use the same data for verification
...
E
...
5: Graphical Representation of a Date Class
The intrinsic constructor, Date (lines 14 and 34), requires all the components be supplied, but it does
no error or consistency checks
...
Its sole purpose is to
do data checking and invoke the intrinsic constructor, Date
...
” In this example we have provided another
manual constructor to set a date, set Date (line 31), with a variable number of optional arguments
...
A sample main program that employs this class is given in Fig
...
7, which contains sample outputs
as comments
...
Note that the definition of the class was copied in via an “include” (line 1) statement and
activated with the “use” statement (line 4)
...
As shown in Fig
...
8, we have made all the type components “private,” but make all the
supporting functionality public, as represented graphically in Fig
...
8
...
The source code for the new Person class is given in Fig
...
9
...
The Date public function from the class Date is “inherited” to initialize the DOB and DOD (lines 18, 57, and 62)
...
Of course, the include could have
been omitted if the compile statement included the path name to that source
...
3
...
It utilizes the
constructors Date (line 7), Person (line10), and make Person (line 24)
...
The student person will have additional “private” components for an identification number, the expected date of matriculation (DOM), the total course credit hours
earned (credits), and the overall grade point average (GPA), as represented in Fig
...
11
...
3
...
There the constructors
are make Student (line 19) and Student (line 47)
...
3
...
Since there are various ways to utilize the various constructors three alternate
methods have been included as comments to indicate some of the programmers options
...
c
2001 J
...
Akin
37
[ 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]
module class Date
! filename: class Date
...
or
...
or
...
or
...
6: Defining a Date Class
[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]
[11]
[12]
[13]
[14]
[15]
[16]
[17]
[18]
[19]
[20]
include ’class Date
...
7: Testing a Date Class
3
...
Often one will find specialized storage modes like linked lists, or tree structures
used for dynamic data structures
...
However, either
class would allow us to encapsulate several matrix functions and subroutines into a module that could be
reused easily in other software
...
E
...
8: Graphical Representation of a Person Class
the important topic of operator overloading
...
3
...
1 A Rational Number Class and Operator Overloading
To illustrate an OOP approach to simple numerical operations we will introduce a fairly complete rational
number class, called class Rational which is represented graphically in Fig
...
14
...
3
...
The type components have been made private (line 5), but not the type
itself, so we can illustrate the intrinsic constructor (lines 38 and 102), but extra functionality has been
provided to allow users to get either of the two components (lines 52 and 57)
...
Note that we would form a new rational number, Þ , as the product of two other rational numbers, Ü
and Ý , by invoking the mult Rational function (line 90),
z = mult Rational (x, y)
which returns Þ as its result
...
This is known as overloading an intrinsic operator
...
Thus, from the “interface operator (*)” statement block (line 14)
the system now knows that the left and right operands of the “*” symbol correspond to the first and
second arguments in the function mult Rational
...
However, to convert
Ü £ Ý
...
E
...
f90
use class Date
implicit none
public :: Person
type Person
private
character (len=20) :: name
character (len=20) :: nationality
integer
:: sex
type (Date)
:: dob, dod
! birth, death
end type Person
contains
function make Person (nam, nation, s, b, d) result (who)
!
Optional Constructor for a Person type
character (len=*), optional, intent(in) :: nam, nation
integer,
optional, intent(in) :: s
! sex
type (Date),
optional, intent(in) :: b, d ! birth, death
type (Person)
:: who
who = Person (" ","USA",1,Date (1,1,0),Date (1,1,0)) ! defaults
if ( present(nam)
) who % name
= nam
if ( present(nation) ) who % nationality = nation
if ( present(s)
) who % sex
= s
if ( present(b)
) who % dob
= b
if ( present(d)
) who % dod
= d ; end function
function Person
(nam, nation, s, b, d) result (who)
!
Public Constructor for a Person type
character (len=*), intent(in) :: nam, nation
integer,
intent(in) :: s
! sex
type (Date),
intent(in) :: b, d ! birth, death
type (Person)
:: who
who = Person (nam, nation, s, b, d) ; end function Person
subroutine print DOB (who)
type (Person), intent(in) :: who
call print Date (who % dob) ; end subroutine
subroutine print DOD (who)
type (Person), intent(in) :: who
call print Date (who % dod) ; end subroutine
print DOB
print DOD
subroutine print Name (who)
type (Person), intent(in) :: who
print *, who % name ; end subroutine print Name
subroutine print Nationality (who)
type (Person), intent(in) :: who
print *, who % nationality ; end subroutine
print Nationality
subroutine print Sex (who)
type (Person), intent(in) :: who
if ( who % sex == 1 ) then ; print *, "male"
else ; print *, "female" ; end if ; end subroutine print Sex
subroutine set DOB (who, m, d, y)
type (Person), intent(inout) :: who
integer, intent(in)
:: m, d, y ! month, day, year
who % dob = Date
(m, d, y) ; end subroutine set DOB
subroutine set DOD(who, m, d, y)
type (Person), intent(inout) :: who
integer, intent(in)
:: m, d, y ! month, day, year
who % dod = Date
(m, d, y) ; end subroutine set DOD
end module class Person
Figure 3
...
Here we have provided the procedure, equal Integer, which is automatically invoked when
we write : type(Rational)y; y = 4
...
Before moving on note that the system does not yet know how to multiply an integer
times a rational number, or visa versa
...
A typical main program which exercises most of the rational number functionality is given in Fig
...
16,
along with typical numerical output
...
E
...
f90’
! see previous figure
include ’class Person
...
He was born on "; call print DOB (author);
print *, " and died on ";
call print DOD (author); print *, "
...
He was born on "; call print DOB (author);
print *, " and died on ";
call print DOD (author); print *, "
...
";
end program main
! Running gives:
! The author of the Declaration of Independence was Thomas Jefferson
...
! The author of the Declaration of Independence was Thomas Jefferson
...
! The creator of Fortran was John Backus who was born in the USA
...
10: Testing the Date and Person Classes
Student Class
Person
who
character
Date
integer
id [SSN]
matriculation
credits
real
gpa
Student
Student_
Student
make_Student
Student
get_Person
Student
Student
print_DOM
print_GPA
Student
set_DOM
Student
Student
Figure 3
...
The intrinsic constructor (line
6) could have been used only if all the attributes were public, and that is considered an undesirable
practice in OOP
...
Later we will see that constructors and destructors often must dynamically allocate and
deallocate, respectively, memory associated with a specific instance of some object
...
E
...
f90
! inherits class Date
use class Person
implicit none
public :: Student, set DOM, print DOM
type Student
private
type (Person)
:: who
! name and sex
character (len=9) :: id
! ssn digits
type (Date)
:: dom
! matriculation
integer
:: credits
real
:: gpa
! grade point average
end type Student
contains ! coupled functionality
function get person (s) result (p)
type (Student), intent(in) :: s
type (Person)
:: p
! name and sex
p = s % who ; end function get person
function make Student (w, n, d, c, g) result (x) ! constructor
!
Optional Constructor for a Student type
type (Person),
intent(in) :: w ! who
character (len=*), optional, intent(in) :: n ! ssn
type (Date),
optional, intent(in) :: d ! matriculation
integer,
optional, intent(in) :: c ! credits
real,
optional, intent(in) :: g ! grade point ave
type (Student)
:: x ! new student
x = Student (w, " ", Date (1,1,1), 0, 0
...
P
...
is ", x % gpa, "
...
12: Defining a Typical Student Class
When considering which operators to overload for a newly defined object one should consider those
that are used in sorting operations, such as the greater-than, , and less-than, , operators
...
If those symbols have been correctly
overloaded then a generic object sorting routine might be used, or require few changes
...
4 Discussion
The previous sections have only briefly touched on some important OOP concepts
...
There are more than one
hundred OOP languages
...
At the same time it includes the F77 standard as a subset
and thus allows efficient use of the many millions of Fortran functions and subroutines developed in the
past
...
E
...
f90’
include ’class Person
...
f90’ ! see previous figure
program main
! create or correct a student
use class Student
! inherits class Person, class Date also
implicit none
type (Person) :: p ; type (Student) :: x
!
Method 1
p = make Person ("Ann Jones","",0) ! optional person constructor
! add birth to person data
call set DOB (p, 5, 13, 1977)
x = Student (p, "219360061", Date (8,29,1955), 9, 3
...
P
...
is 3
...
! Ann Jones was born on: May 13, 1977 , Matriculated: August 29, 1995
! Ann Jones Matriculated: August 29, 1995 , was born on: May 13, 1977
Figure 3
...
It includes most of the High Performance Fortran features that are in wide use
...
None of the OOP languages have all the features one might desire
...
Yet the author has found that a
few dozen lines of F90 code will define a preprocessor that allows templates to be defined in F90 and
expanded in line at compile time
...
For example, several
authors have described widely different approaches for defining classes to be used in constructing OO
finite element systems
...
c
2001 J
...
Akin
43
Rational Class
integer
numerator
integer
denominator
Rational
Rational_
Rational
Rational
Rational
Rational
make_Rational
add_Rational
convert
copy_Rational
Rational
Rational
delete_Rational
equal_Rational
Rational
Rational
get_Denominator
get_Numerator
Rational
Rational
invert
is_equal_to
Rational
Rational
list
mult_Rational
Rational
integer
Rational
Rational
gcd
reduce
Figure 3
...
E
...
f90
2] implicit none
3] ! public, everything but following private routines
4] private :: gcd, reduce
5]
type Rational
6]
private ! numerator and denominator
7]
integer :: num, den ; end type Rational
8]
9]
! overloaded operators interfaces
10]
interface assignment (=)
11]
module procedure equal Integer ; end interface
12]
interface operator (+)
! add unary versions & (-) later
13]
module procedure add Rational ; end interface
14]
interface operator (*)
! add integer mult Rational, etc
15]
module procedure mult Rational ; end interface
16]
interface operator (==)
17]
module procedure is equal to ; end interface
18] contains
! inherited operational functionality
19] function add Rational (a, b) result (c)
! to overload +
20]
type (Rational), intent(in) :: a, b
! left + right
21]
type (Rational)
:: c
22]
c % num = a % num*b % den + a % den*b % num
23]
c % den = a % den*b % den
24]
call reduce (c) ; end function add Rational
25]
26] function convert (name) result (value) ! rational to real
27]
type (Rational), intent(in) :: name
28]
real
:: value ! decimal form
29]
value = float(name % num)/name % den ; end function convert
30]
31] function copy Rational (name) result (new)
32]
type (Rational), intent(in) :: name
33]
type (Rational)
:: new
34]
new % num = name % num
35]
new % den = name % den ; end function copy Rational
36]
37] subroutine delete Rational (name)
! deallocate allocated items
38]
type (Rational), intent(inout) :: name
! simply zero it here
39]
name = Rational (0, 1) ; end subroutine delete Rational
40]
41] subroutine equal Integer (new, I) ! overload =, with integer
42]
type (Rational), intent(out) :: new ! left side of operator
43]
integer,
intent(in) :: I
! right side of operator
44]
new % num = I ; new % den = 1 ; end subroutine equal Integer
45]
46] recursive function gcd (j, k) result (g) ! Greatest Common Divisor
47]
integer, intent(in) :: j, k ! numerator, denominator
48]
integer
:: g
49]
if ( k == 0 ) then ; g = j
50]
else ; g = gcd ( k, modulo(j,k) )
! recursive call
51]
end if ; end function gcd
52]
53] function get Denominator (name) result (n)
! an access function
54]
type (Rational), intent(in) :: name
55]
integer
:: n
! denominator
56]
n = name % den ; end function get Denominator
(Fig
...
15, A Fairly Complete Rational Number Class (continued))
c
2001 J
...
Akin
45
[ 57]
[ 58]
[ 59]
[ 60]
[ 61]
[ 62]
[ 63]
[ 64]
[ 65]
[ 66]
[ 67]
[ 68]
[ 69]
[ 70]
[ 71]
[ 72]
[ 73]
[ 74]
[ 75]
[ 76]
[ 77]
[ 78]
[ 79]
[ 80]
[ 81]
[ 82]
[ 83]
[ 84]
[ 85]
[ 86]
[ 87]
[ 88]
[ 89]
[ 90]
[ 91]
[ 92]
[ 93]
[ 94]
[ 95]
[ 96]
[ 97]
[ 98]
[ 99]
[100]
[101]
[102]
[103]
[104]
[105]
[106]
[107]
[108]
[109]
[110]
[111]
[112]
function get Numerator (name) result (n)
! an access function
type (Rational), intent(in) :: name
integer
:: n
! numerator
n = name % num ; end function get Numerator
subroutine invert (name)
! rational to rational inversion
type (Rational), intent(inout) :: name
integer
:: temp
temp
= name % num
name % num = name % den
name % den = temp ; end subroutine invert
function is equal to (a given, b given) result (t f)
type (Rational), intent(in) :: a given, b given ! left == right
type (Rational)
:: a, b
! reduced copies
logical
:: t f
a = copy Rational (a given) ; b = copy Rational (b given)
call reduce(a) ; call reduce(b)
! reduced to lowest terms
t f = (a%num == b%num)
...
(a%den == b%den) ; end function
subroutine list(name)
! as a pretty print fraction
type (Rational), intent(in) :: name
print *, name % num, "/", name % den ; end subroutine list
function make Rational (numerator, denominator) result (name)
!
Optional Constructor for a rational type
integer, optional, intent(in) :: numerator, denominator
type (Rational)
:: name
name = Rational(0, 1)
! set defaults
if ( present(numerator) ) name % num = numerator
if ( present(denominator)) name % den = denominator
if ( name % den == 0
) name % den = 1
! now simplify
call reduce (name) ; end function make Rational
function mult Rational (a, b) result (c)
type (Rational), intent(in) :: a, b
type (Rational)
:: c
c % num = a % num * b % num
c % den = a % den * b % den
call reduce (c) ; end function mult Rational
! to overload *
function Rational
(numerator, denominator) result (name)
!
Public Constructor for a rational type
integer, optional, intent(in) :: numerator, denominator
type (Rational)
:: name
if ( denominator == 0 ) then ; name = Rational (numerator, 1)
else ; name = Rational (numerator, denominator) ; end if
end function Rational
subroutine reduce (name)
! to simplest rational form
type (Rational), intent(inout) :: name
integer
:: g
! greatest common divisor
g
= gcd (name % num, name % den)
name % num = name % num/g
name % den = name % den/g ; end subroutine reduce
end module class Rational
Figure 3
...
E
...
f90’
program main
use class Rational
implicit none
type (Rational) :: x, y, z
! ------- only if Rational is NOT private ---------! x = Rational(22,7)
! intrinsic constructor if public components
x = Rational (22,7)
!
write (*,’("public
x
write (*,’("converted x
call invert(x)
write (*,’("inverted 1/x
public constructor if private components
= ")’,advance=’no’); call list(x)
= ", g9
...
0)’) get numerator(z)
write (*,’("bottom of z = ", g4
...
Function Tests
write (*,’("making x = 100/360, ")’,advance=’no’)
x = make Rational (100,360)
write (*,’("reduced x = ")’,advance=’no’); call list(x)
write (*,’("copying x to y gives ")’,advance=’no’)
y = copy Rational (x)
write (*,’("a new y = ")’,advance=’no’); call list(y)
!
Test Overloaded Operators
write (*,’("z * x gives ")’,advance=’no’); call list(z*x) ! times
write (*,’("z + x gives ")’,advance=’no’); call list(z+x) ! add
y = z
! overloaded assignment
write (*,’("y = z gives y as ")’,advance=’no’); call list(y)
write (*,’("logic y == x gives ")’,advance=’no’); print *, y==x
write (*,’("logic y == z gives ")’,advance=’no’); print *, y==z
!
Destruct
call delete Rational (y)
! actually only null it here
write (*,’("deleting y gives y = ")’,advance=’no’); call list(y)
end program main
! Running gives:
! public
x = 22 / 7
! converted x = 3
...
16: Testing the Rational Number Class
c
2001 J
...
Akin
47
3
...
Use the class Circle to create a class Sphere that computes the volume of a sphere
...
Use the radius of the Circle via a new member
get Circle radius to be added to the class Circle
...
Use the class Circle and class Rectangle to create a class Cylinder that computes
the volume of a right circular cylinder
...
In the latter member
use the height of the Rectangle via a new member get Rectangle height to be added to the
class Rectangle
...
Create a vector class to treat vectors with an arbitrary number of real coefficients
...
Include two constructors assign and make Vector
...
Provide a destructor, means to read and write a Vector, normalize a Vector, and determine its
extreme values
...
E
...
Modify the above Vector class to extend it to a Sparse Vector Class where the vast majority
of the coefficients are zero
...
Sparse_Vector Class
integer
integer, pointer
non_zeros
rows (:)
real, pointer
values (:)
Sparse_Vector
make_Sparse_Vector
Sparse_Vector add_Real_to_Sparse_Vector
Sparse_Vector
Sparse_Vector
add_Sparse_Vector
delete_Sparse_Vector
real
Sparse_Vector
Sparse_Vector
dot_Vector
el_by_el_Mult
equal_Vector
real
logical
get_element
is_equal_to
integer
real
largest_index
normalize_Vector
length
real
Sparse_Vector
norm
normalize_Vector
Sparse_Vector
Sparse_Vector
pretty
read_Vector
Sparse_Vector
integer
real_mult_Sparse
rows_of
Sparse_Vector
Sparse_Vector
Sparse_Vector
set_element
show
show_r_v
integer
Sparse_Vector
size_of
Sparse_mult_real
Sparse_vector
Sparse_Vector
sub_Sparse_Vector
sum_Sparse_Vector
real
real
Vector_max_value
Vector_min_value
Sparse_Vector
Sparse_Vector
Vector_to_Sparse
zero_Sparse
Sparse_Vector
c
2001 J
...
Akin
Sparse_Vector
49
Title: oops concept
Description: it is given oops concept in this pdf file.here you can learn and understand easily.
Description: it is given oops concept in this pdf file.here you can learn and understand easily.