Object-oriented programming (OOP) models a program as a set of objects, each combining data and the code that works on that data. This matches how we think about real things such as students, bank accounts or game characters.
Classes and objects
A class is a blueprint that defines what data (attributes) and behaviour (methods) its objects have. An object is one actual instance made from the class. Creating an object is called instantiation.
- Attribute (property): a variable belonging to the object, such as Name or Balance.
- Method: a subroutine belonging to the class, such as Deposit.
- Constructor: a special method (
NEW) that runs when an object is created and sets its initial values.
Cambridge pseudocode:
CLASS Account
PRIVATE Owner : STRING
PRIVATE Balance : REAL
PUBLIC PROCEDURE NEW(Name : STRING, Start : REAL)
Owner <- Name
Balance <- Start
ENDPROCEDURE
PUBLIC PROCEDURE Deposit(Amount : REAL)
IF Amount > 0 THEN
Balance <- Balance + Amount
ENDIF
ENDPROCEDURE
PUBLIC FUNCTION GetBalance() RETURNS REAL
RETURN Balance
ENDFUNCTION
ENDCLASS
MyAcc <- NEW Account("Sam", 100)
MyAcc.Deposit(50)
OUTPUT MyAcc.GetBalance() // 150
Many objects can come from one class, each with its own attribute values: MyAcc and YourAcc have separate balances but share the same methods.
Access and encapsulation
Attributes are usually private, so outside code cannot change them directly. Access goes through public methods, such as Deposit, which can check the data is valid. This protects the object’s state and is called encapsulation. Getter methods read private data and setter methods change it safely.
Design with classes
To design a class, list the real-world things in the problem, decide their attributes and what they can do, then draw a class diagram: a box with the class name, the attribute list and the method list.
Account
-------------------
- Owner : STRING
- Balance : REAL
-------------------
+ Deposit(Amount)
+ GetBalance()
A minus sign marks private members and a plus sign marks public ones. Benefits of OOP: code is modular, easier to reuse and maintain, and large systems are easier to organise.
Check your understanding
What is the difference between a class and an object?
- A class is a blueprint and an object is an instance of it
- They are the same
- An object is a blueprint
- A class is a variable
Answer: A class is a blueprint and an object is an instance of it. Many objects can share one class.
What does a constructor do?
- Deletes an object
- Initialises a new object when it is created
- Prints output
- Sorts data
Answer: Initialises a new object when it is created. In pseudocode it is called NEW.
Why are attributes usually private?
- To protect data from being changed incorrectly from outside
- To save memory
- To make code run faster
- So they cannot be used
Answer: To protect data from being changed incorrectly from outside. Public methods control access.
What is instantiation?
- Creating an object from a class
- Deleting a class
- Compiling code
- Copying a file
Answer: Creating an object from a class. The constructor runs at this point.
