5th chap question python
5th chap question python
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)
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}")
# Displaying information
print("Base Class Attribute:")
print("Name in Person class:", person1.name)
print("Age in Person class:", person1.age)
Output:
Base Class Attribute:
Name in Person class: John
Age in Person class: 25
● 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).
def display_info(self):
print(f"Name: {self.name}, Age: {self.age}")
# 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)
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()
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)
Employee Information:
Employee Name: John Doe
Department: Sales
Salary: 50000.0
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
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.")