Constructors &
Destructors
Learn how C++ objects are born and destroyed: the special member functions that initialize and clean up every object you create.
What Is an Object's Lifecycle?
Every C++ object goes through a predictable lifecycle: it is created, it is used, and it is destroyed. Two special member functions control the bookends of this lifecycle.
What Is a Constructor?
A constructor is a special member function that is automatically called when an object of a class is instantiated. It has the same name as the class and no return type: not even void.
What Is a Destructor?
A destructor is a special member function called automatically when an object is destroyed. It has the same name as the class, prefixed with a tilde (\(\sim\)). Like constructors, it has no return type.
Why Do We Need Them?
Types of Constructors
Constructor Syntax Rules
void, not int: literally nothing before the name.new'd, or copy-initialized.private, external code cannot create objects of that class (used in the Singleton pattern).Destructor Syntax Rules
~ClassName().void, no int.delete is called on a heap object.Constructor vs Destructor
| Feature | Constructor | Destructor |
|---|---|---|
| Name | \(\text{ClassName}()\) | \(\sim\text{ClassName}()\) |
| Parameters | Zero or more | None |
| Overloadable? | Yes | No |
| Count per class | Multiple | Exactly one |
| Called when | Object is created | Object is destroyed |
| Purpose | Initialize / allocate | Clean up / deallocate |
Step-by-Step: Writing a Class
Example 1: Basic Class
Example 2: Parameterized Constructor
Example 3: Dynamic Memory
Order of Construction & Destruction
Common Mistakes
void Player() turns it into a regular function, not a constructor. No return type at all.new in the constructor but not writing a destructor with delete causes memory leaks.Summary at a Glance
| Concept | Details |
|---|---|
| Constructor syntax | \(\text{ClassName}(\text{params})\) |
| Destructor syntax | \(\sim\text{ClassName}()\) |
| Default constructor | No parameters, compiler-generated if you write none |
| Parameterized | Takes arguments for custom initialization |
| Copy constructor | \(\text{ClassName}(\text{const ClassName}\&)\) |
| Destruction order | Reverse of construction (LIFO) |
| When to write destructor | When the class manages dynamic resources |
Question 1 of 5
Which of the following is true about a constructor in C++?
void). Constructors can be overloaded, so a class may have multiple. Option C describes a destructor.Question 2 of 5
What is the correct syntax for a destructor of a class named Engine?
~Engine(). Option A wrongly adds a return type, and options B and D are invalid syntax.Question 3 of 5
If three local objects A, B, and C are created in that order inside a function, in what order are their destructors called when the function ends?
Question 4 of 5
A class uses new in its constructor to allocate a dynamic array. What happens if you do not write a destructor with the corresponding delete[]?
delete[], that memory is leaked. You must write a destructor to pair every new with a delete.Question 5 of 5
You define a parameterized constructor Box(double w, double h) but no default constructor. What happens when you write Box b;?
Box b; calls the default constructor and none exists, you get a compilation error. To fix this, explicitly write a default constructor or add default parameter values.