Constructors and Destructors
Constructors and Destructors
(24ECE2101)
Teaching Team
1
Constructors and Destructors
Constructor
Characteristics of Constructors in C++
4
Declaration of Constructor
Define the Constructors within the class
6
Define the Constructors outside the
class
7
Identify the name of the constructor
Answer: Wall
10
More facts about constructors
11
Types of constructors
• Dummy Constructors
• Default Constructors
• Parameterized Constructors
• Copy Constructor
• Dynamic Constructor
12
Dummy Constructor (Do Nothing
Constructor)
13
Implementation of Dummy constructor
14
Default Constructor
16
Implementation of Default constructor
17
Parameterized Constructor
18
Implementation of Parameterized Constructor
The program code given here uses a parameterized constructor to initialize the data member of
the class.
Implementation of Parameterized Constructor
20
Copy Constructor
Copy Constructor
23
Implementation of Copy Constructor
24
Dynamic Constructor
Answer: a
27
Quiz Time
28
Quiz Time
a. Runtime error
b. Preprocessing error
c. Runtime exception
d. Compile-time error
Answer: d
30
Quiz Time
Answer: a
Explanation: This is mandatory to pass the object by reference. Otherwise,
the object will try to create another object to copy its values, in turn a
constructor will be called, and this will keep on calling itself. This will cause
the compiler to give out of memory error.
31
Quiz Time
33
Quiz Time
How many times is a constructor called when an array of objects is created?
34
Quiz Time
35
Quiz Time
36
Thanks
1/31/2025 37