The Art of Programming
The Art of Programming
The Art of Programming
input
output
Structured programming
Strategy
1. Write pseudo-code if desired to define the
problem.
2. Decide on the program blocks, specifying
the inputs and outputs for each blocks.
3. Refine and see if the larger blocks can be
in turn sub-divided into smaller blocks.
4. Now you can start programming..just fill in
the blocks with computer code !
complex block diagrams
START
EXIT
utility routine
EXIT
Structured programming - summary
•• Structured
Structured programs
programs divide the
the problem into smaller sub-units or
blocks, then divided
divided into smaller blocks.. eventually reaching the
level of program code.
•• The blocks should ideally be self-contained – interactions with
with other
blocks should be
be explicit
•• ALL programming
programming languages
languages support Structured
Structured Programming
Programming to
some extent
•• Useful model
model for small-medium sized
sized projects.
•• Becomes
Becomes unmanageable for larger, more complex projects,
projects, with
many programmers involved → Object
Object Oriented
Oriented Programming
Programming
(OOP).
The object-oriented approach
Evolution of Program Design
Examples
1950s-
1960s “simple” program models COBOL, Algol,
BASIC
Q: Why objects?
A: Because the world is made from distinct objects,
which consist of other objects, etc., and this is often
a natural and powerful way of representing a
situation or problem.
Example objects - chemistry
e le c tr o n
n e u tr o n
p r o to n
atoms
object hierarchy
nuclei electrons
protons neutrons
OOP and structured programming
In structured programs the blocks are pieces of code which
are executed as the program is run
BEHAVIOUR
BEHAVIOUR
•• Sleeps
Sleepsaalot
lot
•• Scratches
Scratchesfurniture
furniture
•• Catches
Catchesmice
mice
•• Fights
Fightsother
othercats
cats
Implementation of objects
State is usually held as local
variables (also called properties),
STATE quantities not visible outside the
object (data hiding)
Behaviour controlled by
method functions or
BEHAVIOUR subroutines which act on
the local variables and
interface with the outside.
Key feature of OOP - Inheritance
Important ability of any OOP is the ability to derive one
object from a more general class of related objects: this is
called inheritance.
white blood
cell
nerve cell skin cell
Examples of OO languages
• C++
– Classic example, uses C syntax
• Java
– Based on C++, often used for Web and graphics
• Visual Basic, Visual C++
– Windows programming
• Perl ?
– Possible, e.g. BioPerl, but not originally designed for
objects. Implementation is a bit ad-hoc.
Wanna know more ? For experts C++, for semi-experts Java and
Visual Basic for beginners.
OOP - Summary
Objects provide a powerful and natural approach to
representing many problems
Features such as inheritance allow already written
objects to be re-used – program modification easier.
Certainly more difficult than conventional programming
Some concepts hard, even for
for experienced
experienced programmers
programmers
Implementation of objects often use complicated
syntax/semantics
syntax/semantics
OOP not famous for efficiency (memory or execution
time)
C++ once famous for being slow, now
now much better
Java still
still famous for
for being slow
A family tree of languages
Fortran Cobol
LISP
BASIC Algol 60
PL/1
Simula ML
Algol 68
C Pascal
Scheme
Smalltalk
Dylan
Ada
C++ Perl
Modula 3 Prolog
Java
C# Python Ruby