What are Constructors and Destructors in C++?
Learn what constructors and destructors are in C++, how they initialize and clean up objects, and how they power RAII, with clear code examples.
Expected Interview Answer
A constructor is a special member function that runs automatically when an object is created to initialize its state, and a destructor is a special member function that runs automatically when the object is destroyed to release its resources.
A constructor shares the class's name, has no return type, and can be overloaded to accept different arguments (default, parameterized, copy). A destructor has the same name prefixed with a tilde, takes no arguments, cannot be overloaded, and there is exactly one per class. Together they implement RAII (Resource Acquisition Is Initialization): the constructor acquires resources such as memory, files, or locks, and the destructor guarantees their cleanup when the object goes out of scope.
- Automatic initialization guarantees objects start in a valid state
- Deterministic cleanup prevents resource leaks (RAII)
- Constructors can be overloaded for flexible object creation
- No manual init/cleanup calls needed by the caller
- Exception-safe resource management
AI Mentor Explanation
A constructor is like the pre-match setup for a player walking out to bat: pads strapped, gloves on, guard taken, box in place — everything is prepared before the first ball. The destructor is the walk back to the pavilion after being dismissed, where the kit is unstrapped and returned to the bag. Every batter is automatically kitted up on arrival and stripped down on departure, with no step skipped.
Step-by-Step Explanation
Step 1
Define the constructor
Declare a function with the same name as the class and no return type; use an initializer list to set members.
Step 2
Overload as needed
Provide default, parameterized, and copy constructors to support different ways of creating objects.
Step 3
Acquire resources
Inside the constructor, allocate memory, open files, or take locks the object will own (RAII acquisition).
Step 4
Define the destructor
Declare ~ClassName() with no parameters and no return type; there is exactly one per class.
Step 5
Release resources
In the destructor, free memory, close files, and release locks so nothing leaks when the object is destroyed.
What Interviewer Expects
- Constructor initializes, destructor cleans up
- Constructor naming rules and no return type
- Destructor uses tilde, takes no args, cannot be overloaded
- Understanding of RAII
- Awareness of constructor overloading (default, parameterized, copy)
Common Mistakes
- Giving a constructor or destructor a return type
- Thinking destructors can be overloaded
- Forgetting to release resources in the destructor (leaks)
- Not making the destructor virtual in a base class with polymorphism
- Assigning members in the body instead of using an initializer list
Best Answer (HR Friendly)
“A constructor is the setup code that runs automatically when an object is made, getting it into a valid starting state. A destructor is the cleanup code that runs automatically when the object is destroyed, releasing whatever the object was holding.”
Code Example
#include <iostream>
#include <string>
class FileBuffer {
std::string name;
int* data;
public:
// Parameterized constructor: acquires resources
FileBuffer(const std::string& n, int size)
: name(n), data(new int[size]) {
std::cout << "Opened " << name << "\n";
}
// Destructor: releases resources
~FileBuffer() {
delete[] data;
std::cout << "Closed " << name << "\n";
}
};
int main() {
FileBuffer fb("log.txt", 100); // constructor runs here
return 0; // destructor runs at scope exit
}Follow-up Questions
- What is RAII and how do constructors and destructors implement it?
- Why should a base class destructor be virtual?
- What is the difference between a default and a parameterized constructor?
- In what order are members and base classes constructed and destroyed?
- What is a member initializer list and why is it preferred?
MCQ Practice
1. Which statement about destructors is correct?
A destructor takes no parameters, has no return type, cannot be overloaded, and there is exactly one per class.
2. When does a constructor execute?
A constructor runs automatically at the moment an object is created to initialize its state.
3. What does RAII rely on?
RAII ties a resource's lifetime to an object: the constructor acquires it and the destructor releases it deterministically.
Flash Cards
What is a constructor? — A special member function with the class's name and no return type that runs automatically to initialize an object when it is created.
What is a destructor? — A special member function named ~ClassName with no parameters and no return type that runs automatically to clean up when an object is destroyed.
Can destructors be overloaded? — No. There is exactly one destructor per class; it takes no arguments and cannot be overloaded.
What is RAII? — Resource Acquisition Is Initialization: acquire resources in the constructor and release them in the destructor so cleanup is automatic and leak-free.