Oops
Oops
It is a programming style where we organize code using objects and classes to make it:
Easier to understand
Easier to maintain
Easier to reuse
OOPS Concepts:
Inheritance Child class can use parent class code Avoid code duplication
1. DOG
class Dog:
def __init__(self, name):
self.name = name
def speak(self):
print(self.name + " says Woof!")
dog1 = Dog("Tommy")
dog1.speak() # Output: Tommy says Woof!
2.Student
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def display(self):
print(f"{self.name} is in grade {self.grade}")
s1 = Student("Ravi", 10)
s1.display()
3.Car
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
def show(self):
print(f"This is a {self.color} {self.brand}")
c1 = Car("Toyota", "Red")
c1.show()
4.Calculator
class Calculator:
def __init__(self, a, b):
self.a = a
self.b = b
def add(self):
print("Sum:", self.a + self.b)
calc = Calculator(5, 3)
calc.add()
5. Book
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def details(self):
print(f"'{self.title}' by {self.author}")
def show_salary(self):
print(f"{self.name}'s salary is ₹{self.salary}")
e1 = Employee("Anita", 50000)
e1.show_salary()