OOPs Python CSE 3216 Module 1 Exam Program
OOPs Python CSE 3216 Module 1 Exam Program
Classes and Objects, Constructors, Namespaces, Encapsulation, Abstraction, Creating a Class, The self variable,
Constructor - Types of Methods: Instance Method, Class Method, Static Method, Passing members of one class to
another class - Create a Parent Class, Create a Child Class, Add the __init__() Function, Use the super() Function, Add
Properties
class Person:
# Constructor
def __init__(self, name, age):
self.name = name # Namespace (instance variable)
self.age = age
# Instance Method
def display(self):
print(f"Name: {self.name}, Age: {self.age}")
class Account:
def __init__(self, balance):
self.__balance = balance # Private variable (Encapsulation)
def get_balance(self):
return self.__balance # Public method to access private variable
class Employee:
company_name = "TechCorp" # Class variable
# Instance Method
def display_details(self):
print(f"Employee: {self.name}, Salary: {self.salary}")
# Class Method
@classmethod
def get_company_name(cls):
return cls.company_name
# Static Method
@staticmethod
def is_working_day(day):
return day.lower() not in ['saturday', 'sunday']
### 4. **Inheritance: Create a Parent Class, Create a Child Class, Add the `__init__()` and
`super()` Functions**
class Vehicle:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
print(f"Brand: {self.brand}, Model: {self.model}")
def display_car_info(self):
print(f"Car Info: {self.brand} {self.model} - {self.year}")
# Creating an object of the Child class
car1 = Car("Toyota", "Camry", 2021)
car1.display_car_info()
class Engine:
def __init__(self, horsepower):
self.horsepower = horsepower
def display_engine_info(self):
print(f"Engine Horsepower: {self.horsepower} HP")
class Car:
def __init__(self, brand, model, engine):
self.brand = brand
self.model = model
self.engine = engine # Passing object of Engine class
def display_car_info(self):
print(f"Car: {self.brand} {self.model}")
self.engine.display_engine_info()