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++

  • 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

Constructor and Destructors in C++

Syntax –

A constructor can be declared –

  1. Inside the class
  2. Outside the class (using scope resolution :: operator)

More information

  1. Constructors have the same name as class name
  2. They are called using classname and (); example myClass();
  3. They can also be defined(initialised) outside class by using the class name and scope resolution :: operator.

Example Syntax

class myClass() {
    
    // member variables
    int i;
    public:
    
    // Constructor
    myClass(){
       // initializations here
    }
};
class myClass() {
    
    // member variables
    int i;
    public:
    
    // Constructor declared here
    myClass();
};

// Constructer defined here
myClass::myClass(){
    // data member initializations here
}

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 –

Syntax

  1. Destructors will never have any arguments.
  2. 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();
};
Constructor and Destructors in C++ 2

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

Difference b/w Constructor and Destructor

Compared onConstructorDestructor
UseConstructor is invoked to initialise and object of a classDestructors are invoked when object of a class is destroyed
DeclarationDeclared as –

class_name(arguments if any)
{ // constructor body
}
Declared as –

// destructor 
~Class_Name();

ArgumentsConstructors can have arguments (parameters)Constructors can not have arguments (parameters)
JobMainly 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.
OverloadingConstructors as they can be overloaded
Example –


myClass()
myClass(int a)
myClass(int a, int b)
myClass(float a, float b)
Destructors can not be overloaded
NumberSince, overloading is allowed there can be multiple constructors for a classThere can only be at max one user defined destructor as overloading is not allowed
CopyThere is a concept of copy constructor which is used to initialise an object from another objectThere is no copy destructor concept.