KEMBAR78
CE144 OOPC Unit-06 | PDF
0% found this document useful (0 votes)
48 views42 pages

CE144 OOPC Unit-06

The document discusses different types of constructors in C++ including default, parameterized, copy, and multiple constructors. It also covers constructor overloading, shallow vs deep copy, and constructors with default arguments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views42 pages

CE144 OOPC Unit-06

The document discusses different types of constructors in C++ including default, parameterized, copy, and multiple constructors. It also covers constructor overloading, shallow vs deep copy, and constructors with default arguments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

CE144

OBJECT ORIENTED PROGRAMMING


WITH C++

UNIT-6
Constructors and Destructor

N. A. Shaikh
nishatshaikh.it@charusat.ac.in
Topics to be covered
Introduction to Constructors
Parameterized Constructors
 Multiple Constructors in class
Constructors with default argument
Dynamic initialization of Constructors & objects
Copy Constructor
Dynamic Constructor
Destructors

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


2
Introduction
 One of the aims of C++ is to create user-defined data
types such as class, that behave very similar to the built-
in types.
 This means that we should be able to initialize a class
type variable (object) when it is declared, much the
same way as initialization of an ordinary variable.
 Similarly, when a variable of built-in type goes out of
scope, the compiler automatically destroys the
variable.
 But it has not happened with the objects we have so far
studied.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


3
Introduction
 It is therefore clear that some more features of classes
need to be explored that would enable us to initialize
the objects when they are created and destroy them
when their presence is no longer necessary.
 C++ provides a special member function called the
constructor which enables an object to initialize itself
when it is created. This is known as automatic
initialization of objects.
 It also provides another member function called the
destructor that destroys the objects when they are no
longer required.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


4
Constructors
 A constructor is a special member function of a class
which initializes objects of a class.
 It is special because its name is the same as the class
name.
 Constructor is automatically called/invoked when
object(instance of class) is created.
 It is called constructor because it constructs the values
of data members of the class.
 It has no return type, so can’t use return keyword.
 It must be an instance member function, that is, it can
never be static.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


5
Characteristics of Constructor
 They should be declared in the public section.
 They do not have return type, not even void therefore, they
return nothing.
 They can not be inherited though derived class can call the
base class constructor.
 Like other C++ functions, they can have default arguments.
 They can not be virtual.
 We can not refer to their address.
 An object with a constructor (or destructor) can not be used
as a member of a union.
 They make ‘implicit calls’ to the operators new and delete
when memory allocation is required.
NOTE: When a constructor is declared for a class initialization
of the class, objects becomes mandatory.
Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh
6
Types of Constructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


7
Default Constructor
 Default constructor is the constructor which doesn’t
take any argument. It has no parameters

Note:
 Even if we do not define any constructor explicitly, the
compiler will automatically provide a default
constructor (expects no parameters and has an empty
body) implicitly

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


8
Default Constructor

 Above statement not only creates the object but also


initializes its data members m and n to zero.
 There is no need to write any statement to invoke the
constructor as we do with the normal member function.
Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh
9
Default Constructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


10
Parameterized Constructor
 Parameterized constructor is the constructor which can
take argument(s).
 It is used to initialize the various data elements of
different objects with different values when they are
created.
 It is used to overload constructors.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


11
Parameterized Constructor
 When an object is declared in a parameterized
constructor, the initial values have to be passed as
arguments to the constructor function.

This can be done in two ways:


1. By calling the constructor explicitly

2. By calling the constructor implicitly

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


12
Parameterized Constructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


13
Parameterized Constructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


14
Copy Constructor
 The parameters of a constructor can be of any type except
that of the class to which it belongs.

 However, a constructor can accept a reference to its own


class as a parameter

 In such cases, the constructor is called the copy


constructor.
Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh
15
Copy Constructor
 A copy constructor is a member function which
initializes an object using another object of the same
class.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


16
Copy Constructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


17
Copy Constructor
NOTE:
 A reference variable has been used as an argument to
the copy constructor.
 We can not pass the argument by value to a copy
constructor.
 When no copy constructor is defined , the compiler
supplies its own copy constructor.

Two types of copies are produced by the constructor:


1. Shallow copy
2. Deep copy

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


18
Shallow Copy
 The default copy constructor can only produce the
shallow copy.

 A Shallow copy is defined as the process of creating the


copy of an object by copying data of all the member
variables as it is.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


19
Shallow Copy

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


20
Deep copy
 Deep copy is possible only with user defined copy
constructor.

 In user defined copy constructor, we make sure that


pointers (or references) of copied object point to new
memory locations.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


21
Deep copy

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


22
Let’s Practice

1. Copy Constructor VS Assignment Operator

2. Shallow Copy VS Deep Copy

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


23
Multiple Constructor in a Class
 The process of sharing the same name by two or more
functions is referred to as function overloading.

 Similarly, when more than one constructor function is


defined in a class, we say that the constructor is
overloaded.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


24
Multiple Constructor in a Class

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


25
Constructors with Default Arguments
 It is possible to define constructors with default arguments
NOTE:
 It is important to distinguish between the default
constructor A::A() and the default argument constructor
A::A(int = 0).
 The default argument constructor can be called with either
one argument or no arguments.
 When called with no arguments, it becomes a default
constructor.
 When both these forms are used in a class, it causes
ambiguity for a statement such as
A a;
 The ambiguity is whether to 'call' A::A() or A::A(int= 0).
Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh
26
Constructors with Default Arguments

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


27
Dynamic Initialization of Objects
 Class objects can be initialized dynamically too.

 The initial value of an object may be provided during


runtime.

Advantage:
 We can provide various initialization formats, using
overloaded constructors.

 Provides the flexibility of using different format of data


at runtime depending upon the situation.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


28
Dynamic Initialization of Objects

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


29
Dynamic constructor
 Dynamic constructor is used to allocate the memory to
the objects at the run time.

 Memory is allocated at run time with the help of 'new'


operator.

 By using this constructor, we can dynamically initialize


the objects.

 Thus, object is going to use memory region, which is


dynamically created by constructor.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


30
Dynamic constructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


31
const objects
 We may create and use constant objects using const
keyword before object declaration.

 Any attempt to modify the values of m and n will


generate compile-time error.
 Further, a constant object can call only const member
functions.
 As we know, a const member is a function prototype or
function definition where the keyword const appears
after the function's signature.
 Whenever const objects try to invoke non-const
member functions, the compiler generates errors.
Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh
32
Destructor
 A destructor is used to destroy the objects that have
been created by a constructor
 Like constructor, the destructor is a member function
with the same name as the class name but is preceded
by a tilde

 A destructor never takes any argument nor does it


return any value
 It will be invoked implicitly by the compiler on exit
from the program or block or function as the case may
be to clean up storage that is no longer accessible

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


33
Destructor
 It is a good practice to declare destructors in a program
since it releases memory space for further use
 Only one destructor is written to destroy all the
constructors
 Whenever new is used to allocate memory in the
constructor, we should use delete to free that memory
 The primary use of destructors is to free up the
memory reserved by an object before it gets destroyed
 Destructor will be called in reverse order of the
constructor creation

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


34
Destructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


35
Destructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


36
Destructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


37
Practical 23: Constructor and Destructor
Write a C++ program having class time with data members: hr,
min and sec.
Define following member functions.
1) getdata() to enter hour, minute and second values
2) putdata() to print the time in the format 11:59:59
3) default constructor
4) parameterized constructor
5) copy constructor
6) Destructor.
Use 52 as default value for sec in parameterized constructor.
Use the concepts of default constructor, parameterized
constructor, Copy constructor, constructor with default
arguments and destructor.

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


38
Practical 23: Constructor and Destructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


39
Practical 23: Constructor and Destructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


40
Practical 23: Constructor and Destructor

Unit 6: Constructor and Destructors Prepared By: Nishat Shaikh


41
End of Unit-6

You might also like