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

5th chap question python

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including definitions and examples of classes, objects, constructors, destructors, data hiding, method overloading, and method overriding. It explains the use of the 'self' parameter, data abstraction, and composition, along with inheritance types such as single, multiple, and multilevel inheritance. Additionally, it includes programming examples demonstrating parameterized constructors, default arguments, and the deletion of class objects.

Uploaded by

dasmitag147
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

5th chap question python

The document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including definitions and examples of classes, objects, constructors, destructors, data hiding, method overloading, and method overriding. It explains the use of the 'self' parameter, data abstraction, and composition, along with inheritance types such as single, multiple, and multilevel inheritance. Additionally, it includes programming examples demonstrating parameterized constructors, default arguments, and the deletion of class objects.

Uploaded by

dasmitag147
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

5.

OBJECT ORIENTED PROGRAMMING IN PYTHON(12 MARKS)


1. Define class? How to create an class and its object in python.(CO5)
2m(W-22)
⇨ Class:
A class is a block of statement that combine data and operations, which
are performed on the data, into a group as a single unit and acts as a blueprint
for the creation of objects.
Syntax to create Class and objects in Python:
class ClassName:
‘ Optional class documentation string
#list of python class variables
# Python class constructor
#Python class method definitions
# Create an object of the class
object_name = ClassName(argument1, argument2, ...)
Example:
class student:
def display(self): # defining method in class
print("Hello Python")
s1=student() #creating object of class
s1.display() #calling method of class using object
Output:
Hello Python

2. State the use of parameter “self” in python class. (CO5)(4m in ,S-23)


⇨ 1)In python programming self is a default variable that contains the
memory address of the instance of the current class.
2) The self parameter is a reference to the class itself so we can use self to
refer to all the instance variable and instance methods.
3) In a class we can define variables, functions etc. While writing function in
class we have to pass atleast one argument that is called self parameter.
4) class MyClass:
def init(self, value):
self.value = value
def display_value(self):
print("Value:", self.value)

def update_value(self, new_value):


self.value = new_value
print("Value updated to:", self.value)

# Create an object of the class


obj = MyClass(10)

# Call methods on the object


obj.display_value() # Output: Value: 10

# Update the value attribute


obj.update_value(20) # Output: Value updated to: 20

# Call the method again to see the updated value


obj.display_value() # Output: Value: 20

3. Differentiate between method overloading and method overriding.


(CO5)
feature Method Overloading Method Overriding
Redefining a method in the subclass
Having multiple methods in the same class with with the same name and signature as
Definition the same name but different signatures. a method in the superclass.
Can have a different number or type of Must have the same number and
Parameters parameters. types of parameters.
Python does not support method overloading
natively. However, it can be simulated using Inheritance is necessary for method
Implementation default parameter values or variable arguments. overriding.
Decided at compile-time (statically typed
languages) or runtime (dynamically typed
Decision languages) based on the number and types of Decided at runtime based on the
Resolution parameters passed. instance's type.
Useful when you want to create multiple methods Useful when you want to change the
with the same name but varying behavior based on behavior of a method defined in the
Use cases different parameters. superclass in the subclass.
Example ```python ```python
class Calculator: class Animal:
def add(self, a, b): def sound(self):
return a + b print("Animal makes a sound")
def add(self, a, b, c):
return a + b + c class Dog(Animal):
def sound(self):
feature Method Overloading Method Overriding
print("Dog barks")
calc = Calculator()
print(calc.add(2, 3)) # Output: 5 dog = Dog()
print(calc.add(2, 3, 4)) # Output: 9 dog.sound() # Output: "Dog barks"

4. Explain the use _str() and __repr_() function in python. (CO5)


⇨ str():
● This method is called by the str() built-in function and by the print()
function to get a string representation of an object.
● Its primary purpose is to provide a human-readable representation of an
object.
● If str() is not defined for an object, Python will fall back to calling repr().

repr():
● This method is called by the repr() built-in function and by the interpreter
to get an unambiguous string representation of an object.
● Its primary purpose is to provide a representation that could be used to
recreate the object.

● Example:

class Point:
def init(self, x, y):
self.x = x
self.y = y

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

p = Point(1, 2)
print(repr(p)) # Output: Point(1, 2)

5.Explain Destructor in python. (CO5)


=>
● A class can define a special method called destructor with the help of_
_del_ _().
● It is invoked automatically when the instance (object) is about to be
destroyed.
● It is mostly used to clean up non memory resources used by an
instance(object).
Example: For Destructor
class student:
def __init__(self):
print("This is non parameterized constructor")
def __del__(self):
print("Destructor called")
s1=student()
s2=student()
del s1
Output:
This is non parameterized constructor
This is non parameterized constructor
Destructor called

6. Describe Data Hiding.


=> 1) Data hiding in Python refers to the concept of restricting direct access to
certain attributes or methods of a class from outside the class definition.
2) Data hiding is also known as information hiding. An objects attributes may
or may not be visible outside the class definition.
3) We need to name attributes with a double underscore(_ _) prefix and
those attributes the are not directly visible to outsiders. Any variable prefix
with double underscore is called private variable which is accessible only
with class where it is declared.
4) class counter:
__secretcount=0 # private variable
def count(self): # public method
self.__secretcount+=1
print("count= ",self.__secretcount) # accessible in the same class
c1=counter()
c1.count() # invoke method
c1.count()
print("Total count= ",c1.__secretcount) # cannot access private variable directly
Output:
count= 1
count= 2
7.Explain constructor function in python class with example.
=> • Constructors are generally used for instantiating an object.
• The task of constructors is to initialize(assign values) to the data
members of the class when an object of class is created.
• In Python the __init__() method is called the constructor and is always
called when an object is created.
Syntax of constructor declaration :
def __init__(self):
# body of the constructor

Example: For creating constructor use_ _init_ _ method called as


constructor.
class student:
def __init__(self,rollno,name,age):
self.rollno=rollno
self.name=name
print("student object is created")
p1=student(11,"Ajay",20)
print("Roll No of student= ",p1.rollno)
print("Name No of student= ",p1.name)
Output:
student object is created
Roll No of student= 11
Name No of student= Ajay

4 marks
1.Write a program to demonstrate parameterized constructor in base class
and derived class(CO5)
=> Parameterized constructor- Constructor with parameters is known as
parameterized constructor.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.grade = grade

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

# Creating instances of the classes


person1 = Person("John", 25)
student1 = Student("Alice", 18, "A")

# Displaying information
print("Base Class Attribute:")
print("Name in Person class:", person1.name)
print("Age in Person class:", person1.age)

print("\nDerived Class Attributes:")


student1.display_info()

Output:
Base Class Attribute:
Name in Person class: John
Age in Person class: 25

Derived Class Attributes:


Name: Alice, Age: 18, Grade: A

● The Person class represents a basic person with attributes name and age. It
has a parameterized constructor to initialize these attributes.
● The Student class is a subclass of Person and adds an additional attribute
grade. It also has a parameterized constructor that takes name, age, and
grade as parameters, and it calls the constructor of the base class using
super().__init__(name, age).

2.Write a program to demonstrate constructor with default arguments.


=>
class Person:
def __init__(self, name="John", age=30):
self.name = name
self.age = age

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

# Creating instances of the class with different constructor arguments


person1 = Person("Alice", 25)
person2 = Person() # Using default arguments

# Displaying information
print("Person 1:")
person1.display_info()

print("\nPerson 2:")
person2.display_info()

Output:
Person 1:
Name: Alice, Age: 25

Person 2:
Name: John, Age: 30

● The Person class has a constructor (__init__) with default arguments for
name and age. If no arguments are provided when creating an instance,
these default values will be used.
● Two instances are created of the Person class: person1 with custom
arguments and person2 without any arguments.
● When displaying the information, person1 uses the provided arguments,
while person2 uses the default arguments specified in the constructor.

3. Write a program to create a python class and delete object of that class
using del keyword.
=> class MyClass:
def __init__(self, name):
self.name = name

def display(self):
print("Name:", self.name)

# Creating an object of MyClass


obj = MyClass("Example")

# Displaying object's attribute


obj.display()

# Deleting the object using del keyword


del obj

# Attempting to display the attribute after deletion will raise an error


try:
obj.display()
except NameError:
print("The object is deleted.")

4. Explain data abstraction in detail.


=>
● Data abstraction refers to providing only essential information about the
data to the outside world, hiding the background details of implementation.
● In short hiding internal details and showing functionality is known as
abstraction.
● To support encapsulation, declare the methods or variables as private in the
class. The private methods cannot be called by the object directly. It can be
called only from within the class in which they are defined.
● Any function with double underscore is called private method.
Example: For access modifiers with data abstraction
class student:
__a=10 #private variable
b=20 #public variable
def __private_method(self): #private method
print("Private method is called")
def public_method(self): #public method
print("public method is called")
print("a= ",self.__a) #can be accessible in same class
s1=student()
# print("a= ",s1.__a) #generate error
print("b=",s1.b)
# s1.__private_method() #generate error
s1.public_method()
Output:
b= 20
public method is called
a= 10

5. Describe term composition classes with example.


=>
• In composition we do not inherit from the base class but establish
relationship between classes through the use of instance variables that are
references to other objects.
• Composition means that an object knows another object and explicitly
delegates some tasks to it. While inheritance is implicit, composition is
explicit in python.
• We use composition when we want to use some aspects of another class
without promising all of the features of that other class.
Syntax:
Class GenericClass:
Define some attributes and methods
Class AspecificClass:
Instance_variable_of_generic_class=GenericClass
#use this instance somewhere in the class
Some_method(instance_varable_of_generic_class)
• For example, we have three classes email, gmail and yahoo. In email class
we are referring the gmail and using the concept of composition.
Example:
class gmail:
def send_email(self,msg):
print("sending '{}' from gmail".format(msg))
class yahoo:
def send_email(self,msg):
print("sending '{}' from yahoo".format(msg))
class email:
provider=gmail()
def set_provider(self,provider):
self.provider=provider
def send_email(self,msg):
self.provider.send_email(msg)
client1=email()
client1.send_email("Hello")
client1.set_provider(yahoo())
client1.send_email("Hello")
Output:
sending 'Hello' from gmail
sending 'Hello' from yahoo
6 marks Questions:
6.Explain inheritance in python with example. (6m) SQP 70m,S-23
⇨ The mechanism of designing and constructing classes from other classes is
called inheritance.
● The new class is called derived class or child class and the class from which
this derived class has been inherited is the base class or parent class.
● This allows the subclass to reuse the code of the superclass and extend its
functionality.
Simple Inheritance
● Inheritance can be achieved by passing the parent class as an argument in
the class definition of child class.
● Example:
class Parent:
parentname = ""
childname = ""
def show_parent(self):
print(self.parentname)
# Child class created inherits Parent class
class Child(Parent):
def show_child(self):
print(self.childname)
ch1 = Child() # Object of Child class
ch1.parentname = "Arati" # Access Parent class attributes
ch1.childname = "Purva"
ch1.show_parent() # Access Parent class method
ch1.show_child()
Multiple Parent Classes
In multiple inheritance one child class can inherit multiple parent classes.
● Example
Two parent classes Father and Mother extended by the child class
named Son.
# Father class created
class Father:
fathername = ""
def show_father(self):
print(self.fathername)
# Mother class created
class Mother:
mothername = ""
def show_mother(self):
print(self.mothername)
# Son class inherits Father and Mother classes
class Son(Father, Mother):
def show_parent(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
s1 = Son() # Object of Son class
s1.fathername = "Mark"
s1.mothername = "Sonia"
s1.show_parent()

Multilevel Inheritance
In this type of inheritance, a class can inherit from a child class or derived
class.
● Eg: Son class inherited from Father and Mother classes which derived
from Family class.
class Family:
def show_family(self):
print("This is our family:")
# Father class inherited from Family
class Father(Family):
fathername = ""
def show_father(self):
print(self.fathername)
# Mother class inherited from Family
class Mother(Family):
mothername = ""
def show_mother(self):
print(self.mothername)
# Son class inherited from Father and Mother classes
class Son(Father, Mother):
def show_parent(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
s1 = Son() # Object of Son class
s1.fathername = "Ashish"
s1.mothername = "Sonia"
s1.show_family()
s1.show_parent()

Call Parent Class Constructor from Child Class


In this type of inheritance parent class attributes initialized using child class
constructor.
● Example
Inside the Father class constructor, the Family class constructor is called
and
arguments for the Family class constructor are passed to it.
class Family:
# Parent class constructor
def __init__(self, name):
self.name = name
# Father class inherited from Family
class Father(Family):
# Child class constructor
def __init__(self, name, age):
# Parent class constructor called from child class
Family.__init__(self, name)
self.age = age
f = Father("Ashish", 36)
print(f.name)
print(f.age)

7. Create a class employee with data members: name, department and


salary. Create suitable methods for reading and printing employee
information. (6m) SQP 70m
⇨ class Employee:
def __init__(self, name, department, salary):
self.name = name
self.department = department
self.salary = salary

def read_employee_info(self):
self.name = input("Enter employee name: ")
self.department = input("Enter employee department: ")
self.salary = float(input("Enter employee salary: "))

def print_employee_info(self):
print("Employee Name:", self.name)
print("Department:", self.department)
print("Salary:", self.salary)

# Example usage:
if __name__ == "__main__":
# Creating an object of Employee
emp = Employee("", "", 0.0)

# Reading employee information


emp.read_employee_info()

# Printing employee information


print("\nEmployee Information:")
emp.print_employee_info()
output:
Enter employee name: John Doe
Enter employee department: Sales
Enter employee salary: 50000

Employee Information:
Employee Name: John Doe
Department: Sales
Salary: 50000.0

Past year Question Paper’s question / Extra questions


1.Illustrate the use of method overriding? Explain with example. (4m)
SQP 70m
⇨ Method overriding is an ability of a class to change the implementation of
a method provided by one of its base class.
If a class inherits a method from its superclass, then there is a chance to
override the method provided.
You can always override your parent class methods.
class Parent:
def echo(self):
print('I am from Parent class')
class Child (Parent):
def echo(self):
print('I am from Child class')
p = Parent()
c = Child()
p.echo()
c.echo()
Output:
I am from Parent class
I am from Child class

2.WAP to create base classes namely add,mul having method addition and
mutiplication that prints addition and multiplication respectively. Derive a
class derived from add and mul that has method divide and returns
division. Create object and call methods. 4m
class Add:
def addition(self,a,b):
self.r=a+b
print("Addition",self.r)
class Mul:
def multiplication(self,a,b):
self.r=a*b
print("multiplication",self.r)
class Div(Add,Mul):
def division(self,a,b):
self.r=a/b
print("division",self.r)
d=Div()
d.addition(20,10)
d.multiplication(20,10)
d.division(20,10)

Output:
Addition 30
multiplication 200
division 2.0

3.WAP to implement mutilevel inheritance 4m(pyqb)


class Add:
def addition(self,a,b):
self.r=a+b
print("Addition",self.r)
class Mul(Add):
def multiplication(self,a,b):
self.r=a*b
print("multiplication",self.r)
class Div(Mul):
def division(self,a,b):
self.r=a/b
print("division",self.r)
d=Div()
d.addition(20,10)
d.multiplication(20,10)
d.division(20,10)
Output:
Addition 30
multiplication 200
division 2.0

4. Illustrate with example method over loading. 4 M(W-22)


=> • Method overloading is the ability to define the method with the same name
but with a different number of arguments and data types.
• With this ability one method can perform different tasks, depending on the
number of arguments or the types of the arguments given.
• Method overloading is a concept in which a method in a class performs
operations according to the parameters passed to it.
Example: With a method to perform different operations using method
overloading.
class operation:
def add(self,a,b):
return a+b
op1=operation()
# To add two integer numbers
print("Addition of integer numbers=",op1.add(10,20))
# To add two floting point numbers
print("Addition of integer numbers=",op1.add(11.12,12.13))
# To add two strings
print("Addition of integer numbers=",op1.add("Hello","Python"))
Output:
Addition of integer numbers= 30
Addition of integer numbers= 23.25
Addition of integer numbers= HelloPython
Python does not support method overloading, that is, it is not possible to define
more than one method with the same name in a class in Python.
• This is because method arguments in python do not have a type. A method
accepting one argument can be called with an integer value, a string or a double
as shown in next
example.
class Demo:
def method(self, a):
print(a)
obj= Demo()
obj.method(50)
obj.method('Meenakshi')
obj.method(100.2)
Output:
50
Meenakshi
100.2

5. Design a class student with data members; Name, roll number address.
Create suitable method for reading and printing students details.
6m(W-22,S-23)
⇨ class Student:
def getStudentDetails(self):
self.rollno=input("Enter Roll Number : ")
self.name = input("Enter Name : ")
self.address =input("Enter Address : ")
def printStudentDetails(self):
print(self.rollno,self.name, self.address)
S1=Student()
S1.getStudentDetails()
print("Student Details ")
S1.printStudentDetails ()

Output:
Enter Roll Number : 001
Enter Name : ABC
Enter Address : New York
Student Details :
01 C New York
6. Create a parent class named Animals and a child class Herbivorous
which will extend the class Animal. In the child class Herbivorous over
side the method feed ( ). Create a object. 6 M(W-22)
⇨ # parent class
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# function breath
def breathe(self):
print("I breathe oxygen.")
# function feed
def feed(self):
print("I eat food.")
# child class
class Herbivorous(Animal):
# function feed
def feed(self):
print("I eat only plants. I am vegetarian.")
herbi = Herbivorous()
herbi.feed()
# calling some other function
herbi.breathe()

Output:
I eat only plants. I am vegetarian.
I breathe oxygen.
7. With neat example explain default constructor concept in python.
2m(S-23)
⇨ Default constructor- The default constructor is simple constructor which
does not accept any arguments. Its definition has only one argument which
is a reference to the instance being constructed.

class MyClass:
def __init__(self):
print("Default constructor called.")

# Creating an instance of MyClass


obj = MyClass()
Output:
Default constructor called.
In this example, the default constructor __init__ is called automatically when an
instance of the MyClass class is created. The output confirms that the default
constructor is indeed invoked.

You might also like