Article by: Manish Methani
Last Updated: October 18, 2021 at 10:04am IST
In C++, constructors and destructors are two special member functions that are used to initialize and clean up the objects of a class. Constructors are called when an object is created, while destructors are called when an object is destroyed. Constructors initialize the data members of a class, and destructors clean up any resources allocated by the object. In this tutorial, we will discuss constructors and destructors in detail and provide examples of their usage.
Constructors are of four types in C++: default constructor, parameterized constructor, copy constructor, and constructor overloading.
A default constructor is a constructor that takes no arguments. It is called when an object of the class is created with no arguments. The default constructor initializes the data members of the class to their default values.
#include<iostream> using namespace std; class MyClass { public: MyClass() { cout<<"Default constructor called"<<endl; } }; int main() { MyClass obj; return 0; }
Output:
Default constructor called
A parameterized constructor is a constructor that takes one or more arguments. It is used to initialize the data members of the class with the values passed as arguments. The parameters of a constructor are used to set the values of the data members.
#include<iostream> using namespace std; class MyClass { public: MyClass(int val1, int val2) { member1 = val1; member2 = val2; } void display() { cout<<"Member 1:"<<member1<<endl; cout<<"Member 2:"<<member2<<endl; } private: int member1; int member2; }; int main() { MyClass obj(10, 20); obj.display(); return 0; }
Output:
Member 1: 10 Member 2: 20
A copy constructor is a constructor that is used to create a new object from an existing object of the same class. It is called when an object is created using the assignment operator or when an object is passed as an argument to a function.
#include<iostream> using namespace std; class MyClass { public: MyClass(int val1, int val2) { member1 = val1; member2 = val2; } MyClass(const MyClass &obj) { member1 = obj.member1; member2 = obj.member2; } void display() { cout<<"Member 1:"<<member1<<endl; cout<<"Member 2:"<<member2<<endl; } private: int member1; int member2; }; int main() { MyClass obj1(10, 20); MyClass obj2 = obj1; obj2.display(); return 0; }
Output:
Member 1: 10 Member 2: 20
Constructor overloading is a technique in C++ that allows you to define multiple constructors for a class. Each constructor can have a different number of parameters or different types of parameters. When an object of the class is created, the appropriate constructor is called based on the arguments passed to it.
#include<iostream> using namespace std; class MyClass { public: MyClass() { member1 = 0; member2 = 0; } MyClass(int val) { member1 = val; member2 = val; } MyClass(int val1, int val2) { member1 = val1; member2 = val2; } void display() { cout<<"Member 1:"<<member1<<endl; cout<<"Member 2:"<<member2<<endl; } private: int member1; int member2; }; int main() { MyClass obj1; MyClass obj2(10); MyClass obj3(10, 20); obj1.display(); obj2.display(); obj3.display(); return 0; }
Output:
Member 1: 0 Member 2: 0 Member 1: 10 Member 2: 10 Member 1: 10 Member 2: 20
A destructor is a special member function of a class that is called when an object of the class is destroyed. It has the same name as the class with a tilde (~) symbol in front of it. The destructor is used to free up any resources that were allocated to the object during its lifetime.
#include<iostream> using namespace std; class MyClass { public: MyClass() { cout<<"Constructor called"<<endl; } ~MyClass() { cout<<"Destructor called"<<endl; } }; int main() { MyClass obj; return 0; }
Output:
Constructor called Destructor called
A1. Constructors are used to initialize the data members of a class when an object of the class is created.
A2. A default constructor is a constructor that takes no arguments.
A3. A parameterized constructor is a constructor that takes one or more arguments.
A4. A copy constructor is a constructor that creates a new object as a copy of an existing object.
A5. Constructor overloading is a feature in C++ that allows a class to have multiple constructors with different parameter lists.
ClassName::ClassName() { // Constructor code here }
ClassName::~ClassName() { // Destructor code here }
A8. A constructor is called when an object of a class is created, whereas a destructor is called when an object is destroyed.
A9. Constructors and destructors are important in C++ because they help to ensure that objects are properly initialized and cleaned up, which helps to prevent errors and memory leaks.
A10. No, a class can only have one destructor in C++.
A11. If a constructor throws an exception in C++, the object is not created and any resources that were allocated before the exception occurred are automatically cleaned up.
A12. Yes, constructors can be virtual in C++, but it is generally not recommended because it can lead to confusing and unexpected behavior.
A13. No, a destructor cannot be overloaded in C++.