0% found this document useful (0 votes)
7 views

Python Programs for Oops

Uploaded by

Vandana Bharti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Programs for Oops

Uploaded by

Vandana Bharti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Python programs for Object-Oriented Programming (OOP) concepts that are

commonly covered in labs:

### 1. **Class and Object**

```python
# Define a class
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade

def display_info(self):
print(f"Name: {self.name}, Age: {self.age}, Grade: {self.grade}")

# Create an object of the class


student1 = Student("John", 20, "A")
student1.display_info()
```

### 2. **Encapsulation**

```python
class Employee:
def __init__(self, name, salary):
self.__name = name # Private attribute
self.__salary = salary # Private attribute

def get_info(self):
return f"Employee: {self.__name}, Salary: {self.__salary}"

def set_salary(self, new_salary):


if new_salary > 0:
self.__salary = new_salary
else:
print("Invalid salary!")

emp = Employee("Alice", 50000)


print(emp.get_info())
emp.set_salary(55000)
print(emp.get_info())
```

### 3. **Inheritance**

```python
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound.")

# Child class
class Dog(Animal):
def speak(self):
print(f"{self.name} barks.")

# Child class
class Cat(Animal):
def speak(self):
print(f"{self.name} meows.")

# Create instances
dog = Dog("Buddy")
cat = Cat("Kitty")

dog.speak()
cat.speak()
```

### 4. **Polymorphism**

```python
class Bird:
def sound(self):
print("Birds make sound")

class Sparrow(Bird):
def sound(self):
print("Sparrow chirps")

class Pigeon(Bird):
def sound(self):
print("Pigeon coos")

def make_sound(bird):
bird.sound()

sparrow = Sparrow()
pigeon = Pigeon()

make_sound(sparrow)
make_sound(pigeon)
```

### 5. **Abstraction (Using Abstract Base Classes)**

```python
from abc import ABC, abstractmethod

# Abstract class
class Shape(ABC):
@abstractmethod
def area(self):
pass

# Subclass implementing the abstract method


class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height

def area(self):
return self.width * self.height

class Circle(Shape):
def __init__(self, radius):
self.radius = radius

def area(self):
return 3.14 * self.radius ** 2

# Create objects
rect = Rectangle(5, 3)
circle = Circle(7)

print(f"Rectangle area: {rect.area()}")


print(f"Circle area: {circle.area()}")
```

### 6. **Method Overloading (Using Default Arguments)**

```python
class MathOperations:
def add(self, a=None, b=None, c=None):
if a is not None and b is not None and c is not None:
return a + b + c
elif a is not None and b is not None:
return a + b
else:
return "Insufficient arguments"

# Create an object
math_op = MathOperations()

print(math_op.add(10, 20))
print(math_op.add(10, 20, 30))
print(math_op.add(10))
```

### 7. **Method Overriding**

```python
class Parent:
def show(self):
print("This is the parent class.")

class Child(Parent):
def show(self):
print("This is the child class overriding the parent class.")

# Create objects
parent_obj = Parent()
child_obj = Child()

parent_obj.show()
child_obj.show()
```

### 8. **Constructor Overloading (Using Multiple `__init__` with Default


Values)**

```python
class Person:
def __init__(self, name=None, age=None):
if name is not None and age is not None:
print(f"Name: {name}, Age: {age}")
elif name is not None:
print(f"Name: {name}")
else:
print("No information provided")

# Create objects with different constructors


person1 = Person("John", 30)
person2 = Person("Alice")
person3 = Person()
```

### 9. **Multiple Inheritance**

```python
class Father:
def skill(self):
print("Father is good at painting.")

class Mother:
def skill(self):
print("Mother is good at singing.")
class Child(Father, Mother):
def skill(self):
super().skill() # Calls skill method from Father due to method resolution
order

# Create object
child = Child()
child.skill()
```

### 10. **Operator Overloading**

```python
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y

# Overloading + operator
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)

def __str__(self):
return f"Point({self.x}, {self.y})"

# Create objects
p1 = Point(2, 3)
p2 = Point(4, 5)

# Add objects
p3 = p1 + p2
print(p3)
```

You might also like