Theory Notes/💻 Topic 1: Solution Development/12.1.3
12.1.3Grade 12

Object-Oriented Programming in Delphi

Classes, objects, encapsulation, inheritance and polymorphism — the heart of the Grade 12 practical paper and almost always the biggest single question.

A class bundles data (fields) with the code that operates on it (methods). Objects are references: you must Create them, and — unlike Java — you must Free them, because Pascal has no garbage collector.

Example

type TLearner = class private FName: String; FMark: Integer; procedure SetMark(value: Integer); public constructor Create(AName: String); function ToStringValue: String; property Name: String read FName; property Mark: Integer read FMark write SetMark; end; constructor TLearner.Create(AName: String); begin FName := AName; FMark := 0; end; procedure TLearner.SetMark(value: Integer); begin if (value >= 0) and (value <= 100) then FMark := value; // reject impossible marks end; var l: TLearner; begin l := TLearner.Create('Kabelo'); try l.Mark := 87; WriteLn(l.ToStringValue); finally l.Free; end; end.

ConceptIn Delphi
Encapsulationprivate fields + public properties; validate on write in the setter
Constructorconstructor Create(...) — called as TClass.Create
InheritanceTChild = class(TParent); call the parent with `inherited`
Polymorphismvirtual in the parent, override in the child
Abstractvirtual; abstract; — no body, every child must supply one
Property
Looks like a field to the caller but routes through methods. `read FMark write SetMark` means reads are direct, writes are validated.
F prefix
Convention for private fields (FName, FMark) so the field and its property can share a name.
try…finally Free
Guarantees the object is released even if an exception is raised — the standard Delphi idiom.

💡 Exam Tip

Papers frequently give you a class with the methods stubbed out and ask you to complete them. Read the class declaration first: it tells you exactly what each method must return.