CHAPTER 16
Beginner
Constructors and Destructors in C++
Updated: May 17, 2026
5 min read
# CHAPTER 16
Constructors and Destructors
1. Introduction
When you create a variable likeint x;, it contains garbage memory until you assign it a value. The same applies to Objects. To ensure an object is ready to be used the exact moment it is created, C++ uses Constructors. To clean up an object right before it is destroyed, C++ uses Destructors.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define a Default Constructor.
- Use Parameterized Constructors for custom initialization.
- Understand Constructor Overloading.
- Explain the Copy Constructor.
- Define a Destructor for memory cleanup.
3. The Constructor
A constructor is a special member function that is automatically called when an object is instantiated.- It MUST have the exact same name as the class.
-
It does NOT have a return type (not even
void).
1. Default Constructor (Takes no arguments):
cpp
2. Parameterized Constructor: Allows you to pass arguments when creating the object to set custom values.
cpp
4. Constructor Overloading
Just like regular functions, constructors can be overloaded. You can have multiple constructors in a class, and C++ will call the correct one based on the arguments provided.
cpp
5. The Copy Constructor
A copy constructor is used to create a new object as a copy of an existing object. C++ provides a default copy constructor, but you can define your own.
cpp
6. The Destructor
A destructor is a special function called automatically right before an object is destroyed (e.g., when it goes out of scope or isdeleted from the Heap).
-
It has the same name as the class, preceded by a tilde (
~).
- No return type, and no parameters (meaning it cannot be overloaded).
- Vital for freeing dynamically allocated memory!
cpp
7. Member Initializer Lists
A more efficient way to initialize class members, especially constants or references, is the Initializer List.
cpp
8. Memory-Level Explanation
WhenPlayer p1; is executed, the OS allocates memory on the Stack for p1. Immediately after allocation, the CPU executes the Constructor's instructions to populate that memory. When main() ends, the CPU executes the Destructor's instructions *before* the OS reclaims the Stack memory.
9. Common Mistakes
-
Putting a return type on a constructor:
void Player()is NOT a constructor; it's a regular function named Player.
-
Forgetting the Destructor when using
new: If your class usesnewto allocate heap memory, you MUST have a destructor that callsdelete, otherwise every object you create will leak memory!
10. Exercises
-
1.
Create a
Carclass with a constructor that takesmakeandyear. Create an object and print its details.
-
2.
Add a Destructor to the
Carclass that simply prints "Car destroyed" so you can observe when it runs.
11. MCQ Quiz with Answers
Question 1
What is the primary purpose of a Constructor?
Question 2
What is the return type of a Constructor?
Question 3
Which character precedes the name of a Destructor?
Question 5
When is a Destructor called?
Question 6
What does Player p2 = p1; invoke?
Question 7
If a class uses new to allocate memory for a pointer member, where must delete be called to prevent memory leaks?
Question 8
What happens if you don't write a constructor for your class?
Question 9
Which is the most efficient way to initialize members in a constructor?
p1.Player();)?
a) Yes b) No
Answer: b) No (Constructors are called automatically upon object creation)
12. Interview Questions
- Q: Explain the Rule of Three in C++. (Answer: If a class requires a custom Destructor, Copy Constructor, or Copy Assignment Operator, it almost certainly requires all three to manage dynamic memory correctly).
- Q: What is the difference between shallow copy and deep copy? (Hint: Related to the Copy Constructor).
- Q: Why use an initializer list instead of assigning values inside the constructor body?