0% found this document useful (0 votes)
31 views30 pages

Lecture 10

The document discusses creating classes and objects in Python. It explains how to define a class with properties and behaviors/methods, and how to initialize class objects and call their methods. Examples include creating an Employee class to represent employees with properties like name and salary calculation methods, and a Student class with properties like name and behaviors like studying.

Uploaded by

bsmt22098
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views30 pages

Lecture 10

The document discusses creating classes and objects in Python. It explains how to define a class with properties and behaviors/methods, and how to initialize class objects and call their methods. Examples include creating an Employee class to represent employees with properties like name and salary calculation methods, and a Student class with properties like name and behaviors like studying.

Uploaded by

bsmt22098
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Creating classes

and Objects
Lecture 10
Making classes and objects

• Imagine a class Employee

• Who is an employee? (Objects)


• Carol is an employee
• Majid is an employee
• X is an employee
• Y is an employee
How to create a class?

class Employee:
name = ""
age = 0
How to create objects?

a = Employee()
b = Employee()
How are a and b different?

a = Employee()
b = Employee()
a.name = "Carol"
a.age = 31
b.name = "Majid"
b.age = 22
What is a Class Diagram?

Class Name
• Property 1
• Property 2
• Property 3
• Behavior / Action 1
• Behavior / Action 2
• Behavior / Action 3
• Behavior / Action 4
Employee Class

Employee
• Name
• Hourly Rate
• Hours Worked
• Pay Salary
• Calculate Salary
• Add hours worked
• Fire
What’s in the Employee class?

• What are the properties?


• What are the actions/behaviors/things you can do ?
Employee
• Name
• Age
• Hourly Rate
• Hours Worked

• Calculate Salary
How to create a class in Python?

class Employee:
name = ""
age = 0
hourlyRate = 0
hoursWorked = 0
Remember Functions?

def calculateSalary(self):
return self.hourlyRate * self.hoursWorked
Add functions to class as behaviors

Employee
class Employee: • Name
• Age
name = "" • Hourly Rate
• Hours Worked
age = 0
hourlyRate = 5 • Calculate Salary

hoursWorked = 7

def calculateSalary(self):
return self.hourlyRate * self.hoursWorked
Using Employee Class

x = Employee()
x.name = “Karen”
x.age = 22
x.hourlyRate = 15
x.hoursWorked = 7

print(x.calculateSalary())
Just one object?

x = Employee() y = Employee()
x.name = “Karen” y.name = “Mark”
x.age = 22 y.age = 28
x.hourlyRate = 15 y.hourlyRate = 14
x.hoursWorked = 7 y.hoursWorked = 8

print(x.calculateSalary()) print(y.calculateSalary())
Let’s create a Student class

• Student has following properties


• name,
• address,
• roll number
Student

• And following behaviors


• Study
• Eat
• Beat up
Student Class

Student
• Name
• Address
• Roll no
• Study
• Eat
• Beat
• Print
Define Behavior

• Study: “I’m studying and it’s boring”

• Eat: “ Yum yum yum”

• Beaten Up: “Oh uh *** why are you hitting me?”

• Print: Print name, address and id


Behavior/Method

def study():
print("I'm studying and it's boring")
Behavior/Method

def eat():
print("Yum yum yum")
Behavior/Method

def beat():
print("Oh uh *** why are you hitting me?")
Behavior/Method

def print(self):
print(self.name, self.address, self.rollno)
Class

• Create self variables for properties


• Add function prototypes for behavior

• Create objects to use them


Student Class
class Student:
def eat(self):
print("Yum yum yum")

def study(self):
print("I'm studying and it's boring")

def beat(self):
print("Oh uh *** why are you hitting me?")

def print(self):
print(self.name, self.address, self.rollno)
Student Object

s = Student()

s.eat()
s.beat()
s.study()
Only one object?

s = Student()
t = Student()
another = Student()
x = Student()
newstudent = Student()

s.eat()
t.beat()
x.study()
Objects

• Different students have different properties


• Different students behave the same.

Yum yum yum


Initializing properties?

s = Student("Ahmed", "Lahore", 55) s = Student()


t = Student("Kiran", "Peshawar", 25)
x = Student("Jawad", "Karachi", 51)
t = Student()
a = Student("Maria", "Islamabad", 50) x = Student()
a = Student()
s.print()
t.print()
x.print()
a.print()
Constructor

def __init__(self, n, a, r):


self.name = n
self.address = a
self.rollno = r
Properties and Behavior output?
s = Student("Ahmed", "Lahore", 55)
t = Student("Kiran", "Peshawar", 25)
x = Student("Jawad", "Karachi", 51)
a = Student("Maria", "Islamabad", 50)

s.print()
s.eat()
t.print()
t.eat()
x.print()
x.eat()
a.print()
a.eat()
Personalizing Behaviors
s = Student("Ahmed", "Lahore", 55)
t = Student("Kiran", "Peshawar", 25)
x = Student("Jawad", "Karachi", 51)
Ahmed Lahore 55
a = Student("Maria", "Islamabad", 50)
Ahmed eats, yum yum yum
Kiran Peshawar 25
s.print()
Kiran eats, yum yum yum
s.eat()
Jawad Karachi 51
t.print()
Jawad eats, yum yum yum
t.eat()
Maria Islamabad 50
x.print() Maria eats, yum yum yum
x.eat()
a.print()
a.eat()

You might also like