example program class and object part 2 and constructors program

Published: 19 February 2020
on channel: The Light
56
1

class emp:
##constructor
def __init__(self,name,id,sal):
self.id=id
self.name=name
self.sal=sal
defining the method
def display(self):
print("empid:%d\n ename:%s\nsalary:%f"%(self.id,self.name,self.sal))
e1=emp("ravi",10,1500)
e2=emp("sita",20,2500)
e1.display()
e2.display()
"""output:
empid:10
ename:ravi
salary:1500.000000
empid:20
ename:sita
salary:2500.000000"""
It is a special method, it is called automatically as soon as the object is created. Constructor are used to initialise the data into the object.
No need to call the constructors, once object the object is created, it is called automatically.
There are two types of constructors:
1. Parameterised constructor
2. Non-parameterised constructor
Parameterised constructor: if atleast one parameter is there is called parameterised constructor.
Ex:
def __init__(self,id,name,sal):
….
Ex: E1=emp(10,’sita’,1500)
Non-parameterised constructor:
If there is no parameter in the constructor then it is called non-parameterised constructor. Except ‘self’ keyword.
Ex:
def __init__(self):
….
e.display()


On this page of the site you can watch the video online example program class and object part 2 and constructors program with a duration of hours minute second in good quality, which was uploaded by the user The Light 19 February 2020, share the link with friends and acquaintances, this video has already been watched 56 times on youtube and it was liked by 1 viewers. Enjoy your viewing!