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
Getter - a method to access its associated attribute
Constructor - a special type of method that is called to create a new object and initialise its attributes
Setter - a method to set the value of its associated attribute
Property - a class member that includes the attribute and also getter and setter method calls to access/set the attribute value
Getter
Constructor
Setter
Property
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.
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.
python property
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)
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
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.
ThisBook = Book(Title, Author, ItemID)
ThisCD = CD(Title, Artist, ItemID)
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.
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.