Object-Oriented and Event-Driven Programming: Prelude To Programming, 6Th Edition by Elizabeth Drake
Object-Oriented and Event-Driven Programming: Prelude To Programming, 6Th Edition by Elizabeth Drake
Object-Oriented and
Event-Driven Programming
Classes
The
alarm_clock
class
describes
what
an
alarm_clock
is
and
what
can
be
done
with
it.
An
alarm clock object
is
a
parIcular
example
of
an
alarm_clock.
Objects
Objects
are
made
up
of
two
components:
data
(aYributes)
operaIons
on
that
data
(methods)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Class Cube
Declare Side As Float //Side is an attribute
Declare Volume As Float //Volume is an attribute
Subprogram SetSide(NewSide) //SetSide() is a method
Set Side = NewSide
End Subprogram
Subprogram ComputeVolume() //ComputeVolume() is a method
Set Volume = Side^3
End Subprogram
Function GetVolume() As Float //GetVolume() is a method
Set GetVolume = Volume
End Function
Function GetSide() As Float //GetSide() is a method
Set GetSide = Side
End Function
End Class
Access Methods
In
the
example
on
the
previous
slide,
the
methods
SetSide()
(line
6),
GetVolume()
(line
10),
and
GetSide()
(line
13)
are
called
access
methods
They provide the rest of the program with access to the objects aYributes.
Data Hiding
Why
not
just
pass
the
values
of
the
variables
Side
and
Volume
back
and
forth
to
the
program
as
parameters?
In
OOP,
normally
we
want
to
keep
the
class
variables
completely
hidden
from
the
rest
of
the
program.
This
pracIce
is
called
data
hiding
and
has
a
two-fold
purpose:
1. It
enhances
the
security
of
the
objects
data.
The
data
cannot
be
altered
except
by
using
one
of
the
objects
methods.
v For
example,
if
you
were
wriIng
an
adventure
game
and
had
a
Monster
class
that
dened
objects
with
one
head
and
a
tail,
you
would
want
to
be
sure
that
any
Ime
you
created
a
new
Monster
object,
the
class
had
not
been
changed
to
a
two-headed
tailless
creature
because
someone
else
had
edited
a
Monster
object
elsewhere
in
the
program.
2. It
helps
to
shield
the
inner
workings
of
the
object
from
the
programmer.
In
OOP,
objects
work
like
black
boxes.
State
which
members
of
a
class
are
public
(available
to
code
outside
the
object)
and
which
are
private
(not
available
outside
the
class).
Most
programming
languages
use
the
keywords
Public and
Private
The
keyword,
placed
in
front
of
the
aYribute
or
method
name,
species
the
status
of
that
class
member
AYributes
are
normally
declared
to
be
Private
to
protect
their
integrity
Methods
are
declared
as
Public
if
they
are
part
of
the
interface
between
the
object
and
program
Methods
are
declared
as
Private
if
they
are
only
used
within
the
class
itself
AYributes
may
be
declared
Protected
if
they
are
meant
to
be
available
(Public)
to
derived
classes
but
hidden
(Private)
from
the
rest
of
the
program.
class.
Dot Nota?on
Once
created,
we
can
use
the
objects
Cube1 and
Cube2
in
a
program.
We
use
dot
nota8on
to
refer
to
the
object
and
method
or
aYribute
under
consideraIon.
Example:
to
assign
a
value
of
10
to
the
Side
aYribute
of
Cube1
use:
Call
Cube1.SetSide(10)
This
statement
calls
the
method
SetSide()and
assigns
10
to
its
argument,
NewSide.
To
ensure
that
this
method
is
se[ng
the
Side
of
the
object
Cube1
(not
that
of
Cube2),
place
Cube1
in
front
of
the
subprogram
name,
separated
by
a
dot
(period).
To
display
the
Volume
aYribute
of
Cube2,
use
the
following
statement:
Write Cube2.GetVolume()
In
general,
to
refer
to
a
public
member
of
an
object,
use:
ObjectName.MemberName
Main
Declare Cube1 As New Cube
Declare Side1 As Float
Write Enter the length of the side of a cube:
Input Side1
Call Cube1.SetSide(Side1)
Call Cube1.ComputeVolume()
Write Volume of a cube of side + Cube1.GetSide() +
is + Cube1.GetVolume()
End Program
The Constructor
If
the
main
program
calls
a
subprogram
before
its
aYributes
have
been
given
a
value,
an
error
may
occur.
To
prevent
this
we
use
constructors
to
ini8alize
an
objects
aYributes.
A
constructor
is
a
special
method
included
in
the
class
deniIon
that
automaIcally
performs
specied
setup
tasks
when
an
object
is
created.
The
constructor
will
iniIalize
the
objects
aYributes.
It
establishes
the
condiIons
that
dont
change.
A
constructor
is
a
special
method
that
can
be
used
to
create
objects
of
the
class.
Constructors
are
automaIcally
called
when
an
object
is
created.
They
are
normally
disInguished
by
having
the
same
name
as
the
class
of
the
object
they
are
associated
with.
The
constructor
is
created
when
the
class
is
created.
Crea?ng
a Constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Class Cube
Declare Private Side, Volume As Float
// The Cube constructor:
Public Cube()
Set Side = 1.0
Set Volume = 1.0
End Constructor
Public Subprogram SetSide(NewSide)
Set Side = NewSide
End Subprogram
Public Subprogram ComputeVolume()
Set Volume = Side^3
End Subprogram
Public Function GetVolume() As Float
Set GetVolume = Volume
End Function
Public Function GetSide() As Float
Set GetSide = Side
End Function
End Class
2.
The
graphical
user
interface
(GUI)
gradually
became
almost
universal.
A
GUI
comprises
objects
(windows,
boxes,
buYons,
and
so
forth),
so
OOP
became
the
natural
way
to
program
for
these
interfaces.
Inheritance
We
can
say
that
the
child
class
extends
the
parent
class.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Class Cube
Declare Protected Side, Volume As Float
Public Cube() //create constructor
Set Side = 1.0; Set Volume = 1.0
End Constructor
Public Subprogram SetSide(Float NewSide)
Set Side = NewSide
End Subprogram
Public Subprogram ComputeVolume()
Set Volume = Side^3
End Subprogram
Public Function GetVolume() As Float
Set GetVolume = Volume
End Function
Public Function GetSide() As Float
Set GetSide = Side
End Function
End Class
Example:
A Child Class
of the Cube Class
Polymorphism
In
a
hierarchy
of
classes,
oKen
some
methods
are
common
to
several
classes,
but
their
deniIons
may
dier
from
class
to
class.
A
module
might
contain
deniIons
of
many
three-
dimensional
objects,
like
cubes,
boxes,
spheres,
or
cylinders.
Each
of
these
needs
a
dierent
formula
to
compute
the
volume.
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Modeling Languages
Flowcharts,
hierarchy
charts,
and
IPO
(Input-Process-Output
charts)
are
used
to
help
design
programs.
As
programs
get
larger
the
models
also
get
larger
and
more
complicated.
SoKware
developers
use
modeling
languages
to
help
design
large
programs.
An
object
modeling
language
is
a
standardized
set
of
symbols
that
includes
ways
to
arrange
these
symbols
to
model
parts
of
an
object-
oriented
soKware
design
or
system
design.
The
term
Unied
Modeling
Language
resulted
from
the
combined
eorts
of
3
men:
Ivar
Jacobson,
James
Rumbaugh,
and
Grady
Booch,
nicknamed
the
Three
Amigos
emphasizes
the
staIc
structure
of
the
system
using
objects,
aYributes,
operaIons,
and
relaIonships
includes
diagrams
that
allow
the
designer
to
see
the
structure
of
a
program
by
showing
the
classes,
their
aYributes,
and
the
relaIonships
between
the
classes
also
includes
diagrams
that
show
the
internal
structure
of
a
class
and
the
relaIonships
that
this
structure
makes
possible
Window
Components
PRELUDE
TO
PROGRAMMING,
6TH
EDITION
BY
ELIZABETH
DRAKE
Se[ng
Proper?es
command button
Click():
executes
automaIcally
when
the
buYon
is
clicked
text box
Click():
executes
automaIcally
when
the
box
is
clicked
Change:
executes
automaIcally
when
the
text
within
the
box
is
changed
option button
Click():
executes
automaIcally
when
the
buYon
is
clicked
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
Window
name = MainWindow
title = Area Calculator
Upper Label
text = Side of square
Lower Label
text = Area of square
Upper Text Box
name = InputBox
text =
Subprogram InputBox.Click()
Set InputBox.text =
Set OutputBox.text =
Set CalculateButton.enabled = false
End Subprogram
Subprogram InputBox.Change()
Set CalculateButton.enabled = true
End Subprogram
Lower Text Box
name = OutputBox
text =
Left Command Button
name = CalculateButton
caption = Calculate
enabled = false
Subprogram CalculateButton.Click()
Set OutputBox.text = Val(InputBox.text)^2
End Subprogram
Right Command Button
name = DoneButton
caption = Done
Subprogram DoneButton.Click()
Call MainWindow.Close
End Program
End Subprogram
An Event-Driven
GUI Calculator
Flow Diagram
for a
GUI Program