LearnStanmorephysics

Grade 12 · Matric Mastery

Objects & classes — the big idea

Grade 12 · Object-oriented programming · Lesson and quiz · about 10 minutes

Take this lesson in the app →

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, declared private.
  • 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…

  1. a variable is to a constant
  2. a blueprint is to a house built from it
  3. a loop is to an array
  4. a form is to a button
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?

  1. To save memory
  2. Because Delphi does not allow public variables
  3. Encapsulation — outside code must go through the public methods, so the class controls its own data
  4. To make the program run faster
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?

  1. objL := new TLearner('Anele', 90);
  2. TLearner := objL.Create('Anele', 90);
  3. objL := TLearner.Create('Anele', 90);
  4. objL.TLearner('Anele', 90);
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…

  1. a mutator (setter)
  2. an accessor (getter)
  3. a constructor
  4. an event handler
Answer

an accessor (getter) — Accessors (GetMark) read; mutators (SetMark) write. This pair of terms is asked in theory papers nearly every year.

About this material

This platform provides original CAPS-aligned practice material and study tools. Content is machine-verified and has not been reviewed by subject specialists. It is not affiliated with or endorsed by the Department of Basic Education. Learners should also use official past papers and consult their teachers where uncertain.