3-4
3-4
Programming
Dr. Bharat Singh
PhD, CSE
• Introduction
• Defining a class
• Fields Declaration
• Methods declaration
• Creating Objects
• Accessing Class Members
• Constructors
• Self argument
• Static Members
What is Object Oriented
Programming?
• Object-oriented programming (OOP) is a programming
paradigm based on the concept of "objects".
• The object contains both data and code: Data in the form of
properties (often known as attributes), and code, in the form
of methods (actions object can perform).
What is Object Oriented
Programming ?
• class class_name:
• '''This is a docstring. I have created a new class'''
• <statement 1>
• <statement 2> class ClassName:
• . initializer
• .
methods
• <statement N>
Example: Define a class in Python
• In this example, we are creating a Person Class with name, sex, and
profession instance variables.
class Person:
def __init__(self, name, sex, profession):
# data members (instance variables)
self.name = name
self.sex = sex
self.profession = profession
# A sample method
def fun(self):
print("I am a", self.attr1)
print("I am a", self.attr2)
• <object-name> = <class-name>(<arguments>)
• for example
• jessa = Person('Jessa', 'Female', 'Software Engineer')
# Python program to demonstrate instantiating a class
class Person:
# A simple class attribute
attr1 = "smart boy"
attr2 = "smart girl"
# A sample method
def fun(self):
print("I am a", self.attr1)
print("I am a", self.attr2)
def greet(self):
print("hope you are doing well")
# Driver code
# Object instantiation
Rodger = Person()
# instance Method
def show(self):
print('Hello, my name is', self.name)
# call methods
jessa.show()
jessa.work()
Constructor
• A constructor is a special method used to create and initialize an object of a class.
• Constructors are used to initializing the object’s state.
• This method is defined in the class.
• The constructor is executed automatically at the time of object creation.
• The primary use of a constructor is to declare and initialize data member/ instance
variables of a class. The constructor contains a collection of statements (i.e., instructions)
that executes at the time of object creation to initialize the attributes of an object.
• For example, when we execute obj = ClassName(), Python gets to know that obj
is an object of ClassName and calls the constructor of that class to create an
object.
• On the other hand, a destructor is used to destroy the object.
Constructor
• In Python, Object creation is divided into two parts in Object Creation
and Object initialization
# Sample Method
def say_hi(self):
print('Hello, my name is', self.name)
p = Person('Nikhil')
p.say_hi()
Constructor
• Note:
• For every object, the constructor will be executed only once. For
example, if we create four objects, the constructor is called four
times.
• In Python, every class has a constructor, but it’s not required to define
it explicitly. Defining constructors in class is optional.
• Python will provide a default constructor if no constructor is defined.
Types of Constructors
• In Python, we have the following
three types of constructors.
1. Default Constructor
2. Non-parametrized constructor
3. Parameterized constructor
Default Constructor
• Python will provide a default constructor if no constructor
is defined. Python adds a default constructor when we do
not include the constructor in the class or forget to
declare it. It does not perform any task but initializes the class Employee:
objects. It is an empty constructor without a body.
def display(self):
• If you do not implement any constructor in your class or print('Inside Display')
forget to declare it, the Python inserts a default
constructor into your code on your behalf. This emp = Employee()
constructor is known as the default constructor. emp.display()