10. OOP
Docs / Pseudocode / OOP
A Level OOP

10. Object-Oriented Programming

Cambridge A Level (9618) OOP: CLASS declarations, constructor NEW, PUBLIC/PRIVATE encapsulation, inheritance with INHERITS, and SUPER dispatch.

Classes are defined with CLASS..ENDCLASS. Properties and methods default to PUBLIC unless marked PRIVATE. Constructors use NEW. Inheritance uses INHERITS and parent methods are chained with SUPER.

Class Definition & Constructor

PSEUDOCODE Run in Sandbox
// runnable
// Pet class demo
CLASS Pet
   PRIVATE Name : STRING

   PUBLIC PROCEDURE NEW(GivenName : STRING)
      Name <- GivenName
   ENDPROCEDURE
ENDCLASS

// Creating an object
DECLARE MyPet : Pet
MyPet <- NEW Pet("Buddy")

Inheritance & SUPER Dispatch

PSEUDOCODE
CLASS Cat INHERITS Pet
   PRIVATE Breed : STRING

   PUBLIC PROCEDURE NEW(GivenName : STRING,
                        GivenBreed : STRING)
      SUPER.NEW(GivenName)
      Breed <- GivenBreed
   ENDPROCEDURE
ENDCLASS

MyCat <- NEW Cat("Kitty", "Shorthaired")

Encapsulation, Methods & Property Access

PSEUDOCODE
// PUBLIC and PRIVATE visibility
PRIVATE Attempts : INTEGER
Attempts <- 3

PUBLIC PROCEDURE SetAttempts(Number : INTEGER)
   Attempts <- Number
ENDPROCEDURE

PRIVATE FUNCTION GetAttempts() RETURNS INTEGER
   RETURN Attempts
ENDFUNCTION

// Calling methods via dot notation
Player.SetAttempts(5)
OUTPUT Player.GetAttempts()