0% found this document useful (0 votes)
20 views

Lab # 03 Introduction To Object Oriented Programming (Methods and Constructors) Objective

This document discusses methods and constructors in object-oriented programming in Python. It defines constructors as special methods used to initialize class attributes when an object is created. There are two types of constructors: parameterized and non-parameterized. Methods are functions defined within a class that receive the class instance automatically. The document provides examples of defining non-parameterized and parameterized constructors, as well as methods, in Python classes. It concludes with lab tasks for students to write programs using methods and constructors.

Uploaded by

Ali Asghar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lab # 03 Introduction To Object Oriented Programming (Methods and Constructors) Objective

This document discusses methods and constructors in object-oriented programming in Python. It defines constructors as special methods used to initialize class attributes when an object is created. There are two types of constructors: parameterized and non-parameterized. Methods are functions defined within a class that receive the class instance automatically. The document provides examples of defining non-parameterized and parameterized constructors, as well as methods, in Python classes. It concludes with lab tasks for students to write programs using methods and constructors.

Uploaded by

Ali Asghar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY

Hamdard Institute of Engineering & Technology


Hamdard University
Lab # 03

Introduction to Object Oriented Programming


(Methods and Constructors)

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.

Constructors can be of two types.

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.

Creating the constructor in python

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

Consider the following example to initialize the Employee class attributes.

Non-Parameterized Constructor Example

The default constructor which is also known as non-parameterized constructor is simple


constructor which doesn’t accept any arguments. Its definition has only one argument which is a
reference to the instance being constructed.

Example # 1

Output:

Parameterized Constructor Example


Constructor with parameters is known as parameterized constructor. The parameterized constructor
take its first argument as a reference to the instance being constructed known as self and the rest of the
arguments are provided by the programmer.

Page | 2
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University

Example #2

Output:

Methods and Attributes in a Python Class

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy