Lab # 03 Introduction To Object Oriented Programming (Methods and Constructors) Objective
Lab # 03 Introduction To Object Oriented Programming (Methods and Constructors) Objective
Objective
To get introduced with methods and constructors.
By applying the different methods and constructors.
Theory
Constructor
A constructor is a special type of method (function) which is used to initialize the instance
members of the class.
1. Parameterized Constructor
2. Non-parameterized Constructor
Constructor definition is executed when we create the object of this class. Constructors also
verify that there are enough resources for the object to perform any start-up task.
In python, the method __init__ simulates the constructor of the class. This method is called
when the class is instantiated. We can pass any number of arguments at the time of creating the
class object, depending upon __init__ definition. It is mostly used to initialize the class
attributes. Every class must have a constructor, even if it simply relies on the default constructor.
Page | 1
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Example # 1
Output:
Page | 2
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Example #2
Output:
Now creating a class is incomplete without some functionality. So functionalities can be defined
by setting various attributes which acts as a container for data and functions related to those
attributes. Functions in python are also called as Methods. Talking about the init method, it is a
special function which gets called whenever a new object of that class is instantiated. You can
think of it as initialize method or you can consider this as constructors if you’re coming from any
Page | 3
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
another object-oriented programming background such as C++, Java etc. Now when we set a
method inside a class, they receive instance automatically.
Example # 3
Output:
Page | 4
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Lab Task
3.1 Write a program to find out the GCD by using methods.
3.2 Write a program to reverse a string word by word by using parameterized constructor.
3.3 write a program to find ASCII Value of a Character by using parameterized constructor.
3.4 write a program to find the maximum numbers between 10 numbers by using methods.
NOTE: Attach printouts of above mentioned task with your name and roll number in header or
footer.
Learning outcomes:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Page | 5