1. Module1-OOPs in Python
1. Module1-OOPs in Python
Semester-2
1. OOPs in Python
● Introduction to OOPs
● Problems in Procedure-Oriented Approach
● Features of Object-Oriented Programming System (OOPS)
● Constructors and Destructors
● Classes and Objects
● Creating a Class
● Self-Variable
● Types of Variables
● Types of Methods
● Passing Members of One Class to Another Class
● Problems in Procedure-Oriented Approach
s1 = Student("Aditya", 18)
s2 = Student("Siddhi", 19)
print(s1.name) # Output: Aditya
print(s2.name) # Output: Siddhi
***
(ii) Class Variables:
These variables are shared among all instances of a class and are defined
outside methods but within the class.
class ClassName:
variable_name = value
Example:
class School:
school_name = "VPM College" # Class variable
print(School.school_name) # Output: VPM College
(iii) Local Variables:
These variables are defined inside a method and can only be used within that method.
def method_name(self):
variable_name = value
Example:
class Calculator:
def add(self, a, b):
result = a + b # Local variable
return result
calc = Calculator()
print(calc.add(10, 5)) # Output: 15
2. Types of Methods in Python
Methods in Python are functions inside a class. They can be Instance Methods, Class Methods, or
Static Methods.
Example:
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self): # Instance method
return 3.14 * self.radius ** 2
c1 = Circle(5)
***
(ii) Class Methods:
These methods operate on class variables and use the @classmethod decorator. They require cls
as the first parameter.
@classmethod
def method_name(cls, args):
# method body
class School:
school_name = "XYZ College"
@classmethod
def get_school_name(cls):
return cls.school_name
class Math:
@staticmethod
def add(a, b):
return a + b
class ClassB: has its own __init__ method, which accepts make and
def __init__(self, class_a_instance): engine as arguments and assigns the engine object
self.class_a = class_a_instance
to the engine attribute. An Engine instance with a
Example: horsepower of 150 is created, followed by a Car
class Engine:
def __init__(self, horsepower): instance with the make "Toyota" and the engine object.
self.horsepower = horsepower
This demonstrates that the Engine object is passed to
class Car: the Car class, so car.engine.horsepower will return
def __init__(self, make, engine):
self.make = make 150, confirming the successful integration of the
self.engine = engine
# Creating instances engine into the car.
engine = Engine(150)
car = Car("Toyota", engine)
print(car.make) # Output: Toyota
print(car.engine.horsepower) # Output: 150
Programming Questions
1. Introduction to OOPs: Create a class Student with attributes name and age. Implement a method to display
the student's details.
2. Problems in Procedure-Oriented Approach: Write a function to calculate the area of a rectangle, then
rewrite it using a class with attributes length and width.
3. Features of Object-Oriented Programming System (OOPS): Create a class Car and a subclass
ElectricCar. Add attributes and a method to display car details, demonstrating inheritance and polymorphism.
4. Constructors and Destructors: Create a class Book with attributes title and author. Use a constructor to
initialize and a destructor to print a message on deletion.
5. Passing Members of One Class to Another Class: Create two classes: Engine with horsepower, and Car
that accepts an Engine object. Display the engine's horsepower from the car.