Constructors and Destructors in C++ — Complete Tutorial
Last Updated on: 11th Dec 2025 19:16:37 PM
In Object Oriented Programming (OOP), constructors and destructors are special member functions used to create and destroy objects. They play a crucial role in initialization, memory management, resource handling, and cleanup.
This tutorial explains every concept in a simple with real-life examples.
What is a Constructor in C++?
A constructor is a special member function of a class that is automatically executed whenever an object of that class is created.
Its main purpose is to initialize the object.
Key Rules
-
Constructor name is same as the class name
-
It has no return type, not even
void -
Executed automatically when object is created
-
Used to set initial values of data members
Why Constructors Are Important?
Because they:
-
Initialize variables
-
Allocate memory
-
Open files or connections
-
Set default values
-
Prepare an object to work
Real-Life Example of a Constructor
Imagine you enter a hotel room:
-
Lights turn on
-
AC starts
-
TV power is enabled
You didn’t manually do anything — the system does it automatically when you enter.
That automatic setup = Constructor.
Types of Constructors in C++
C++ provides several types of constructors:
-
Default Constructor
-
Parameterized Constructor
-
Copy Constructor
-
Dynamic Constructor
-
Constructor Overloading
We will cover each in detail.
Default Constructor
A constructor that takes no arguments is called a default constructor.
Example
#include <iostream>
using namespace std;
class Student {
public:
Student() {
cout << "Default constructor called!" << endl;
}
};
int main() {
Student s1; // Constructor automatically called
}
Parameterized Constructor
A constructor that accepts parameters to initialize object data is called a parameterized constructor.
Real-Life Example
When buying a mobile phone, you select:
-
Model
-
RAM
-
Storage
-
Color
These inputs initialize your phone's configuration → parameterized constructor.
Example
class Student {
public:
string name;
int age;
Student(string n, int a) {
name = n;
age = a;
}
};
Copy Constructor
A constructor that creates a new object by copying data of an existing object.
Real-Life Example
Photocopying a document — the new paper is an exact copy.
Syntax
ClassName (const ClassName &oldObj);
Example
class Demo {
public:
int x;
Demo(int val) {
x = val;
}
// Copy constructor
Demo(const Demo &obj) {
x = obj.x;
}
};
Dynamic Constructor
A constructor that allocates memory dynamically using new keyword.
Example
class Sample {
int *ptr;
public:
Sample(int x) {
ptr = new int;
*ptr = x;
}
};
Constructor Overloading
Having multiple constructors with different parameter lists in the same class.
Example
class Demo {
public:
Demo() { }
Demo(int x) { }
Demo(int x, int y) { }
};
What is a Destructor in C++?
A destructor is a special member function that is automatically called when an object is destroyed.
It is used to release memory and clean up resources.
Key Rules
-
Always named with a tilde (~) before class name
-
Cannot be overloaded
-
Cannot take parameters
-
Cannot return a value
-
Automatically called when the object goes out of scope
Why Destructors Are Important?
Because they:
-
Free memory
-
Close files
-
Release network connections
-
Destroy temporary objects
-
Prevent memory leaks
Real-Life Example of a Destructor
When you check out of a hotel room:
-
Lights turn off
-
AC stops
-
Services are stopped
-
Room is cleaned
This cleanup process = Destructor.
Syntax of Destructor
~ClassName() {
// cleanup code
}
Example: Constructor + Destructor in One Program
#include <iostream>
using namespace std;
class Demo {
public:
Demo() {
cout << "Constructor called!" << endl;
}
~Demo() {
cout << "Destructor called!" << endl;
}
};
int main() {
Demo obj; // Constructor called here
return 0; // Destructor called automatically here
}
Output
Constructor called!
Destructor called!
Order of Constructor & Destructor Call
For a single object:
-
Constructor → first
-
Destructor → last
For multiple objects:
Constructors execute in creation order,
Destructors execute in reverse order.
Advanced Concept
Using Constructor to Initialize Private Variables
class Account {
private:
int balance;
public:
Account(int b) {
balance = b;
}
};
Constructor with Initialization List (Modern C++)
class Demo {
int a, b;
public:
Demo(int x, int y) : a(x), b(y) { }
};
Destructor in Dynamic Memory Cleanup (Very Important)
class Test {
int *ptr;
public:
Test(int x) {
ptr = new int(x);
}
~Test() {
delete ptr;
}
};
Without destructor → memory leak
Major Differences: Constructor vs Destructor
| Feature | Constructor | Destructor |
|---|---|---|
| Purpose | Initialize object | Destroy object + cleanup |
| Name | Same as class | ~ClassName |
| Parameters | Allowed | Not allowed |
| Return type | No return type | No return type |
| Overloading | Allowed | Not allowed |
| Auto called | Yes, during creation | Yes, during destruction |
Diagram – How Constructor and Destructor Work
Object Lifecycle:
------------------------------------------
| Object Created → Constructor Called |
| |
| Object Used |
| |
| Object Destroyed → Destructor Called |
------------------------------------------
Real-Life Use Cases in Software
| Application | Constructor Use | Destructor Use |
|---|---|---|
| Banking App | Load account details | Save & close session |
| Game Development | Load textures, sounds | Release memory |
| Database App | Connect to DB | Close connection |
| File Handling | Open file | Close file |
| Web Browser | Load tabs | Clear history cache |
Summary
-
Constructors initialize the object
-
Destructors clean up the object
-
Constructors can be overloaded
-
Destructors cannot be overloaded
-
Copy, default, parameterized constructors are widely used
-
Very important for dynamic memory management
Keep practicing — you're doing amazing!
Happy Coding! ![]()