You are on page 1of 10

CONSTRUCTORS

AND DESTRUCTORS
NAME REEWA DASTGIR

AG# 2019-ag-2826

COURSE CODE CS-625 (OOP)


DATE 14-4-2020
CONSTRUCTORS
A constructor resembles an instance method, but it differs from a method
in that it has no explicit return type, it is not implicitly inherited and it
usually has different rules for scope modifiers. Constructors often have
the same name as the declaring class. They have the task of initializing
the object's data members and of establishing the invariant of the class,
failing if the invariant is invalid. A properly written constructor leaves the
resulting object in a valid state. Immutable objects must be initialized in
a constructor.
EXAMPLE OF CONSTRUCTOR
DEFAULT CONSTRUCTOR

A default constructor is a constructor which can be called with no


arguments (either defined with an empty parameter list, or with default
arguments provided for every parameter). A type with a public default
constructor is DefaultConstructible.
Syntax:
EXAMPLE
CONSTRUCTOR OVERLOADING

• Constructor overloading is a concept of having more than one


constructor with different parameters list, in such a way so that each
constructor performs a different task. For e.g. Vector class has 4 types
of constructors.
EXAMPLE
DESTRUCTOR

• In object-oriented programming, a destructor gives an object a


last chance to clean up any memory it allocated or perform any other
tasks that must be completed before the object is destroyed. Like
constructors, destructors are defined as subroutines in the class
definition.

You might also like