UNIT U2ClasExample; {Declares classes TOval (subclass of TShape), TMyControl (subclass of TControl)} INTERFACE USES {$U UObject} UObject, {Needed to compile the U1ClasExample INTERFACE} {$U QuickDraw} QuickDraw, {Declares TYPE Pattern and procedures such as FillArc} {$U U1ClasExample} U1ClasExample; {Declares classes TShape, TArc, TRoundRect, TControl} TYPE TOval = SUBCLASS OF TShape FUNCTION TOval.CREATE(object: TObject; itsHeap: THeap): TOval; PROCEDURE TOval.Draw(pat: pattern); OVERRIDE; PROCEDURE TOval.Erase; OVERRIDE; END; TMyControl = SUBCLASS OF TControl FUNCTION TMyControl.CREATE(object: TObject; itsHeap: THeap; itsLine: STR255; itsBox: Rect): TMyControl; FUNCTION TMyControl.NewShape(ch: CHAR): TShape; OVERRIDE; {TControl.NewShape needn't be DEFAULT} END; IMPLEMENTATION METHODS OF TOval; FUNCTION TOval.CREATE(object: TObject; itsHeap: THeap): TOval; BEGIN IF object = NIL THEN {If space is not already allocated} object := NewObject(itsHeap, THISCLASS); { then allocate it} SELF := TOval(TShape.CREATE(object, itsHeap)); {Initialize inherited fields} END; PROCEDURE TOval.Draw(pat: pattern); BEGIN FillOval(SELF.boundRect, pat); {A QuickDraw call} END; PROCEDURE TOval.Erase; BEGIN EraseOval(SELF.boundRect); {A QuickDraw call} END; END; METHODS OF TMyControl; FUNCTION TMyControl.CREATE(object: TObject; itsHeap: THeap; itsLine: STR255; itsBox: Rect): TMyControl; BEGIN IF object = NIL THEN {If space is not already allocated} object := NewObject(itsHeap, THISCLASS); { then allocate it} SELF := TMyControl(TControl.CREATE(object, itsHeap, itsLine, itsBox)); {Init. inherited fields} END; FUNCTION TMyControl.NewShape(ch: CHAR): TShape; BEGIN IF (ch = 'o') OR (ch = 'O') THEN NewShape := TOval.CREATE(NIL, SELF.heap) ELSE NewShape := SUPERSELF.NewShape(ch); {Calls TControl.NewShape regardless of SELF's class} END; END; END.