Lecture Notes for Advanced Programming II
13 February 2001 - Classes and Abstract Data Types
- an abstract data type
- a class without inheritance
- what's left - enforced information hiding; enforced interface
definitions; type definitions; first-class citizenship
- but isn't inheritance important - well, no; understanding inheritance
is hard; understanding classes using inheritance is hard; there's
always the contains-a relation
- interface and implementation
- interface - the functions (and sometimes data) made available by the
adt
- implementation - the function code, types, and data used in the adt
- this is just our library discussion continued - a class is a library
- adt state
- state is what distinguishes two instances of an adt
-
stack<int> is1, is2;
is1.push(0);
- is
is1
different from is2
- how
- logical state - what the adt user thinks about; stack of integers
- physical state - what the adt implementer thinks about; array of
integers, an index value
- adt correctness involves state - logical state, physical state and
the relation between logical and physical state
- state is consistent if all values hang together - logical, physical,
and their relation
- class invariants - the correct relations among the values in
physical state; important for understanding classes
- adt consistency also involves state
- similar state behaves the same - consistency
- no surprises
- class (library, adt) rules
- redundancy - can be good (reliability) or bad (repeated computation);
unused redundancy is bad
- don't copy code; turn it into a procedure - constructors
- dynamic storage - always a problem; the assignment operator
This page last modified on 13 February 2001.