PWP Assignment 5
PWP Assignment 5
<statemnentN>
With neat example explain default constructor concept in Python.
The default constructor is simple constructor which does not accept any arguments. It's
definition has only one argument which is a reference to the instance being constructed.
Example 1: Display Hello message using default constructor.
class Student:
sl = Student()
sl.show("Student1")
Output:
This is non parametrized constructor
Hello Student1
Define Data Hiding concept? Write two advantages of Data Hiding.
ns Data hiding is a concept which underlines the hiding of data or information from the
user.
# properties of class A
class B(A):
# class B inheriting property of class A
# more properties of class B
Page
Example:
# Base class
class Animal:
def speak(self):
print("Animal speaks")
dog = Dog("Buddy")
cat = Cat("Whiskers")
def_init_(self):
self.name = ""
self.roll no = ""
self.department ="n
self.mobile_no =
def read_student_info(self):
self.name = input("Enter student name: ")
def print_student_info(self):
print("Student Information:")
print("Name:", self.name)
print("RollNumber:", self.roll_no)
print("Department:", self.department)
print("Mobile Number:", self.mnobile_no)
student = Student()
Page
student.read_student_info()
student.print_student_info)
output:
Enter student name: raj
Enter roll number: 11
Student Information:
Name: raj
Roll Number: 11
Department: computer
Mobile Number: 123456
d) Explain method overloading and overriding in python.
Ans Method Overloading: 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. 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
d) Write a program to create class EMPLOYEE with ID and NAME
and display its contents.
class employee:
id=0
name=n
e= employee()
e.getdata(11, "Vijay")
e.showdata()
Output:
ID:11
Name : Vijay
c) Explain use of format() method with example
The format() method formats the specified value(s) and insert
them inside the string's placeholder.
The placeholder is defined using curly brackets: ().
The format() method returns the formatted string.
Syntax
string.format (valuel, value2...)
Example:
#named indexes:
>>>txt1 = ("My name is {fname), I'm {age'".format(fname =
"abc", age =36))
>>>print(txt1)
My name is abc, I'm 36
#numbered indexes:
>>>txt2 =( "My name is {0}, I'm {1}".format("xyz",36))
>>>print(txt2)
My name is xyz, I'm 36
#empty placeholders:
>>>txt3 = ("My name is {}, I'm {".format("pgr",36))
>>>print(txt3)
My name is pgr, I'm 36