In the previous chapter, we learned about classes and objects.
When an object is created, sometimes we want to give it some starting values automatically. C++ provides constructors for this.
A destructor is used when an object is removed from memory.
Let’s understand C++ Constructor and Destructor with simple examples.
What is a C++ Constructor?
A constructor is a special function that runs automatically when an object is created.
A constructor:
- Has the same name as the class.
- Does not have a return type.
- Runs automatically.
Simple Example
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
Student() {
name = "Rahul";
}
};
int main() {
Student student;
cout << student.name;
return 0;
}
Output:
Rahul
When this line runs:
Student student;
the Student() constructor runs automatically.
Parameterized Constructor
A constructor can also receive values.
This is called a parameterized constructor.
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
int age;
Student(string n, int a) {
name = n;
age = a;
}
};
int main() {
Student student("Rahul", 14);
cout << "Name: " << student.name << "\n";
cout << "Age: " << student.age;
return 0;
}
Output:
Name: Rahul
Age: 14
Here:
Student student("Rahul", 14);
passes "Rahul" and 14 to the constructor.
The constructor then stores these values in the object.
Creating Multiple Objects
A parameterized constructor makes it easy to create objects with different values.
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
int age;
Student(string n, int a) {
name = n;
age = a;
}
};
int main() {
Student student1("Rahul", 14);
Student student2("Anjali", 15);
cout << student1.name << " - " << student1.age << "\n";
cout << student2.name << " - " << student2.age;
return 0;
}
Output:
Rahul - 14
Anjali - 15
Each object gets its own values.
Constructor Overloading
A class can have more than one constructor, as long as they have different parameters.
This is called constructor overloading.
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
int age;
Student() {
name = "Unknown";
age = 0;
}
Student(string n, int a) {
name = n;
age = a;
}
};
int main() {
Student student1;
Student student2("Rahul", 14);
cout << student1.name << " - " << student1.age << "\n";
cout << student2.name << " - " << student2.age;
return 0;
}
Output:
Unknown - 0
Rahul - 14
Here, C++ chooses the correct constructor based on how the object is created.
What is a C++ Destructor?
A destructor is a special function that runs automatically when an object is destroyed.
A destructor:
- Has the same name as the class.
- Starts with
~. - Does not have a return type.
- Does not take parameters.
Simple Example
#include <iostream>
using namespace std;
class Student {
public:
Student() {
cout << "Object created\n";
}
~Student() {
cout << "Object destroyed";
}
};
int main() {
Student student;
return 0;
}
Output:
Object created
Object destroyed
The constructor runs when the object is created.
The destructor runs when the object is destroyed.
Constructor vs Destructor
| Constructor | Destructor |
|---|---|
| Runs when an object is created | Runs when an object is destroyed |
| Same name as the class | Same name as the class with ~ |
| Used to initialize an object | Used for cleanup |
| Can have parameters | Cannot have parameters |
For example:
Student();
~Student();
A Simple Example
Let’s create a class that shows both the constructor and destructor.
#include <iostream>
using namespace std;
class Demo {
public:
Demo() {
cout << "Constructor called\n";
}
~Demo() {
cout << "Destructor called\n";
}
};
int main() {
Demo obj;
cout << "Program is running\n";
return 0;
}
Output:
Constructor called
Program is running
Destructor called
The order is:
Object created
↓
Constructor runs
↓
Program uses object
↓
Object is destroyed
↓
Destructor runs
Key Points to Remember
- A constructor runs automatically when an object is created.
- A constructor has the same name as the class.
- A constructor does not have a return type.
- A parameterized constructor can receive values.
- A class can have multiple constructors.
- A destructor runs when an object is destroyed.
- A destructor starts with
~. - A destructor does not take parameters.
What’s Next?
In the next chapter, we will learn about C++ Inheritance and see how one class can use the features of another class.
Frequently Asked Questions (FAQs)
Q1. What is a C++ constructor?
A constructor is a special function that runs automatically when an object is created and is commonly used to initialize its data.
Q2. What is a C++ destructor?
A destructor is a special function that runs automatically when an object is destroyed and is used for cleanup.
Q3. Can a C++ constructor have parameters?
Yes. A parameterized constructor in C++ can accept values when an object is created.
Q4. What is constructor overloading in C++?
Constructor overloading means creating multiple constructors in the same class with different parameter lists.
Q5. What is the difference between a constructor and destructor in C++?
A constructor runs when an object is created, while a destructor runs when the object is destroyed.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
