Python OOPs Concepts 7

Parameterized Constructor

Parameterized Constructor is a special function which if present automatically gets executed when an object is declared with arguments.

The data elements of the object are initialized by the arguments passed at the time of object declaration

Example:
s=student(101,”Amit”,98.99)

Example:1
Python program to initialize the data elements of the class student using parameterized constructor and display them?

#class creation
class student:
    def __init__(self,r,n,p):
        self.roll=r
        self.name=n
        self.per=p
    def show(self):
        print("Roll no ",self.roll)
        print("name ",self.name)
        print("Per ",self.per)

#calling
#parameterized
s=student(1001,"Mohan",99.99)
s.show()

Output:

Roll no  1001
name  Mohan
Per  99.99
>>> 

Example:2
Python program to initialize the data elements of the class employee using parameterized constructor and display them?

#class creation
class employee:
    def __init__(self,e,n,s):
        self.empno=e
        self.name=n
        self.sal=s
    def show(self):
        print("Empno ",self.empno)
        print("Name ",self.name)
        print("Salary ",self.sal)

#calling
#parameterized
e=employee(5001,"Kapil",9800)
e.show()

Output:

Empno  5001
Name  Kapil
Salary  9800
>>> 

Visitor Counter

0183259