A Quick Look at Object-Oriented Programming
Next: Design of the
Up: The CWP Object-Oriented Optimization
Previous: Optimizing With COOOL
Object-Oriented Programming is a modern approach to
the old ideas of code modularity and reusabilty.
Many programming languages provide support for OOP features,
including C++, Smalltalk, LISP
and Pascal.
C++, due to the popularity of C, is the
most widely used OOP environment so we will focus our
short tour on it.
COOOLhas been developed using the Free Software
Foundation's C/C++ compiler gcc, which is freely available
via ftp from prep.ai.mit.edu.
Anything other than a superficial account of OOP is
beyond the scope of this paper. For more details see
the book by Stanley Lippman cited at the end.
Here we content ourselves to mention
the basic ideas without really explaining them.
The objects in OOP, called classes
in C++, can be abstractions of either the primary problem
or its supporting implementation. The principal tasks
are to define the classes and their inter-relationship
among one another. These relationships can be of two
types. One class can be an example of another-a house
is a kind of building-or one class can be a member
of another-a house has windows. We also must define
the set of operations provided by each class; this is
the class hierarchy.
All OO languages provide some support
for
encapsulation, sharing, and inheritance
of code.
We briefly explain these feature in terms of C++.
-
Encapsulation
is a mechanism for achieving code isolation:
separating
the external aspects of an object, which are accessible to other objects
and users,
from its internal implementation details. This isolation is
highly desirable for large software projects since you don't
want fiddling with the internal algorithm of some object
to cause other code using this object to break.
-
Sharing is accomplished in C++ via
- Overloading:
We could, for example, define a new object called an array, and
enrich the meaning of the binary operator +
so that adding two arrays element by element is as simple as
specifying
.
- Templates:
Templates allow one to define a family of similar
objects that share most
data
attributes as well as methods.
For example, we could
define a sparse-matrix template which could represent either double,
float, or integer sparse matrices.
-
Inheritance is the way we share similarities among different
classes while preserving their differences.
In C++, a class can be derived from other classes
which have similar, but more general attributes and methods.
We could define a class representing, say, our abstract notion
of a building. Then, later on, if we needed the more specific
concept of a house, it could automatically inherit its
building-ness from the base class building; adding
on only what was spcific to the house type of building.
The ability to derive one class from another
enables one to define abstract classes
which contain the common features of all their descendents.
Next: Design of the
Up: The CWP Object-Oriented Optimization
Previous: Optimizing With COOOL
Sun Feb 25 12:08:00 MST 1996