Constructors and destructors are special member functions in C++ that are automatically invoked during the lifecycle of an object. They play an important role in object creation and memory management in object-oriented programming.
- They help manage the lifecycle of objects.
- Widely used for initialization and cleanup operations in C++.
Constructor
A constructor is a special member function that is automatically called when an object of a class is created. It is mainly used to initialize data members of the class.
- Constructor name must be the same as the class name.
- It does not have any return type.
- It is automatically invoked during object creation.
#include <iostream>
using namespace std;
class Student{
public:
// Constructor
Student() {
cout << "Constructor Called" << endl;
}
};
int main(){
Student s1;
return 0;
}
Output
Constructor Called
Explanation: In the above program, the constructor Student() is automatically called when the object s1 is created. The constructor prints a message showing that the object has been initialized successfully.
Destructor
A destructor is a special member function that is automatically called when an object goes out of scope or is destroyed. It is mainly used to free memory and release resources.
- Destructor name is same as class name preceded by ~.
- It does not take parameters or return any value.
- It is automatically called before object destruction.
#include <iostream>
using namespace std;
class Student{
public:
// Constructor
Student() {
cout << "Object Created" << endl;
}
// Destructor
~Student() {
cout << "Destructor Called" << endl;
}
};
int main() {
Student s1;
return 0;
}
Output
Object Created Destructor Called
Explanation: In above code, the constructor is called when the object s1 is created, and the destructor is automatically called when the program execution ends and the object goes out of scope. The destructor performs cleanup operations before the object is removed from memory.
Note: If we do not specify any access modifiers for the members inside the class then by default the access modifier for the members will be Private.
Constructor Vs Destructor
| Constructor | Destructor |
|---|---|
| Constructor helps to initialize the object of a class. | Whereas destructor is used to destroy the instances. |
| It is declared as className( arguments if any ){Constructor's Body }. | Whereas it is declared as ~ className( no arguments ){ }. |
| Constructor can either accept arguments or not. | While it can't have any arguments. |
| A constructor is called when an instance or object of a class is created. | It is called while object of the class is freed or deleted. |
| Constructor is used to allocate the memory to an instance or object. | While it is used to deallocate the memory of an object of a class. |
| Constructor can be overloaded. | While it can't be overloaded. |
| The constructor's name is same as the class name. | Here, its name is also same as the class name preceded by the tiled (~) operator. |
| In a class, there can be multiple constructors. | While in a class, there is always a single destructor. |
| There is a concept of copy constructor which is used to initialize an object from another object. | While here, there is no copy destructor concept. |
| They are often called in successive order. | They are often called in reverse order of constructor. |