Grade 12 · Object-oriented programming · Lesson and quiz · about 10 minutes
Until now your data (variables) and your actions (procedures) lived apart. Object-oriented programming (OOP) packs them together: a class is the blueprint, an object is one real thing built from it.
type
TLearner = class
private
fName : String; // attributes (fields) - hidden
fMark : Integer;
public
constructor Create(sName : String; iMark : Integer);
function GetName : String; // accessor ("getter")
procedure SetName(sName : String); // mutator ("setter")
function ToString : String;
end;
The vocabulary you must own for Paper 1 and Paper 2 theory:
- Class — the blueprint (TLearner). By convention class names start with T.
- Object — one instance built from the blueprint:
objL := TLearner.Create('Anele', 90); - Attributes / fields — the object's own variables, prefixed
f, declaredprivate. - Encapsulation — fields are private; the outside world must use public methods. The class guards its own data.
- Constructor — the special method (Create) that builds and initialises a new object.
- Accessor / mutator — GetX returns a field's value; SetX changes it (often with validation).
- toString — returns a neat String describing the object, ready for a memo or message.
In the exam the class usually lives in its own unit; the form unit creates and uses the objects. Here we keep them in one editor so you can see everything at once.
Quick check
Question 1
TLearner is to objThabo as…
Answer
a blueprint is to a house built from it — The class (TLearner) is the design; the object (objThabo) is one actual instance built from that design with Create.
Question 2
Why are the fields (fName, fMark) declared PRIVATE?
Answer
Encapsulation — outside code must go through the public methods, so the class controls its own data — Encapsulation protects the data. If SetMark validates 0–100, no outside code can sneak in a mark of 250 by writing to the field directly.
Question 3
Which line correctly creates the object?
Answer
objL := TLearner.Create('Anele', 90); — In Delphi you call the constructor on the CLASS name: variable := TClassName.Create(arguments);
Question 4
A method that RETURNS the value of fMark without changing anything is called…
Answer
an accessor (getter) — Accessors (GetMark) read; mutators (SetMark) write. This pair of terms is asked in theory papers nearly every year.