Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

OOPs Python CSE 3216 Module 1 Exam Program

Hdhdhsjsjsjhshsxbbshsjbbbzbsbzbxbzhzjsjdbbxbdbshs

Uploaded by

N.Akshay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

OOPs Python CSE 3216 Module 1 Exam Program

Hdhdhsjsjsjhshsxbbshsjbbbzbsbzbxbzhzjsjdbbxbdbshs

Uploaded by

N.Akshay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Module 1: Introduction to OOPS, Methods

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

### 1. **Classes and Objects, Constructors, Namespaces**

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}")

# Create an object of the class


person1 = Person("Pratik", 30)
person1.display()

### 2. **Encapsulation and Abstraction**

class Account:
def __init__(self, balance):
self.__balance = balance # Private variable (Encapsulation)

def deposit(self, amount):


if amount > 0:
self.__balance += amount

def withdraw(self, amount):


if amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient balance!")

def get_balance(self):
return self.__balance # Public method to access private variable

# Creating an Account object


acc = Account(1000)
acc.deposit(500)
acc.withdraw(200)
print(f"Remaining Balance: {acc.get_balance()}")
### 3. **Creating a Class, The `self` Variable, Constructor - Types of Methods:
Instance, Class, Static Methods**

class Employee:
company_name = "TechCorp" # Class variable

def __init__(self, name, salary):


self.name = name # Instance variable
self.salary = salary

# 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']

# Using the class


emp = Employee("Alice", 60000)
emp.display_details()
print(f"Company: {Employee.get_company_name()}")
print(f"Is Monday a working day? {Employee.is_working_day('Monday')}")
```

### 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}")

# Child class inheriting from Vehicle


class Car(Vehicle):
def __init__(self, brand, model, year):
super().__init__(brand, model) # Use of super()
self.year = year

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()

### 5. **Passing Members of One Class to Another**

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()

# Create an Engine object


engine1 = Engine(150)

# Create a Car object, passing the Engine object as an argument


car1 = Car("Honda", "Civic", engine1)
car1.display_car_info()

You might also like