In Object-Oriented Programming, managing the lifecycle of an object—from the moment it is created in memory to the moment it is destroyed—is critical. C++ provides two special member functions for this purpose: Constructors and Destructors.
Theoretically, constructors and destructors ensure that an object is always in a "valid state."
The Constructor: Its job is Initialization. It sets the initial values of data members and acquires resources (like opening a file or allocating memory) the moment an object is born.
The Destructor: Its job is Cleanup. It ensures that when an object's lifetime ends, it releases any resources it held, preventing memory leaks or locked files.
They have the same name as the class.
They do not have a return type (not even void).
They are called automatically by the compiler.
Constructors can be overloaded; Destructors cannot.
A constructor that takes no arguments. If you do not write any constructor, the C++ compiler generates a "compiler-defined default constructor" for you. However, it often leaves variables with garbage values.
A constructor that accepts arguments to initialize an object with specific values. This allows you to create different objects with different starting data.
A special constructor used to create a new object as a copy of an existing object.
Theory: It is essential when passing objects by value to functions. By default, C++ does a "shallow copy," but if your object uses dynamic memory (pointers), you must write your own Copy Constructor to perform a "deep copy."
The destructor is identified by the tilde symbol (~) followed by the class name (e.g., ~Student()).
The theory of Deterministic Destruction is what makes C++ unique compared to languages like Java or Python.
Local Objects: Destroyed as soon as the code execution leaves the block { } where they were defined.
Global Objects: Destroyed when the program terminates.
Dynamic Objects: Destroyed only when you explicitly call delete.
This is the most important theoretical concept in C++ resource management. RAII means that the holding of a resource is tied to the object's lifetime.
The Constructor acquires the resource (memory, file, network socket).
The Destructor automatically releases it.
This ensures that even if an error occurs or the program exits early, the destructor will run and clean up the mess.
#include <iostream> #include <string> using namespace std; class Wall { private: double length; public: // 1. Parameterized Constructor Wall(double len) { length = len; cout << "Constructor: Creating a wall of length " << length << endl; } // 2. Destructor ~Wall() { cout << "Destructor: Cleaning up wall resources." << endl; } double getLength() { return length; } }; int main() { cout << "--- Main Starts ---" << endl; { // Creating an object inside a local scope Wall w1(10.5); cout << "Wall length: " << w1.getLength() << endl; // w1 will be destroyed here when the scope ends } cout << "--- Main Ends ---" << endl; return 0; }
| Feature | Constructor | Destructor |
| Purpose | Initializes the object. | Cleans up after the object. |
| Syntax | ClassName() | ~ClassName() |
| Call Timing | When object is created. | When object goes out of scope. |
| Arguments | Can take arguments (Overloading). | Cannot take arguments. |
| Return Type | None. | None. |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION