











Constructor and Destructor in C++
Constructor and Destructor in C++
Here, on this page, we will discuss Constructors and Destructors in C++.
- The constructor is used to allocate the memory if required and constructs the object of the class.
- Whereas, a destructor is used to perform required clean-up when an object is destroyed . The destructor is called automatically by the compiler when an object gets destroyed.


Constructor in C++
What are Constructors
Constructors are a unique class functions that do the job of initialising every object. Whenever, any object is created the constructor is called by the compiler.
- When we create an object and don’t assign values the data of that object take up garbage values.
- To make sure that this doesn’t happen we use constructors to assign values to them as soon as they are created or memory is assigned.
The new version of C++ automatically calls a default constructor implicitly to assign a default value in cases when we don’t create a constructor ourselves.
Note – Constructors do not have a return type


Example
Assume that you're playing an online game. Whenever someone starts a new game the following may need to be initialized by default -
1. Player Health
2. Player Speed
3. Initial Ammos
4. Initial location etc
1. Player Health
2. Player Speed
3. Initial Ammos
4. Initial location etc
Syntax –
A constructor can be declared –
- Inside the class
- Outside the class (using scope resolution :: operator)
More information
- Constructors have the same name as class name
- They are called using classname and (); example myClass();
- They can also be defined(initialised) outside class by using the class name and scope resolution :: operator.
Example Syntax
Inside the class
class myClass() { // member variables int i; public: // Constructor myClass(){ // initializations here } };
Outside the class
class myClass() { // member variables int i; public: // Constructor declared here myClass(); }; // Constructer defined here myClass::myClass(){ // data member initializations here }
Default Constructor
If we do not create our own Constructor, C++ creates a default Destructor to allocate memory to various members of the class.
Destructors in C++
We worked with initialising the object values, but what when the scope of the object ends?
We must also destroy the object, right ! For this we use Destructors in C++. We can create our own destructor as follows –
Default Destructor
If we do not create our own Destructor, C++ creates a default Destructor. Which is called automatically by compiler as soon as the object goes out of scope.
Syntax
- Destructors will never have any arguments.
- The class name is used for the name of the destructor, with a
~
sign as a prefix to it.
class myClass { public: // this automaticlly destroyed // the current instance of the class ~myClass(); };


Let us look at an example how we can implement destructors and constructors together in sync
Run
#include <iostream> using namespace std; class myClass { public: myClass(){ cout << "Constructor called" << endl; } ~myClass(){ cout << "Destructor called" << endl; } }; int main() { myClass obj1; // Constructor Called for obj1 int x = 1; if(x){ myClass obj2; // Constructor Called for obj2 } // Destructor Called for obj2 as obj2 out of scope return 0; }// Destructor called for obj1 as obj1 out of scope
Output -
Constructor called Constructor called Destructor called Destructor called
Read Further
There are many types of constructors in C++. Please also read more below –
Difference b/w Constructor and Destructor
Compared on | Constructor | Destructor |
---|---|---|
Use | Constructor is invoked to initialise and object of a class | Destructors are invoked when object of a class is destroyed |
Declaration | Declared as – class_name(arguments if any) | Declared as – // destructor |
Arguments | Constructors can have arguments (parameters) | Constructors can not have arguments (parameters) |
Job | Mainly used to initialise the objects of any class and also to initialise values to data members of the class. | Destructor is used to deallocate the memory of an object of a class. |
Overloading | Constructors as they can be overloaded Example – myClass() | Destructors can not be overloaded |
Number | Since, overloading is allowed there can be multiple constructors for a class | There can only be at max one user defined destructor as overloading is not allowed |
Copy | There is a concept of copy constructor which is used to initialise an object from another object | There is no copy destructor concept. |


Login/Signup to comment