Search Header Logo
F6-CS OOP

F6-CS OOP

Assessment

Presentation

Computers

12th Grade

Practice Problem

Medium

Created by

Adrien Ebitner

Used 2+ times

FREE Resource

20 Slides • 2 Questions

1

Object Oriented Programming

2

What is OOP?

  • method of structuring a program

  • using individual objects

  • think of it like components of a system

  • an object contains data

3

What is OOP?

  • is a programming paradigm (way to classify programming language based on)

  • strucutre like objects

  • For e.g. an object could represent a person

  • with properties like name, age, address

  • behaviors such as walking, talking, breathing, etc.

4

Objectives

  • Create a class

  • using class to create new objects

  • model systems with class inheritance

5

Class

  • why do we need classes?

  • sometimes we need classes for many reasons

  • remember list in Python?

  • look at this list

  • pikachu = [electric, ground, [electric, fly, steel], 35, 55, 40, 50, 50, 90]

  • many issues

6

Class

  • pikachu = [electric, ground, [electric, fly, steel], 35, 55, 40, 50, 50, 90]

  • when the list gets long, will you remember the element with index 5 mean?

  • what if you have another list but, it is shorter?

  • it is too inconsistent

7

How to Define a Class

  • class Dog:

    pass

  • usually PascalCase

8

How to Define a Class

class Dog:

    def __init__(self, name, age):

        self.name = name

        self.age = age

9

How to Define a Class

class Dog:

    def __init__(self, name, age):

        self.name = name

        self.age = age

__init__ is an intance method that initializes a newly created object. It takes the object as its first argument followed by additional arguments.

10

How to Define a Class

class Dog:

    def __init__(self, name, age):

        self.name = name

        self.age = age

attributes created in .__init__() are called instance attributes. Meaning all Dog objects have a name and an age, but the values may vary

11

adding class attributes

class Dog:
# Class attribute
species = "Canis familiaris"

    def __init__(self, name, age):

        self.name = name

        self.age = age

12

adding class attributes

class Dog:
# Class attribute
species = "Canis familiaris"

   
Class attributes are different, where all class attributes are directly beneath the first line of the class name. All class instances will have the same value for class attributes.

13

to create instance

buddy = Dog("Buddy", 9)
miles = Dog("Miles", 4)

  • These create two new Dog instances

  • Why we don't need 3 arguments?

    When creating a new instance, python will pass the instance to the first paramter of .__init__()

14

to create instance

buddy.name
>> 'Buddy'

miles.age
>> 4

buddy.species
'Canis familiaris'

15

to create instance

You can change the different instance's attribute's value

>>buddy.age = 10
>>buddy.age
10
>>miles.species = "Felis silvestris"
>>miles.species
'Felis silvetris'

16

Fill in the Blank

What is the line to create a new class called School?

:

17

Fill in the Blank

Type the way to define the method for init incluiding the following arguments excluding self, (vehicleId, Engine)

hint: ?? __init__(??, ??, ??):

_
_
_
_
(
,
,
)
:

18

Inheritance

A process by which the methods and data from one class, superclass or base class, are copied to another class, a derived class

19

to create method

Write a short program to declare a class, student, with the private attributes name, dateOfBirth and examMark, and the public method displayExamMark. Declare an object myStudent, with a name and exam

mark of your choice, and use your method to display the exam mark.

20

Inheritance

media

21

Inheritance

media

22

Exercise

Object Oriented Programming

Show answer

Auto Play

Slide 1 / 22

SLIDE