Constructors and destructors are two of the most important concepts in Object-Oriented Programming (OOP). They automate object initialization and cleanup, making C++ programs more efficient, organized, and easier to maintain. C++ Constructors and Destructors practice questions with solutions help to understand the concepts.
A constructor is a special member function that is automatically called when an object is created.
A destructor is a special member function that is automatically called when an object is destroyed.
Unlike normal functions:
- Constructors have the same name as the class.
- Constructors do not have a return type.
- Destructors start with the tilde (
~) symbol. - Destructors also do not have a return type.
Constructor Example
class Student
{
public:
Student()
{
cout << "Constructor Called";
}
};
Destructor Example
class Student
{
public:
~Student()
{
cout << "Destructor Called";
}
};
Types of Constructors
- Default Constructor
- Parameterized Constructor
- Copy Constructor
Constructors and destructors are widely used in:
- Memory Management
- Resource Allocation
- File Handling
- Database Connections
- Game Development
- Enterprise Applications
In this chapter, you’ll solve beginner-friendly constructor and destructor practice questions with complete explanations.
Each question includes:
- Problem Statement
- Complete C++ Solution
- Sample Input
- Sample Output
- Explanation
- Concepts Covered
Let’s begin.
1. C++ Program to Demonstrate a Default Constructor
Problem Statement
Write a C++ program to demonstrate a default constructor.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
Student()
{
cout << "Default Constructor Called";
}
};
int main()
{
Student student;
return 0;
}
Sample Output
Default Constructor Called
Explanation
The constructor is automatically executed when the object is created.
Concepts Covered
- Default Constructor
- Automatic Function Call
- Object Creation
2. C++ Program to Initialize Data Using a Default Constructor
Problem Statement
Write a C++ program to initialize class data members using a default constructor.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
int rollNumber;
string name;
Student()
{
rollNumber = 101;
name = "Rahul";
}
void display()
{
cout << "Roll Number: "
<< rollNumber << endl;
cout << "Name: "
<< name;
}
};
int main()
{
Student student;
student.display();
return 0;
}
Sample Output
Roll Number: 101
Name: Rahul
Explanation
The constructor automatically initializes the object’s data members.
Concepts Covered
- Constructor
- Initialization
- Member Function
3. C++ Program to Demonstrate a Parameterized Constructor
Problem Statement
Write a C++ program to initialize object data using a parameterized constructor.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
int rollNumber;
string name;
Student(int number, string studentName)
{
rollNumber = number;
name = studentName;
}
void display()
{
cout << "Roll Number: "
<< rollNumber << endl;
cout << "Name: "
<< name;
}
};
int main()
{
Student student(101, "Rahul");
student.display();
return 0;
}
Sample Output
Roll Number: 101
Name: Rahul
Explanation
A parameterized constructor allows object values to be initialized during object creation.
Concepts Covered
- Parameterized Constructor
- Object Initialization
- Arguments
4. C++ Program to Create Multiple Objects Using Constructors
Problem Statement
Write a C++ program to create multiple objects using a parameterized constructor.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
int rollNumber;
Student(int number)
{
rollNumber = number;
}
void display()
{
cout << rollNumber << endl;
}
};
int main()
{
Student student1(101);
Student student2(102);
Student student3(103);
student1.display();
student2.display();
student3.display();
return 0;
}
Sample Output
101
102
103
Explanation
Each object calls the constructor independently and stores its own values.
Concepts Covered
- Multiple Objects
- Parameterized Constructor
- Object Initialization
5. C++ Program to Demonstrate a Destructor
Problem Statement
Write a C++ program to demonstrate a destructor.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
Student()
{
cout << "Constructor Called\n";
}
~Student()
{
cout << "Destructor Called";
}
};
int main()
{
Student student;
return 0;
}
Sample Output
Constructor Called
Destructor Called
Explanation
The constructor executes when the object is created, while the destructor executes automatically when the object goes out of scope.
Concepts Covered
- Destructor
- Constructor
- Object Lifetime
6. C++ Program to Demonstrate a Copy Constructor
Problem Statement
Write a C++ program to demonstrate a copy constructor.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
int rollNumber;
Student(int number)
{
rollNumber = number;
}
Student(const Student &student)
{
rollNumber = student.rollNumber;
}
void display()
{
cout << "Roll Number = "
<< rollNumber << endl;
}
};
int main()
{
Student student1(101);
Student student2 = student1;
student1.display();
student2.display();
return 0;
}
Sample Output
Roll Number = 101
Roll Number = 101
Explanation
The copy constructor creates a new object by copying the values of an existing object.
Concepts Covered
- Copy Constructor
- Object Copying
- Constructor
7. C++ Program to Demonstrate Constructor Overloading
Problem Statement
Write a C++ program to demonstrate constructor overloading.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
Student()
{
cout << "Default Constructor\n";
}
Student(int rollNumber)
{
cout << "Parameterized Constructor\n";
cout << "Roll Number = "
<< rollNumber << endl;
}
};
int main()
{
Student student1;
Student student2(101);
return 0;
}
Sample Output
Default Constructor
Parameterized Constructor
Roll Number = 101
Explanation
Multiple constructors with different parameter lists can exist in the same class.
Concepts Covered
- Constructor Overloading
- Default Constructor
- Parameterized Constructor
8. C++ Program to Initialize an Array of Objects Using Constructors
Problem Statement
Write a C++ program to initialize an array of objects using constructors.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
Student()
{
cout << "Object Created" << endl;
}
};
int main()
{
Student students[3];
return 0;
}
Sample Output
Object Created
Object Created
Object Created
Explanation
Every object in the array automatically calls the constructor during creation.
Concepts Covered
- Array of Objects
- Constructor
- Automatic Initialization
9. C++ Program to Demonstrate Constructor Execution in Multiple Objects
Problem Statement
Write a C++ program to demonstrate constructor execution for multiple objects.
C++ Solution
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout << "Employee Object Created" << endl;
}
};
int main()
{
Employee employee1;
Employee employee2;
Employee employee3;
return 0;
}
Sample Output
Employee Object Created
Employee Object Created
Employee Object Created
Explanation
Every object created from a class executes the constructor independently.
Concepts Covered
- Constructors
- Multiple Objects
- Object Creation
10. C++ Program to Demonstrate Constructor and Destructor Execution Order
Problem Statement
Write a C++ program to demonstrate the execution order of constructors and destructors.
C++ Solution
#include <iostream>
using namespace std;
class Demo
{
public:
Demo()
{
cout << "Constructor Called" << endl;
}
~Demo()
{
cout << "Destructor Called" << endl;
}
};
int main()
{
Demo object1;
Demo object2;
return 0;
}
Sample Output
Constructor Called
Constructor Called
Destructor Called
Destructor Called
Explanation
Constructors execute in the order objects are created, while destructors execute in the reverse order when objects go out of scope.
Concepts Covered
- Constructor
- Destructor
- Object Lifetime
- Execution Order
11. C++ Program to Demonstrate Dynamic Object Creation Using Constructors
Problem Statement
Write a C++ program to create an object dynamically using the new operator and a constructor.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
Student()
{
cout << "Constructor Called" << endl;
}
~Student()
{
cout << "Destructor Called" << endl;
}
};
int main()
{
Student *student = new Student();
delete student;
return 0;
}
Sample Output
Constructor Called
Destructor Called
Explanation
The constructor is automatically called when the object is created using new, and the destructor is called when delete is used.
Concepts Covered
- Dynamic Memory Allocation
- Constructor
- Destructor
newOperatordeleteOperator
12. C++ Program to Demonstrate Constructor Chaining
Problem Statement
Write a C++ program to demonstrate constructor chaining using overloaded constructors.
C++ Solution
#include <iostream>
using namespace std;
class Student
{
public:
Student()
{
cout << "Default Constructor" << endl;
}
Student(int rollNumber)
{
cout << "Roll Number = "
<< rollNumber << endl;
}
};
int main()
{
Student student1;
Student student2(101);
return 0;
}
Sample Output
Default Constructor
Roll Number = 101
Explanation
Different constructors can initialize objects in different ways depending on the arguments supplied.
Concepts Covered
- Constructor Overloading
- Object Initialization
- Constructors
13. C++ Program to Initialize Employee Details Using a Parameterized Constructor
Problem Statement
Write a C++ program to initialize employee details using a parameterized constructor.
C++ Solution
#include <iostream>
using namespace std;
class Employee
{
public:
int employeeId;
string employeeName;
Employee(int id, string name)
{
employeeId = id;
employeeName = name;
}
void display()
{
cout << "Employee ID: "
<< employeeId << endl;
cout << "Employee Name: "
<< employeeName;
}
};
int main()
{
Employee employee(201, "Amit");
employee.display();
return 0;
}
Sample Output
Employee ID: 201
Employee Name: Amit
Explanation
Parameterized constructors allow values to be assigned during object creation.
Concepts Covered
- Parameterized Constructor
- Object Initialization
- Employee Class
14. C++ Program to Demonstrate Automatic Destructor Calling
Problem Statement
Write a C++ program to demonstrate that destructors are automatically called when an object goes out of scope.
C++ Solution
#include <iostream>
using namespace std;
class Demo
{
public:
Demo()
{
cout << "Constructor Executed" << endl;
}
~Demo()
{
cout << "Destructor Executed" << endl;
}
};
void testFunction()
{
Demo object;
}
int main()
{
testFunction();
cout << "Back to Main Function";
return 0;
}
Sample Output
Constructor Executed
Destructor Executed
Back to Main Function
Explanation
The destructor executes automatically when the object leaves its scope inside the function.
Concepts Covered
- Destructor
- Scope
- Automatic Object Destruction
15. C++ Program to Demonstrate Constructors and Destructors Together
Problem Statement
Write a C++ program that demonstrates the complete life cycle of an object using constructors and destructors.
C++ Solution
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle()
{
cout << "Rectangle Object Created" << endl;
}
~Rectangle()
{
cout << "Rectangle Object Destroyed";
}
};
int main()
{
Rectangle rectangle;
return 0;
}
Sample Output
Rectangle Object Created
Rectangle Object Destroyed
Explanation
The constructor initializes the object when it is created, while the destructor cleans up automatically when the object is destroyed.
Concepts Covered
- Constructor
- Destructor
- Object Life Cycle
- OOP Fundamentals
Chapter Summary
In this chapter, you learned how constructors and destructors automate object initialization and cleanup in C++. You explored default constructors, parameterized constructors, copy constructors, constructor overloading, arrays of objects, dynamic object creation, constructor execution order, automatic destructor invocation, and the complete object life cycle. These concepts form the foundation for advanced Object-Oriented Programming and are essential for writing efficient, maintainable C++ applications.
Key Takeaways
- A constructor is automatically called when an object is created.
- Constructors have the same name as the class and do not have a return type.
- A destructor is automatically called when an object is destroyed.
- Destructors begin with the
~symbol. - Parameterized constructors initialize objects with user-defined values.
- Copy constructors create a new object from an existing object.
- Constructor overloading allows multiple constructors in the same class.
- Constructors execute in creation order, while destructors execute in reverse order.
- Dynamic objects require
deleteto invoke the destructor. - Constructors and destructors play a vital role in resource management and memory handling.
Frequently Asked Questions (FAQs)
1. What is a constructor in C++?
A constructor is a special member function that is automatically executed when an object is created.
2. What is a destructor?
A destructor is a special member function that is automatically called when an object is destroyed.
3. Can a constructor have parameters?
Yes. Such constructors are called parameterized constructors.
4. What is a copy constructor?
A copy constructor creates a new object by copying the values of an existing object.
5. Can a class have multiple constructors?
Yes. This is known as constructor overloading.
6. When is a destructor called?
A destructor is called automatically when an object goes out of scope or when a dynamically allocated object is deleted.
7. Why are constructors important?
Constructors ensure that objects are properly initialized before they are used.
8. Why are destructors important?
Destructors help release resources such as dynamically allocated memory, file handles, and database connections, preventing resource leaks.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
