46  A recap of object oriented programming

Let’s say we want to write code that defines how an ambulance works.

There will be properties the ambulance has. Things like :

There will also be things the ambulance does :

In Object Oriented Programming,

46.1 An example

The pseudocode (i.e. this won’t run in python - but gives you an idea of the structure) shows how we would set up a class and what instances of that class might look like.

46.1.1 Class

CLASS : Ambulance

**Attributes**
name_of_trust : string
reg_number : string
patient_on_board : boolean
siren_on : boolean

**Methods**
drive (speed : float)
park (location : string)
load_patient (patient_name : string)
unload_patient (patient_name : string)
turn_on_siren ()
turn_off_siren ()

46.1.2 Instances

dans_ambulance

name_of_trust = “Chalk NHS Trust”
reg_number = CH41 LKS
patient_on_board = False
siren_on = True

sammis_ambulance

name_of_trust = “Rosser Healthcare”
reg_number = HS44 MAS
patient_on_board = True
siren_on = True

46.2 Constructors

A constructor defines what happens when an object is instantiated from a class (ie created from a blueprint).

The constructor essentially “constructs” the object, specifies the initial values for the attributes, and may even run some methods to start things off.

The constructor is itself a method within the class.

In our example, the constructor might set up the name of the trust and registration number, and specify that the siren is off and there’s no patient loaded at the start.