Constructor and Destructor in C++

Constructors in C++

As the name suggest to construct space, or in direct words, 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 any 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 though automatically calls a default constructor implicitly to assign a default value in cases when we don’t create a constructor ourselves.
  • Constructor do not have a return type
Constructors and vs Destructors in C++

Syntax –

Constructor can be declared inside the class or outside the class (using scope resolution :: operator)

Inside the class

class A { 
int i;
public:
A(){
// Constructor declared
// assignments for initialisation
}
};

Outside the class

class A
{
 int i;
 public:
     A(); 
//Constructor declared }; A::A()
// Constructor definition {
// initialisation of values i=1; }
  1. Constructors have the same name as class name
  2. They are called using classname and (); example A();
  3. They are initialised outside class definition and defined with class name and scope resolution :: operator.

Types of Constructors in C++

  • Default Constructor
  • Parametrised Constructor
  • Copy Constructor
Types of Constructors in C++

Default Constructor

The default constructor is basically the most basic type of constructor which doesn’t take any type of passing arguments.

For Example –

class A
{
int i;
public:
A()
 {
  i=1;
 }
};

int main()
{
A a;
cout << a.i;
}

When the object is created with A a; automatically our defined constructor is called and assigned a value of 1;

Output : 1

Even if, we don’t create a constructor the complier implicitly will call its own default constructor –

Example –

#include <iostream>
using namespace std;
class A
{
public:
int i;
};

int main()
{
A a;
cout << a.i;
}

  • In this case the output will be 0.
  • The compiler calls its own default constructor and assigns a value of 0.
  • It may be any value randomly chosen by computer not necessarily 0.

Parameterised Constructors

In the extension of default constructor, we pass arguments in the constructor call and set values.

Example –

#include
using namespace std;
class A
{
  public:
    int i;
    A(int val)
    {
       i=val;
    }
};
int main()
{
//setting parameterised values A a(10); A a2(20) cout << a.i; cout << a2.i; }

Output will be 10 and 20.

Copy Constructor

These are the special type of Constructors which takes an object as an argument, and is used to copy values of data members of one object into another object.

Syntax –

class_name (class-name &){
....
}
  • Copy Constructor is a type of constructor which is used to create a copy of an already existing object of a class type.
  • It is usually of the form X (X&), where X is the class name.
  • The compiler provides a default Copy Constructor to all the classes.
#include<iostream> 
using namespace std;

class PrepInsta
{
private:
int x, y;
public:
PrepInsta()
{

}
PrepInsta(int x1, int y1)
{
x = x1;
y = y1;

}

// Copy constructor
PrepInsta(const PrepInsta &p2)
{
x = p2.x;
y = p2.y;

}

int getX()
{
return x;

}
int getY()
{
return y;

}
};

int main()
{
// Trying to call parameterized constructor here
PrepInsta p1(10, 15);
// Trying to call copy constructor here
PrepInsta p2 = p1;
// Trying to call Copy constructor here
PrepInsta p3(p1);
PrepInsta p4;
// Here there is no copy constructor called only assignment operator happens
p4 = p1;

// displaying values for both constructors
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();

cout << "\np3.x = " << p3.getX() << ", p3.y = " << p3.getY();
cout << "\np4.x = " << p4.getX() << ", p4.y = " << p4.getY();
return 0;
}

In the above example there are two ways to call copy constructor –

  • PrepInsta p2 = p1;
  • PrepInsta p3(p1);

However, PrepInsta p4; followed by p4 = p1; doesn’t call copy constructor, its just simply and assignment operator

Read more about Copy Constructors here

Reference

Constructor Overloading

Both Default and Parameterised constructors can be overloaded

Example –

#include<iostream> 
using namespace std;

class A{
int x, y;
public:
A(int a)
{
x=a;
y=0;
cout<<"I am called and only a(10) was called\n";
}

A(int a, int b)
{
cout<<"I am called and a(10,20) was called\n";

x=a;
y=b;

}
};

int main()
{
A a(10);
A b(10, 20);
}

 

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++. As soon as the object goes out of scope the compiler automatically destructs the object.

Syntax

  1. Destructors will never have any arguments.
  2. The class name is used for the name of destructor, with a ~ sign as prefix to it.
class A
{
public:
~A();
};

Let us look at an example how we can implement destructors and constructors together in sync

#include<iostream>
using namespace std;

class A
{
public:
A()
{
cout << "Constructor called";
}

~A()
{
cout << "Destructor called";
}
};

int main()
{
A obj1;
// Constructor Called
int x=1;
if(x)
{
A obj2;
// Constructor Called
}
// Destructor Called for obj2 as obj2 out of scope
}
// Destructor called for obj1 as obj1 out of scope

Output –

Constructor called
Constructor called
Destructor called
Destructor called   

Please Login/Signup to comment