Object Oriented Programming OOP (Autosaved)
Object Oriented Programming OOP (Autosaved)
Programming OOP
Concept of OOP
• OOP groups together the record data structure and the subroutines
that operate on the data items in this data structure.
• Such a group is called an 'object'.
• The feature of data being combined with the subroutines acting on the
data is known as encapsulation.
• To use an object, we first define an object type. An object type is called
a class.
• Encapsulation: combining data and subroutines into a class
• Class: a type that combines a record with the methods that operate on
the properties in the record
Example of a record
• A car manufacturer and seller wants to store details about cars.
• These details can be stored in a record structure
TYPE CarRecord
DECLARE VehicleID PROCEDURE UpdateRegistration(BYREF ThisCar :
DECLARE Registration CarRecord, BYVAL NewRegistration)
DECLARE DateOfRegistration ThisCar.Registration NewRegistration
DECLARE EngineSize END PROCEDURE
DECLARE PurchasePrice
END TYPE
• We can call this procedure from anywhere in our program.
• we can also access the record fields directly from anywhere within the scope of ThisCar:
ThisCar.EngineSize🡨 2500
Classes in OOP
• OOP group together the data structure and subroutine that operate on
the data items in this structure
• Such a group is called an object
• Data of an object are called attributes and the subroutines acting on the
attributes are called methods
Classes in OOP
• The idea behind classes in OOP is that attributes can only be accessed
through methods
• The path to data is unavailable
• Attributes are referred to as 'private'.
• The methods to access the data are made available to programmers, so
these are 'public'.
• Classes are templates for objects.
• When a class type has been defined it can be used to create one or
more objects of this class type
• Therefore an object are instances of a class
New vocabulary
• Attributes: the data items of a class
• Methods: the subroutines of a class
• Object: an instance of a class
• Encapsulation: combining data and subroutine into a class and
restricting external access to the data
Advantages of OOP over procedural languages
class ClassName:
<statement-1>
.
.
.
<statement-N>
Class Objects
• Class objects support two kinds of operations: attribute references and instantiation.
• Attribute references use the standard syntax used for all attribute references in Python: obj.name.
• So, if the class definition looked like this:
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
x = MyClass()
• creates a new instance of the Myclass and assigns this object to the local variable x.
Initialising class object
• The instantiation operation (“calling” a class object) creates an empty object.
• Many classes like to create objects with instances customized to a specific initial state.
• Therefore a class may define a special method named __init__(), like this:
def __init__(self):
self.data = []
class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart
Creating a class
• 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, vehicle ID 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. The identifier table for the car class is shown in Table 27.01.
Creating class
We can represent this information as a class diagram
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'.
• Python include the method body within the class declaration.
Instantiating a class
• The code below shows how a base class and its subclasses are declared in
Python.
Declaring a base class and derived classes (subclasses) in Python
Instantiating a subclass
• Creating an object of a subclass is done in the same way as with any
class
ThisBook = Book(Title, Author, ItemID)
ThisCD = CD (Title, Artist, ItemID)
Using a method
• Using an object created from a subclass is exactly the same as an
object created from any class.
Multiple Inheritance
class Course:
def __init__(self, t, m): # sets up a new course
self.__CourseTitle = t
self.__MaxStudents = m
self.__NumberOfLessons = 0
self.__CourseLesson = []
self.__CourseAssessment = Assessment
def AddLesson(self, t, d, r):
self.__NumberOfLessons = self.__NumberOfLessons + 1
self.__CourseLesson.append(Lesson(t, d, r))
def AddAssessment(self, t, m):
self.__CourseAssessment = Assessment(t, m)
def OutputCourseDetails(self):
print(self.__CourseTitle, "Maximum number: ", self.__MaxStudents)
for i in range(self.__NumberOfLessons):