Program Realisation 2 http://www.win.tue.nl/˜hemerik/2IP20/ Lecture 5 Kees Hemerik Tom Verhoeff Technische Universiteit Eindhoven Faculteit Wiskunde en Informatica Software Engineering & Technology Feedback to T.Verhoeff@TUE.NL
c 2007, T. Verhoeff @ TUE.NL 1 Program Realization 2: Lecture 5
Today’s Topics
- Inheritance revisited
- Dynamic variables : heap, pointer type ˆT, New, Dispose
Informatics Thriller: My Life among the Pointers
- Linear dynamic data structures with pointers:
Linked Lists
c 2007, T. Verhoeff @ TUE.NL 2 Program Realization 2: Lecture 5
Inheritance Revisited Inheritance is a mechanism with many faces. We use it only for:
- Multiple co-existing implementations of an ADT
Parent : no fields; methods are virtual, usually also abstract Child : same contract; private fields; override the methods
- Subtyping (specialization; substitutability principle; e.g. VCL)
Parent : usually has private fields and virtual methods Child : stronger contract: weaker pre, stronger post; may add fields, add/override methods
c 2007, T. Verhoeff @ TUE.NL 3 Program Realization 2: Lecture 5
Dynamic Method Binding: Advanced Example
1 type 2
TParent = class(TObject)
3
procedure M; virtual; { possibly abstract; }
4
procedure D; virtual;
5
end; { class TParent }
6 7
TChild = class(TParent)
8
procedure M; override;
9
end; { class TChild }
10 11 procedure TParent.M; begin Write(’TParent.M ’) end; { omit if abstract } 12 13 procedure TParent.D; begin M ; M end; 14 15 procedure TChild.M; begin Write(’TChild.M ’) end; 16 17 begin 18
with TChild.Create do D { outputs: ’TChild.M TChild.M ’ }
c 2007, T. Verhoeff @ TUE.NL 4 Program Realization 2: Lecture 5