Week10-Chapter 10-Classes and Object-Oriented Programming
Week10-Chapter 10-Classes and Object-Oriented Programming
Faculty of Engineering
CEN121
COMPUTER PROGRAMMING 1
Chapter 10
2
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Procedural Programming
3
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Object-Oriented Programming
Object-oriented programming: focused on creating objects. Python is
an object oriented programming language. Almost everything in
Python is an object, with its properties and methods.object-oriented
5
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Classes
6
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
To create a class, use the keyword class:
print(MyClass)
Now we can use the class named MyClass to create
objects:
class MyClass:
x = 5
p1 = MyClass()
print(p1.x)
7
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Class Definitions
Class definition: the set of statements that define a class’s methods
and data attributes.
8
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
class Person: The previous examples are classes and objects in their
def __init__(self, name, age): simplest form and are not really useful in real-life
self.name = name applications.
self.age = age
To understand the meaning of classes we have to understand
p1 = Person("John", 36) the built-in __init__() function.
print(p1.name) All classes have a function called __init__(), which is
print(p1.age) always executed when the class is being initiated.
9
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Objects can also contain methods. Methods in objects are functions that belong to the object.
Note: The self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.
Let us create a method in the Person class:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
10
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
The self parameter is a reference to the current instance of the class and is used to access variables that belongs to the class.
It does not have to be named self, you can call it whatever you like, but it has to be the first parameter of any function in the class.
If we want to use the words mysillyobject and abc instead of self:
11
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
import random
# The Coin class simulates a coin that can
class Coin:
def __init__(self):
self.sideup = 'Heads'
def toss(self):
if random.randint(0, 1) == 0:
self.sideup = 'Heads'
else:
self.sideup = 'Tails' This side is up: Heads
def get_sideup(self): I am tossing the coin ...
return self.sideup
This side is up: Tails
def main():
my_coin=Coin()
print('This side is up:', my_coin.get_sideup())
print('I am tossing the coin ...')
my_coin.toss()
print('This side is up:', my_coin.get_sideup())
if __name__ == '__main__':
main()
12
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
13
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
To create a new instance of a class call the initializer method.
To call any of the class methods using the created instance, use dot notation.
*Format: My_instance.method()
*Because the self parameter references the specific instance of the object, the method will affect this instance.
Reference to self is passed automatically.
14
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Hiding Attributes and Storing Classes in Modules
15
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
import random
class Coin:
def __init__(self):
self.__sideup = 'Heads'
# The toss method generates a random number
# in the range of 0 through 1. If the number
# is 0, then sideup is set to 'Heads'.
# Otherwise, sideup is set to 'Tails'.
def toss(self):
if random.randint(0, 1) == 0:
self.__sideup = 'Heads'
else:
self.__sideup = 'Tails'
def get_sideup(self):
return self.__sideup
This side is up: Heads
# The main function.
def main(): I am going to toss the coin ten times:
# Create an object from the Coin class.
my_coin = Coin() Tails
# Display the side of the coin that is facing up. Tails
print('This side is up:', my_coin.get_sideup())
Tails
# Toss the coin.
print('I am going to toss the coin ten times:') Heads
for count in range(10):
my_coin.toss() Tails
print(my_coin.get_sideup())
Tails
# Call the main function.
if __name__== '__main__': Heads
main()
Tails
Tails
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
Tails
16
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
17
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
The BankAccount Class – More About Classes
Class methods can have multiple parameters in addition to
self.
18
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
19
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
The __str__ method
20
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
The __str__() function controls what should be returned
def __str__(self):
when the class object is represented as a string. return f"{self.name}({self.age})"
John(36)
21
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Working With Instances
Instance attribute: belongs to a specific instance of a class.
22
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
23
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Accessor and Mutator Methods
*Safe way for code outside the class to retrieve the value of
attributes.
24
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Passing Objects as Arguments
25
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Techniques for Designing Classes
26
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Finding the Classes in a Problem
When developing the object-oriented program, the first goal is to identify classes.
*Typically involves identifying the real-world objects that are in the problem.
27
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
1. Get a written description of the problem domain:
*Remove nouns that represent items that the program does not need to be concerned with.
*Remove nouns that represent simple values that can be assigned to a variable.
28
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Identifying a Class’s Responsibilities
29
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Summary
This chapter covered:
• Procedural vs. object-oriented programming
• Classes and instances
• Class definitions, including:
*The self parameter
*Data attributes and methods
*__init__ and __str__ functions
*Hiding attributes from code outside a class
• Storing classes in modules
• Designing classes
30
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Asst.Prof. Zehra Merve Cinan
Faculty of Engineering
Department of Electrical and Electronics Engineering,
8th Block, Floor: 2
Office: 2521