OOP Flashcards
Class
Defines a type of object using OOP.
each object is an instance of the class
Attribute
a value within an OOP class
OOP Method
procedures or functions written within an OOP class
OOP Object
an independent entity
OOP defines the attributes of an object
OOP defines the methods that can be applied to the object
many instances of the object can be declared
Constructor in OOP
takes place immediatelly after defining the class .
-sets attributes
- gives them starting values
- provides parameters
write a constructor in python
(with two attributes called att1 and att2, set to the parameters p1 and p2)
def __init__(self, p1, p2):
self.att1=p1
self.att2=p2
advantages of OOP
easier to read code
less work in long run
better control over the process
fewer erros
good for team work
disadvantages of OOP
needs careful planning
more work at the start
objects must be similar
class Car():
def__init__(self, p1, p2):
self.colour = p1
self.make = p2
self.speed = 0
create an instance of the car class called mycar that is a pink miata
mycar = Car(“Pink”,”Miata”)
imagine this method is written within an OOP class:
def gofaster(self):
self.speed = self.speed + 10
in the main program, write a line of code to use this method on mycar
mycar.gofaster()
rewrite this constructor in psuedocode:
class Car():
def__init__(self, p1, p2):
self.colour = p1
self.make = p2
self.speed = 0
class Car
procedure new(p1, p2)
colour = p1
make = p2
speed = 0
end procedure
end class
inheritance
one class can “inherit” all the features of another class.
show the first line of a class definition of a class Y that inherits class X
Class Y inherits X
what do we call the class that inherits / the class it inherits from
super class and sub class
or
parent class and child class
encapsulation
using private methods and attribites in OOP
- they cannot be altered directly, only accessed via “get” and “set” methods.
get and set methods
if an attribute is private we cant show or change it in the main program, so get and set methods are used instead
write a get method for the attribute colour in pseudocode
public procedure getcolour()
return colour
end procedure
write a set method for the attribute colour in psuedocode, using p1 as the parameter
public procedure setcolour(p1)
colour = p1
end procedure
What is inheritance in OOP
A sub class can inherit attributes and methods from an existing super - class. The subclass is then defined by making alterations or additions to the super- class.
(psuedocode) here is a superclass.
class Employee
public procedure new(surname, jobtitle):
surname = surname
jobtitle = jobtitle
end procedure
endclass
now write a subclass for manager with their name and title, explaining which line inherits what.
-class manager inherits Employee
- public procedure new (thisname, thistitle)
- super.new( thisname, thistitle)
- end procedure
-endclass
the first line inherits all the methods of the superclass
the third line inherits all attributes from the super class
here is the pseudocode for a sub- class .
class manager inherits Employee
public procedure new (thisname, thistitle)
super.new( thisname, thistitle)
end procedure
endclass
add a line to this constructor, that will add a new attribute called “manages” and set the variable to 0 .
class manager inherits Employee
public procedure new (thisname, thistitle)
super.new( thisname, thistitle)
manages = 0
end procedure
endclass
(psuedocode) here is a class defenition. Now write it in python
class Employee
public procedure new(surname, jobtitle):
surname = surname
jobtitle = jobtitle
end procedure
endclass
Class Employee():
def __init__(self, surname, jobtitle)
self.surname = surname
self.jobtitle = jobtitile
here is a superclass written in python.
Class Employee():
def __init__(self, surname, jobtitle)
self.surname = surname
self.jobtitle = jobtitile
now write a subclass for manager with their name and title
class Manager (Employee):
def __init__(self, thisname, thistitle)
super(). __init__ (thisname, thistitle)
end procedure
endclass
in python, write the opening line for a class constructor called car.
then, write the opening line for a class constructor called car which inherits the class vehicle
class Car():
class Car(Vehicle):