About Lesson
Design a Class for a Simple Banking System
- Objective: Design a class to represent a simple banking system with attributes like account number, account holder name, and balance. Include methods for depositing and withdrawing money.
- Steps:
- Define the
BankAccount
class with attributes and methods. - Create objects of the
BankAccount
class and perform operations.
python
class BankAccount: def __init__(self, account_number, account_holder, balance=0): self.account_number = account_number self.account_holder = account_holder self.balance = balance def deposit(self, amount): self.balance += amount print(f"Deposited {amount}. New balance is {self.balance}") def withdraw(self, amount): if amount <= self.balance: self.balance -= amount print(f"Withdrew {amount}. New balance is {self.balance}") else: print("Insufficient funds") account = BankAccount("123456", "John Doe", 1000) account.deposit(500) account.withdraw(200)
- Define the
Implement Inheritance with a Base Class and Derived Classes
- Objective: Implement inheritance by creating a base class and derived classes. For example, create a base class
Vehicle
and derived classesCar
andBike
. - Steps:
- Define the
Vehicle
base class with common attributes and methods. - Define the
Car
andBike
classes that inherit fromVehicle
.
python
class Vehicle: def __init__(self, make, model, year): self.make = make self.model = model self.year = year def display_info(self): print(f"{self.year} {self.make} {self.model}") class Car(Vehicle): def __init__(self, make, model, year, doors): super().__init__(make, model, year) self.doors = doors def display_info(self): super().display_info() print(f"Doors: {self.doors}") class Bike(Vehicle): def __init__(self, make, model, year, type): super().__init__(make, model, year) self.type = type def display_info(self): super().display_info() print(f"Type: {self.type}") car = Car("Toyota", "Corolla", 2020, 4) bike = Bike("Yamaha", "MT-07", 2021, "Sport") car.display_info() bike.display_info()
- Define the