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

Assignment Constructor

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment Constructor

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DAR ES SALAM MARITIME INSTITUTE (DMI)

DEPARTMENT: MARINE ENGINEERING

COURSE NAME: MECHATRONIC ENGINEERING

MODULE NAME: DATA STRUCTURE AND OBJECT-ORIENTED


PROGRAMING

MODULE CODE: MEU 07675

LECTURE NAME: MR A. MAKANTA

STUDENT NAME: ABDUL-HALIM HAFIDH MOH’D

REGISTRATION NO: BMTE/21/004

TASK GIVEN: INDIVIDUAL ASSIGNMENT

1
CONSTRUCTOR:
A constructor is a member function of a class that has the same name as the class
name. It helps to initialize the object of a class. It can either accept the arguments
or not. It is used to allocate the memory to an object of the class. It is called
whenever an instance of the class is created. It can be defined manually with
arguments or without arguments. There can be many constructors in a class. It can
be overloaded but it cannot be inherited or virtual. There is a concept of copy
constructor which is used to initialize an object from another object.
Syntax:

DESTRUCTOR:
Like a constructor, Destructor is also a member function of a class that has the
same name as the class name preceded by a tilde (~) operator. It helps to deallocate
the memory of an object. It is called while the object of the class is freed or
deleted. In a class, there is always a single destructor without any parameters so it
can’t be overloaded. It is always called in the reverse order of the constructor. if a
class is inherited by another class and both the classes have a destructor then the
destructor of the child class is called first, followed by the destructor of the parent
or base class.
Syntax:

If we do not specify any access modifiers for the members inside the class then by
default the access modifier for the members will be Private.
Example/Implementation of Constructor and Destructor:

2
C++

OUTPUT:

3
NOW USING PYTHON LANGUAGE.

In Python, object initialization is typically done using a constructor method


`__init__ () ` and finalization or cleanup is handled using a destructor method
`__del__ ()`.

Here is an example to demonstrate object constructor and destructor in Python:

In the example above:

- The `__init__()` method is the constructor used for initializing the object. It takes
at least one argument (`self`) which refers to the instance of the class being created,
along with any other arguments needed for initialization.

- The `__del__()` method is the destructor used for finalizing or cleaning up the
object. It does not need to be explicitly called as Python's garbage collector
automatically takes care of memory management.

When you run the code, you will see the OUTPUT:

4
DIFFERENCE BETWEEN CONSTRUCTOR AND DESTRUCTOR :

S.
No. Constructor Destructor

Constructor helps to initialize the Whereas destructor is used to


1.
object of a class. destroy the instances.

It is declared as className (arguments Whereas it is declared as ~


2.
if any) {Constructor’s Body }. className (no arguments) { }.

Constructor can either accept While it can’t have any


3.
arguments or not. arguments.

A constructor is called when an It is called while object of the


4.
instance or object of a class is created. class is freed or deleted.

While it is used to deallocate


Constructor is used to allocate the
5. the memory of an object of a
memory to an instance or object.
class.

5
S.
No. Constructor Destructor

6. Constructor can be overloaded. While it can’t be overloaded.

Here, its name is also same as


The constructor’s name is same as the
7. the class name preceded by the
class name.
tiled (~) operator.

In a class, there can be multiple While in a class, there is always


8.
constructors. a single destructor.

There is a concept of copy constructor


While here, there is no copy
9. which is used to initialize an object
destructor concept.
from another object.

They are often called in successive They are often called in reverse
10.
order. order of constructor.

ADVANTAGES OF USING DESTRUCTORS IN PYTHON:

• Automatic cleanup: Destructors provide automatic cleanup of resources


used by an object when it is no longer needed. This can be especially useful
in cases where resources are limited, or where failure to clean up can lead to
memory leaks or other issues.

• Consistent behaviour: Destructors ensure that an object is properly cleaned


up, regardless of how it is used or when it is destroyed. This helps to ensure
consistent behaviour and can help to prevent bugs and other issues.

6
• Easy to use: Destructors are easy to implement in Python, and can be
defined using the __del__ () method.

• Supports object-oriented programming: Destructors are an important


feature of object-oriented programming, and can be used to enforce
encapsulation and other principles of object-oriented design.

• Helps with debugging: Destructors can be useful for debugging, as they can
be used to trace the lifecycle of an object and determine when it is being
destroyed.

Destructors are an important feature of Python and can help to ensure that objects
are properly cleaned up and resources are not wasted. They are easy to use and can
be useful for enforcing encapsulation and other principles of object-oriented design

You might also like