Object Oriented Programming using Python
-
Refer Here for basics of OOP
-
Basic constructs of OOP
- class
- object
- attributes/members
- methods
-
objects represent litrerally anything (few examples below)
- bank account
- car
- engine
- movie
- theatre
- transcation
-
object will have contents and charecteristics
- laptop:
- contents: (What it has) => members
- cpu
- ram
- disk
- monitor
- keyboard
- charecteristics: (What it does) => methods
- started
- stopped
- browse
- run applications
- contents: (What it has) => members
- bank account
- contents: (What it has) => members
- id
- name
- address
- mobile
- balance
- type
- characteristics: => methods
- deposit
- withdraw
- close
- contents: (What it has) => members
- laptop:
-
Class: This is a design/template of object, we use class to create objects.
-
Easiest way to represnt classes is uml diagram Refer Here
-
oop in python
- Real Python Refer Here
- Programiz Refer Here
-
Refer Here for basic oop in python
Problem 1:
- Lets create a demonstration of Bank Account
"""
This module will have Bank Account class
"""
class BankAccount:
"""
This class will have basic Bank Account Functionality
"""
def __init__(self, account_number:str,name:str, initial_deposit:float = 0) -> None:
"""
This method initializes the object with initial values
"""
self.account_number = account_number
self.name = name
self.balance = initial_deposit
def withdraw(self, amount):
"""
This method withdraw amount
"""
self.balance -= amount
def deposit(self, amount):
"""
This method deposits amount
"""
self.balance += amount
- Debug the account creation, deposits and withdrawls. Always print the balance
- Refer Here for the project structure.
Objects: Inheritence (is-a), composition (has-a)
- Refer Here for tutorial on Inheritance and composition
Problem 2
- We have two types of bank accounts
- savings account
- donot allow deposits more than 1 Million or 10 lakhs
- dont allow negativ balance
- Current account
- allow deposits of any amount
- allow negative balance
- savings account
- Refer Here for changes
Problem 3
- Create Student and Course implemenation according to any institute and design classes. Refer Here for changes