Constructors and Destructors in C++
Constructors and destructors are special member functions of a class that handle the creation and destruction of objects automatically. They make object initialization clean and memory management reliable.
What is a Constructor?
A constructor is a special function that is automatically called when an object is created. It is used to initialize data members. A constructor:
- Has the same name as the class.
- Has no return type (not even
void). - Is called automatically — no need to call it manually.
Default Constructor
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
int age;
Student() { // default constructor
name = "Unknown";
age = 0;
cout << "Student object created." << endl;
}
};
int main() {
Student s1; // constructor called automatically
cout << s1.name << " - " << s1.age << endl;
return 0;
}
Output:
Student object created.
Unknown - 0Parameterized Constructor
A constructor can accept arguments to initialize members with specific values:
class Student {
public:
string name;
int age;
Student(string n, int a) {
name = n;
age = a;
}
void show() {
cout << name << " (Age: " << age << ")" << endl;
}
};
int main() {
Student s1("Anita", 20);
Student s2("Dev", 22);
s1.show();
s2.show();
return 0;
}
Output:
Anita (Age: 20)
Dev (Age: 22)Constructor Initialization List
A more efficient way to initialize members — especially for const or reference members:
class Box {
public:
int length, width;
Box(int l, int w) : length(l), width(w) {} // initialization list
void show() {
cout << length << " x " << width << endl;
}
};
int main() {
Box b(5, 3);
b.show(); // Output: 5 x 3
return 0;
}
Copy Constructor
A copy constructor creates a new object as a copy of an existing object:
class Point {
public:
int x, y;
Point(int x, int y) : x(x), y(y) {}
Point(const Point &p) { // copy constructor
x = p.x;
y = p.y;
cout << "Copy created." << endl;
}
};
int main() {
Point p1(3, 4);
Point p2 = p1; // copy constructor called
cout << p2.x << ", " << p2.y << endl;
return 0;
}
Output:
Copy created.
3, 4Constructor Overloading
A class can have multiple constructors with different parameter lists (same name, different signatures):
class Circle {
public:
double radius;
Circle() { radius = 1.0; } // default
Circle(double r) { radius = r; } // parameterized
};
int main() {
Circle c1; // radius = 1.0
Circle c2(5.5); // radius = 5.5
cout << c1.radius << " " << c2.radius << endl;
return 0;
}
Output:
1 5.5What is a Destructor?
A destructor is a special function that is automatically called when an object goes out of scope or is deleted. It is used to release resources (like memory, file handles, connections). A destructor:
- Has the same name as the class, preceded by a tilde
~. - Has no return type and no parameters.
- Cannot be overloaded.
class Resource {
public:
Resource() {
cout << "Resource acquired." << endl;
}
~Resource() {
cout << "Resource released." << endl;
}
};
int main() {
Resource r; // constructor called
cout << "Using resource..." << endl;
// destructor called automatically when main() ends
return 0;
}
Output:
Resource acquired.
Using resource...
Resource released.Constructor vs Destructor
| Feature | Constructor | Destructor |
|---|---|---|
| Called when | Object is created | Object is destroyed |
| Parameters | Can have parameters | Cannot have parameters |
| Overloading | Allowed | Not allowed |
| Name | Same as class | Tilde + class name (~ClassName) |
Key Takeaways
- Constructors initialize objects automatically when they are created.
- Parameterized constructors allow flexible object creation with specific values.
- The copy constructor creates a new object from an existing one.
- Constructor overloading allows multiple ways to create an object.
- Destructors clean up resources automatically when objects are destroyed.
