ALevel-CS Chapter 27 Object-Oriented Programming (OOP)

27.01 Concept of OOP

Key Terms

Record

TYPE CarRecord 
    DECLARE VehicleID             : STRING
    DECLARE Registration          : STRING
    DECLARE DateOfRegistration    : DATE
    DECLARE EngineSize            : INTEGER
    DECLARE PurchasePrice         : CURRENCY
ENDTYPE

PROCEDURE UpdateRegistration(BYREF ThisCar : CarRecord, BYVALUE NewRegistration)
    ThisCar.Registration ← NewRegistration 
ENDPROCEDURE

Classes in OOP

27.02 Designing classes and objects

Key Terms

Designing classes and objects

When a car is manufactured it is given a unique vehicle ID that will remain the same throughout the car’s existence. The engine size of the car is fixed at the time of manufacture. The registration ID will be given to the car when the car is sold.

In our program, when a car is manufactured, we want to create a new car object. We need to instantiate it using the constructor. Any attributes that are already known at the time of instantiation can be set with the constructor. In our example, VehicleID and EngineSize can be set by the constructor. The other attributes are assigned values at the time of purchase and registration. So, we need setters for them.

27.03 Writing object-oriented code

Declaring a class

Attributes should always be declared as ‘Private’. This means they can only be accessed through the class methods. So that the methods can be called from the main program, they have to be declared as ‘Public’. There are other modifiers (such as ‘Protected’), but they are beyond the scope of this book.

Declaring a class in Python

python property

Instantiating a class

To use an object of a class type in a program the object must first be instantiated. This means the memory space must be reserved to store the attributes.

ThisCar = Car("ABC1234", 2500)

Using a method

To call a method in program code, the object identifier is followed by the method identifier and the parameter list.

# sets the purchase price for an object ThisCar of class Car.
ThisCar.SetPurchasePrice(12000) 
ThisCar.PurchasePrice = 12000 # using properties
# gets and prints the vehicle ID for an object ThisCar of class Car.
print(ThisCar.GetVehicleID()) 
print(ThisCar.VehicleID) # using properties

27.04 Inheritance

Key Terms

Inheritance

The advantage of OOP is that we can design a class (a base class or a superclass) and then derive further classes (subclasses) from this base class. This means that we write the code for the base class only once and the subclasses make use of the attributes and methods of the base class, as well as having their own attributes and methods.

Example

Declaring a base class and derived classes (subclasses) in Python

Instantiating a subclass

ThisBook = Book(Title, Author, ItemID) 
ThisCD = CD(Title, Artist, ItemID)

27.05 Polymorphism

Key Terms

Polymorphism

To include the additional attributes from the subclass, we need to declare the method again. Although the method in the subclass will have the same identifier as in the base class, the method will actually behave differently. This is known as polymorphism.

27.06 Garbage collection

When objects are created they occupy memory. When they are no longer needed, they should be made to release that memory, so it can be re-used. If objects do not let go of memory, we eventually end up with no free memory when we try and run a program. This is known as ‘memory leakage’.

Memory management involves a private heap containing all Python objects and data structures. The management of the Python heap is performed by the interpreter itself. The programmer does not need to do any housekeeping.

27.07 Containment (aggregation)

Key Terms

Example