Table of Contents
ToggleConstructors and Destructors
Constructors and destructors are important features of object-oriented programming in C++. 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 the data members of an object. A destructor is another special member function that is automatically called when an object is destroyed. It is mainly used to perform cleanup operations and release resources.
In this complete tutorial, you will learn about constructors and destructors in C++, their syntax, types, practical examples, constructor overloading, copy constructors, dynamic memory, the difference between constructors and destructors, common mistakes, FAQs, and C++ interview questions.
Table of Contents
- What is a Constructor in C++?
- Syntax of a Constructor
- Example of Constructor
- How Does a Constructor Work?
- Types of Constructors in C++
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Constructor with Default Arguments
- Constructor Overloading
- What is a Destructor in C++?
- Syntax of Destructor
- Example of Destructor
- How Does a Destructor Work?
- Destructor and Dynamic Memory
- Constructors and Destructors in Inheritance
- Constructor vs Destructor
- Advantages of Constructors
- Advantages of Destructors
- Common Errors
- Practical C++ Example
- Frequently Asked Questions
- C++ Interview Questions
- C++ Practice Questions
- Conclusion
What is a Constructor in C++?
A constructor in C++ is a special member function of a class that is automatically executed when an object is created. Its primary purpose is to initialize the object’s data members. A constructor has the same name as the class and does not have a return type, including void.
For example:
#include <iostream>
using namespace std;
class Student {
public:
Student() {
cout << "Constructor is called." << endl;
}
};
int main() {
Student s1;
return 0;
}
Output
Constructor is called.
In this example, Student() is the constructor. When the object s1 is created, the constructor is automatically called.
Syntax of a Constructor
The general syntax of a constructor is:
class ClassName {
public:
ClassName() {
// initialization code
}
};
A constructor can also receive parameters:
class ClassName {
public:
ClassName(parameter1, parameter2) {
// initialization code
}
};
Important characteristics of constructors
- A constructor has the same name as the class.
- It does not have a return type.
- It is called automatically when an object is created.
- Constructors can accept parameters.
- Constructors can be overloaded.
- A class can have multiple constructors.
- Constructors are mainly used for object initialization.
Example of Constructor in C++
 Example: Question: Write a C++ program to demonstrate a default constructor that initializes a student’s age to 18 and displays it
#include <iostream>
using namespace std;
class Student {
int age;
public:
Student() {
age = 18;
}
void display() {
cout << "Age = " << age << endl;
}
};
int main() {
Student s1;
s1.display();
return 0;
}
Output: age = 18 :Â Here, the constructor initializes the age variable to 18.
How Does a Constructor Work?
When an object is created, C++ automatically calls the appropriate constructor.For example:
Student s1;
The above statement creates an object named s1. As soon as the object is created, the constructor of the Student class is executed. The general sequence is:
Class Definition
↓
Object Creation
↓
Constructor Called
↓
Data Members Initialized
↓
Object Used
This automatic initialization makes constructors very useful in object-oriented programming.
Types of Constructors in C++
There are several commonly used types of constructors in C++:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
Constructor with Default Arguments
Constructors can also be overloaded to provide different ways of creating and initializing objects.
1. Default Constructor in C++
A default constructor is a constructor that does not require arguments.Example:
#include <iostream>
using namespace std;
class Student {
public:
Student() {
cout << "Default constructor called." << endl;
}
};
int main() {
Student s1;
return 0;
}
Output:Â Default constructor called. The constructor Student() does not accept any parameters, so it is called a default constructor.
Example with Data Members
#include <iostream> using namespace std; class Student { string name; int age; public: Student() { name = "Basant"; age = 18; } void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main() { Student s1; s1.display(); return 0; }OutputName: Basant Age: 18
2. Parameterized Constructor in C++:
A parameterized constructor is a constructor that accepts one or more arguments.It allows different objects to be initialized with different values.
Example
#include <iostream>
using namespace std;
class Student {
string name;
int age;
public:
Student(string n, int a) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main() {
Student s1("Basant", 20);
Student s2("Ram", 19);
s1.display();
s2.display();
return 0;
}
Output
Name: Basant
Age: 20
Name: Ram
Age: 19
The parameterized constructor allows each object to store different initial values.
3. Copy Constructor in C++
A copy constructor creates a new object by copying the values of an existing object of the same class.The common syntax is:
ClassName(const ClassName &object) {
// copy data
}
Example
#include <iostream>
using namespace std;
class Student { Â Â string name;
int age;
public:
Student(string n, int a) {
name = n;
age = a;
}
Student(const Student &s) {
name = s.name;
age = s.age;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main() {
Student s1("Basant", 20);
Student s2(s1);
s1.display();
s2.display();
return 0;
}
The object s2 is initialized using the existing object s1.
4. Constructor with Default Arguments
A constructor can have default values for its parameters.Example:
#include <iostream>
using namespace std;
class Student {
string name;
int age;
public:
Student(string n = "Unknown", int a = 0) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main() {
Student s1;
Student s2("Basant", 20);
s1.display();
s2.display();
return 0;
}
The first object uses the default values, while the second object uses the values supplied by the programmer.
Constructor Overloading in C++
Constructor overloading means defining multiple constructors in the same class with different parameter lists.Example:
#include <iostream>
using namespace std;
class Student {
string name;
int age;
public:
Student() {
name = "Unknown";
age = 0;
}
Student(string n) {
name = n;
age = 0;
}
Student(string n, int a) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main() {
Student s1;
Student s2("Basant");
Student s3("Ram", 20);
s1.display();
s2.display();
s3.display();
return 0;
}
Here, the class contains three constructors with different parameter lists.
Advantages of Constructor Overloading
- Provides multiple ways to initialize objects.
- Makes classes more flexible.
- Improves code readability.
- Allows objects to be created with different amounts of information.
- Supports different initialization requirements.
What is a Destructor in C++?
A destructor in C++ is a special member function that is automatically called when an object is destroyed.A destructor is mainly used for cleanup operations, such as releasing dynamically allocated memory or other resources owned by an object.
A destructor has the same name as the class but is preceded by the tilde symbol ~. For example:
class Student {
public:
~Student() {
cout << "Destructor called.";
}
};
Here, ~Student() is the destructor.
Syntax of Destructor in C++
The general syntax is:
class ClassName {
public:
~ClassName() {
// cleanup code
}
};
 Features of A destructor:
- Has the same name as the class.
- Begins with the
~symbol. - Does not have a return type.
- Does not accept parameters.
- Cannot normally be overloaded.
- Is called automatically when an object’s lifetime ends.
- Is commonly used for cleanup and resource management.
Example of Destructor in C++
#include <iostream>
using namespace std;
class Student {
public:
Student() {
cout << "Constructor called." << endl;
}
~Student() {
cout << "Destructor called." << endl;
}
};
int main() {
Student s1;
cout << "Object is being used." << endl;
return 0;
}
Output
Constructor called.
Object is being used.
Destructor called.
The constructor is called when s1 is created, while the destructor is called automatically when s1 is destroyed.
How Does a Destructor Work?
The lifetime of an object can be represented as:
Object Created
↓
Constructor Called
↓
Object Used
↓
Object Lifetime Ends
↓
Destructor Called
↓
Resources Released
For a local object, the destructor is generally called when the object goes out of scope.For an object created using new, the destructor is normally invoked when the object is destroyed using delete.
Destructor and Dynamic Memory
Dynamic memory is memory allocated during program execution.In C++, dynamic memory can be allocated using new and released using delete.Example:
#include <iostream>
using namespace std;
class Number {
int *value;
public:
Number() {
value = new int;
*value = 100;
cout << "Memory allocated." << endl;
}
~Number() {
delete value;
cout << "Memory released." << endl;
}
void display() {
cout << "Value = " << *value << endl;
}
};
int main() {
Number n;
n.display();
return 0;
}
Output
Memory allocated.
Value = 100
Memory released.
The destructor releases the dynamically allocated memory.In modern C++, resource management is often better handled with RAII and smart pointers such as std::unique_ptr and std::shared_ptr, because they reduce the risk of memory leaks and ownership errors.
Constructors and Destructors in Inheritance
Constructors and destructors are also important when inheritance is used.Consider:
#include <iostream>
using namespace std;
class Parent {
public:
Parent() {
cout << "Parent constructor" << endl;
}
~Parent() {
cout << "Parent destructor" << endl;
}
};
class Child : public Parent {
public:
Child() {
cout << "Child constructor" << endl;
}
~Child() {
cout << "Child destructor" << endl;
}
};
int main() {
Child obj;
return 0;
}
The constructors are generally executed from the base class toward the derived class, while destruction happens in the reverse order. Typical output:
Parent constructor
Child constructor
Child destructor
Parent destructor
This order is important when designing classes that manage resources.
Constructor vs Destructor
The constructor and destructor perform opposite roles in an object’s lifetime.
| Feature | Constructor | Destructor |
|---|---|---|
| Purpose | Initializes an object | Cleans up an object |
| Name | Same as class | Same as class with ~ |
| Symbol | No special symbol | ~ |
| Called | Object creation | Object destruction |
| Parameters | Can accept parameters | Does not accept parameters |
| Return type | None | None |
| Overloading | Can be overloaded | Cannot be overloaded |
| Main use | Initialization | Cleanup |
| Number | Multiple constructors possible | One destructor per class |
| Dynamic memory | Can allocate resources | Can release resources |
Difference Between Constructor and Destructor
The major difference is that a constructor is responsible for initializing an object, while a destructor is responsible for cleaning up resources when the object’s lifetime ends.
A constructor is automatically called when an object is created. A destructor is automatically called when an object is destroyed. Constructors can have parameters and can be overloaded, whereas destructors cannot have parameters and cannot be overloaded.
Advantages of Constructors
Constructors provide several advantages in C++:
- Automatic initialization – Objects can be initialized automatically when they are created.
- Code simplicity – Initialization code can be placed inside the class instead of being repeated throughout the program.
- Constructor overloading – Objects can be initialized in different ways.
- Improved readability – Object creation and initialization become easier to understand.
- Better object-oriented design – Constructors help establish a valid initial state for an object.
- Resource initialization – Constructors can acquire resources that an object needs.
Advantages of Destructors
Destructors are useful for managing the end of an object’s lifetime.
- Automatic cleanup – The destructor is called automatically when an object’s lifetime ends.
- Memory management – Dynamically allocated resources can be released.
- Resource management – Files, connections, locks, and other resources can be cleaned up.
- Prevents resource leaks – Proper cleanup helps avoid resources remaining allocated unnecessarily.
- Supports RAII – Destructors are a fundamental part of C++ resource-management techniques.
- Improves program reliability – Proper cleanup makes programs safer and easier to maintain.
Common Errors in Constructors and Destructors
Beginners often make several mistakes when working with constructors and destructors.
1. Giving a Constructor a Return Type : Incorrect:
void Student() {
}
A constructor should not have a return type.: Correct:
Student() {
}
2. Giving a Destructor Parameters
A destructor cannot take parameters. :Incorrect:
~Student(int x) {
}
Correct:
~Student() {
}
3. Using the Wrong Destructor Name
The destructor must use the class name. For:
class Student
the destructor should be:
~Student()
4. Forgetting to Release Manually Managed Resources
If a class manually owns dynamically allocated memory, appropriate cleanup is required. Modern C++ should generally prefer RAII and smart pointers where practical.
5. Creating Unnecessary Manual Memory Management
Beginners sometimes use new and delete everywhere. Modern C++ generally provides safer alternatives such as containers and smart pointers.
Practical C++ Example: Student Class
The following example demonstrates both a constructor and destructor:
#include <iostream>
using namespace std;
class Student {
string name;
int age;
public:
Student(string n, int a) {
name = n;
age = a;
cout << "Constructor called." << endl;
}
void display() {
cout << "Student Name: " << name << endl;
cout << "Student Age: " << age << endl;
}
~Student() {
cout << "Destructor called." << endl;
}
};
int main() {
Student student1("Basant", 20);
student1.display();
return 0;
}
Output
Constructor called.
Student Name: Basant
Student Age: 20
Destructor called.
This example demonstrates the complete lifecycle of a simple object.
Real-Life Example of Constructor and Destructor
Consider a banking application. When a BankAccount object is created, its constructor can initialize:
- Account number
- Account holder name
- Initial balance
When the object is destroyed, its destructor can perform necessary cleanup for resources owned by the object.Similarly, in a file-processing application, an object may acquire a file-related resource during initialization and ensure that the resource is properly released when the object is destroyed.This approach helps programmers manage resources according to the object’s lifetime.
Constructor and Destructor Execution Order
Suppose three objects are created:
Student s1;
Student s2;
Student s3;
Their constructors are called in creation order:
s1 constructor
s2 constructor
s3 constructor
When their lifetime ends, their destructors are generally called in reverse order:
s3 destructor
s2 destructor
s1 destructor
This is commonly described as LIFO (Last In, First Out) behavior for objects with the same scope.
Why Are Constructors and Destructors Important in C++?
Constructors and destructors help manage the complete lifecycle of an object. A constructor establishes the object’s initial state, while a destructor handles cleanup when the object’s lifetime ends.They are particularly important in programs that use:
- Classes and objects
- Dynamic memory
- File handling
- Database connections
- Network resources
- Locks and synchronization
- Resource management
- Object-oriented programming
Understanding constructors and destructors is therefore essential for students learning C++ OOP.
Frequently Asked Questions
What is a constructor in C++?
A constructor is a special member function that is automatically called when an object is created. It is mainly used to initialize the object.
What is a destructor in C++?
A destructor is a special member function that is automatically called when an object is destroyed. It is mainly used for cleanup and resource management.
Can a constructor be overloaded?
Yes. A class can have multiple constructors with different parameter lists.
Can a destructor be overloaded?
No. A class can have only one destructor because a destructor cannot have parameters.
Can a constructor return a value?
No. A constructor does not have a return type.
Can a destructor have parameters?
No. A destructor cannot accept parameters.
When is a destructor called?
A destructor is called automatically when an object’s lifetime ends. For dynamically allocated objects, it is normally invoked when the object is destroyed using delete.
What is a copy constructor?
A copy constructor initializes a new object using an existing object of the same class.
What is constructor overloading?
Constructor overloading means defining multiple constructors in the same class with different parameter lists.
Why are destructors important?
Destructors help perform cleanup and release resources when an object’s lifetime ends.
C++ Interview Questions on Constructors and Destructors
- What is a constructor in C++?
- What are the main characteristics of a constructor?
- What is a default constructor?
- What is a parameterized constructor?
- What is a copy constructor?
- What is constructor overloading?
- Can a constructor be private?
- Can a constructor be inherited?
- What is a destructor?
- Why does a destructor use the tilde symbol?
- Can a destructor be overloaded?
- Can a destructor accept parameters?
- When is a destructor called?
- What is the difference between a constructor and destructor?
- How are constructors and destructors related to object lifetime?
- What happens when an object goes out of scope?
- What is the role of a destructor in dynamic memory management?
- What is the order of constructor and destructor calls in inheritance?
- What is RAII in C++?
- Why are smart pointers useful in modern C++?
C++ Practice Questions
Short Answer Questions
Can a destructor be overloaded?
- Define a constructor.
- Define a destructor.
- Write the syntax of a constructor.
- Write the syntax of a destructor.
- What is a default constructor?
- What is a parameterized constructor?
- What is a copy constructor?
- What is constructor overloading?
- State two differences between a constructor and destructor.
Programming Questions
- Write a C++ program to demonstrate a default constructor.
- Write a C++ program using a parameterized constructor.
- Write a program to demonstrate a copy constructor.
- Write a program demonstrating constructor overloading.
- Write a C++ program containing both a constructor and destructor.
- Write a program that demonstrates dynamic memory allocation and cleanup.
- Write a program demonstrating constructor and destructor execution order.
- Write a program demonstrating constructors in inheritance.
Related C++ Topics
If you are learning C++ programming, you should also study the following topics:
- C++ Classes and Objects
- C++ Object-Oriented Programming
- C++ Inheritance
- C++ Polymorphism
- C++ Encapsulation
- C++ Abstraction
- C++ Function Overloading
- C++ Operator Overloading
- C++ Pointers
- C++ Dynamic Memory
- C++ Arrays
- C++ File Handling
- C++ Exception Handling
- C++ Templates
- C++ STL
- C++ Interview Questions
These topics together provide a strong foundation for learning object-oriented programming in C++.
Conclusion
Constructors and destructors are fundamental concepts in C++ object-oriented programming. A constructor is automatically called when an object is created and is mainly used to initialize the object. A destructor is automatically called when an object is destroyed and is mainly used for cleanup and resource management.
C++ provides different types of constructors, including default constructors, parameterized constructors, copy constructors, and constructors with default arguments. Constructor overloading allows a class to provide multiple ways to initialize objects.
Destructors are particularly important when classes manage resources whose lifetime must be controlled. In modern C++, good resource management is commonly implemented using RAII, standard library containers, and smart pointers.
A clear understanding of constructors and destructors provides a strong foundation for more advanced C++ concepts such as inheritance, polymorphism, dynamic memory management, templates, and resource management.
About the Author
Er. Basant Kumar Yadav is an engineering graduate, teacher and educational content creator who writes programming, computer engineering and technology tutorials for students.
