1 of 25

COL100 �Introduction to Computer Science

Lecture 20 – Object-Oriented Programming

April 2025

Semester II

2024-2025

Vireshwar Kumar, Aaditeshwar Seth

CSE@IITD

2 of 25

Agenda

  • OOP

2

Disclaimer: The contents of these slides are taken from various publicly available resources such as books, talks and lectures. They are being used for the purpose of classroom teaching and academic dissemination only. The sources are acknowledged wherever possible.

3 of 25

Beyond built-in types

  • Built-in types: int, bool, float, strings, tuples, lists, dicts, …

​

  • In practice, information usually has internal structure, meaningful relationships between different pieces

​

  • We can represent this structure by defining our own types, called classes

3

4 of 25

Class, Object, and OOP

  • Class
    • A program entity representing template for a new type of objects.

​

  • Object
    • An instance of a particular class

​

  • Object-Oriented Programming (OOP)
    • Programs that perform their behavior as interactions between objects.

​

​

4

5 of 25

Object Oriented Programming

  • Create our own collections of data
  • Organize information
  • Division of work
  • Access information in a consistent manner
  • Add layers of complexity

​

5

6 of 25

Person Class

class Person:

def __init__(self, name, email, phone_ext):

self.name = name

self.email = email

self.phone_ext = phone_ext

p1 = Person("Amit", "amit@iitd.ac.in", "1384")

p2 = Person(“Aadi”, aseth@iitd.ac.in, “7256”)

print(type(p1))

print(p1.name, p1.email, p1.phone_ext)

print(p2.name, p2.email, p2.phone_ext)

people = [p1, p2]

print(p1[0].name, p1[0].email, p1[0].phone_ext)

6

7 of 25

How Classes Enforce OOP Concepts?

  • Abstraction
    • Representation
  • Encapsulation
    • Data and methods
  • Inheritance
    • Reusing the created classes

​

7

8 of 25

Student Class

class Student:

def __init__(self,name,number):

self.name = name

self.number = number

8

9 of 25

Sport Class

class Sport:

def __init__(self,name:str,team:list,max_budget,balance_amount):

self.name = name

self.team = team

self.max_budget = max_budget

self.balance_amount = balance_amount

#method

def size_of_team(self):

return len(team)

def spend_budget(self,spent_money):

self.balance_amount = self.balance_amount - spent_money

return self.balance_amount

def add_budget(self,added_money):

self.balance_amount = self.balance_amount + added_money

return self.balance_amountclear

9

10 of 25

BSA Class and Objects

class BSA:

def __init__(self, sports: list):

self.sports = sports

​

A = Student("Amit","2345")

B = Student("Viresh","1345")

bsa_board = BSA([Sport("badminton",[A,B],500,50), Sport("cricket",[A,B],500,50),Sport("football",[A,B],500,50)])

​

print(bsa_board.sports[1].add_budget(100))

10

11 of 25

How Classes Enforce OOP Concepts?

  • Abstraction
    • Representation
  • Encapsulation
    • Data and methods
  • Inheritance
    • Reusing the created classes

​

11

12 of 25

City Class

class City:

def __init__(self, name: str, country: str):

self.name = name

self.country = country

​

def __repr__(self):

return "|" + self.name + ", " + self.country + "|"

12

13 of 25

Person Class

class Person:

def __init__(self, name: str, email: str, city: City):

self.name = name

self.email = email

self.cities = []

self.cities.append((city, "01-01-2020"))

self.nickname = None

self.age = -1

def next_travel(self, city, date_of_travel):

self.cities.append((city, date_of_travel))

def num_cities_traveled(self):

return len(self.cities)

def assign_nickname(self, nickname):

self.nickname = nickname

​

def set_age(self, age):

self.__age = age

​

def __repr__(self):

return f"|{self.name}, {self.email}, {len(self.cities)}, {self.cities}|"

13

14 of 25

Using the City and Person Classes

c1 = City("New Delhi", "India")

c2 = City("New York", "USA")

c3 = City("Paris", "France")

​

p1 = Person("aaditeshwar", "aadi@iitd.ac.in", c1)

p2 = Person("viresh", "viresh@iitd.ac.in", c2)

p3 = Person("amit", "amit@iitd.ac.in", c1)

p4 = Person("maya", "maya@iitd.ac.in", c1)

​

print(p1, "\n", p2, "\n", p3, "\n", p4)

​

p1.next_travel(c2, "5 May 2021")

print(p1, "\n", p2, "\n", p3, "\n", p4)

​

p2.next_travel(c3, "10 Jun 2022")

p2.next_travel(c1, "30 Jul 2023")

print(p1, "\n", p2, "\n", p3, "\n", p4)

14

15 of 25

How Classes Enforce OOP Concepts?

  • Abstraction
    • Representation
  • Encapsulation
    • Data and methods
  • Inheritance
    • Reusing the created classes

​

15

16 of 25

Slot Class

class Slot:

def __init__(self, day, start_time, end_time):

self.day = day

self.start_time = start_time

self.end_time = end_time

16

17 of 25

Course Class

class Course:

def __init__(self, code, lecture_slot, tutorial_slot, lab_slot):

self.code = code

self.lecture_slot = lecture_slot

self.tutorial_slot = tutorial_slot

self.lab_slot = lab_slot

def total_lecture_duration(self):

# while loop

i = 0

lecture_time = 0

while (i < len(self.lecture_slot)):

lecture_time = lecture_time + (self.lecture_slot[i].end_time - self.lecture_slot[i].start_time)

i = i + 1

return lecture_time

17

18 of 25

Course Class (Cont.)

def total_lab_duration(self):

# while loop

i = 0

lab_time = 0

while (i < len(self.lab_slot)):

lab_time = lab_time + (self.lab_slot[i].end_time - self.lab_slot[i].start_time)

i = i + 1

return lab_time

​

def total_contact_duration(self):

contact_time = self.total_lecture_duration() + self.total_lab_duration()

return contact_time

​

18

19 of 25

Semester Schedule Class

class SemesterSchedule:

def __init__(self, courses):

self.courses = courses

19

20 of 25

Slot and Course Objects

lecture_slot_acol100 = [Slot("Monday", 9.5, 11), Slot("Thursday", 9.5, 11)]

lab_slot_acol100 = [Slot("Monday", 14, 16), Slot("Tuesday", 14, 16), Slot("Wednesday", 14, 16), Slot("Thursday", 14, 16)]

course_acol100 = Course("ACOL100", lecture_slot_acol100, [], lab_slot_acol100)

​

lecture_slot_amtl100 = [Slot("Monday", 11, 12), Slot("Thursday", 11, 12)]

tutorial_slot_amtl100 = [Slot("Monday", 14, 15), Slot("Tuesday", 14, 15), Slot("Wednesday", 14, 15), Slot("Thursday", 14, 15)]

course_amtl100 = Course("ACOL100", lecture_slot_amtl100, tutorial_slot_amtl100, [])

​

btech_course_schedule = SemesterSchedule([course_acol100, course_amtl100])

print(btech_course_schedule.courses[1].total_contact_duration())

20

21 of 25

How Classes Enforce OOP Concepts?

  • Abstraction
    • Representation
  • Encapsulation
    • Data and methods
  • Inheritance
    • Reusing the created classes

​

21

22 of 25

Abstraction

class Student:

def __init__(self, name, email, entryNumber, score):

self.name = name

self.email = email

self.entryNumber = entryNumber

self.score = score

​

amit = Student("Amit", "amit@iitd.ac.in", "2024EE10350", 75)

print(amit.name)

print(amit.email)

print(amit.entryNumber)

print(amit.score)

COL100: Introduction to Computer Science (Semester II, 2024-2025)

22

23 of 25

Encapsulation

class Student:

def __init__(self, name, email, entryNumber, score):

self.name = name

self.email = email

self.entryNumber = entryNumber

self.score = score

def updateName(self, newName):

self.name = newName

​

amit = Student("Amit", "amit@iitd.ac.in", "2024EE10350", 75)

amit.updateName("Amit Jain")

print(amit.name)

COL100: Introduction to Computer Science (Semester II, 2024-2025)

23

24 of 25

Inheritance

class SportsPerson(Student):

def assignLetterGrade(self):

if(self.score > 80): return "A"

else: return "A-"

varun = SportsPerson("Varun", "varun@iitd.ac.in", "2024CE10323", 75)

print(varun.name)

varun.updateName("Varun Sethi")

print(varun.name)

print(varun.assignLetterGrade())

COL100: Introduction to Computer Science (Semester II, 2024-2025)

24

25 of 25

Polymorphism

class SportsPerson(Student):

def assignLetterGrade(self):

if(self.score > 80): return "A"

else: return "A-"

class Artist(Student):

def assignLetterGrade(self):

if(self.score > 60): return "A"

else: return "A-"

​

varun = SportsPerson("Varun", "varun@iitd.ac.in", "2024CE10323", 75)

print(varun.assignLetterGrade())

salman = Artist("Salman", "salman@iitd.ac.in", "2024PH10235", 75)

print(salman.assignLetterGrade())

COL100: Introduction to Computer Science (Semester II, 2024-2025)

25