Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

What is Constructor in C++ ?

6 min 7 sec read

Constructors are special class functions that perform the initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after the storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object.

Whenever you create an instance or object of a class, by default one function is being called. And that member function is called Constructors. They are special functions. There are three types of Constructors in C++

  1. Default Constructor
  2. Parameterised Constructor
  3. Copy Constructor

Why Constructors are special functions?

Usually, when we create a function, it returns some value if we specify the datatype. The return value can be like an integer value, float value, or double value, or void

  • Constructors return nothing not even void.
  • Constructors have the same name as the class name.
  • Constructors are used for the initialization of the objects.

First Scenario

In this case, we will create an object of a class but we dont write any default constructor to catch it. Default constructors are always there by default. They are there even if you dont call them. So the following program will get executed successfully.

#include 
using namespace std;
class Game
{
public:

}; 

int main()
{
// Creating Game class's object
 Game sankeObject;   
}

Second Scenario

In this case, we will create an object of a class and now we create one Default Constructor  to catch it. As mentioned in the first scenario, Default constructors are created by default whenever we create an object of a class. Here, we created our own default constructor to handle it programmatically.

#include 
using namespace std;
class Game
{
public:
// Default Constructor
   Game()
   {
    cout <<"Constructor is being initialised 
here with no parameters";
   }

}; 

int main()
{
 Game sankeObject;  // Creating Game class's object
}

In the given program, 

The program starts through main() function in which we created an object of Game class named as sankeObject. When this sankeObject is created by default compiler calls a Constructor which is also called a default constructor. Inside the default constructor, "Constructor is being initialized here with no parameters" is printed.

Parameterised Constructor

It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. When we create an object of a class by default "Default Constructor" is called. You can also pass some data as a parameter to these constructors like any other function. Lets see this in action with two cases so that concept gets cleared in-depth:

First scenario

#include 
using namespace std;
class Game
{
public:

}; 

int main()
{
// Creating Game class's object with parameter 2
 Game sankeObject(2);    
    // Parameterised Constructor is being called
}

Output :-

Compilation Error

In this program, we created a snakeObject with parameter value 2. But there is no Parameterised Constructor in the class which is defined there. In the case of the default constructor, we do not need any parameters to pass but parameterized constructor wont work like that. Lets see the second scenario now.

Second Scenario

#include 
using namespace std;
class Game
{
public:
// Parameterised Constructor
   Game(int a)
   {
    cout <

Output

2

In this program, what we did is simply created an object snakeObject and passed a value 2 which will then be caught by a Parameterised Constructor, and then printed the passed value which is 2. Thats why the output is 2.

Points to Remember 

1) If an object is created using Default Constructor and even if we do not define any default constructor in the class, it will still work. 

2) If an object is created using Parameterised Constructor and if we do not define Parameterised Constructor then the C++ program will throw a Compilation Error.

What is Destructor?

Destructor is a special class function that destroys the object as soon as the scope of an object ends. The destructor is called automatically by the compiler when the object goes out of scope.

The syntax for destructor is the same as that for the constructor, the class name is used for the name of the destructor, with a tilde ~ sign as a prefix to it.

Example :-

#include 
using namespace std;
class Game
{
public:
   Game(int a )
   {
    cout <

Output

2
Destructor Function is called and deleted an Object.

In this program, the Destructor Function is called and deleted an Object.

When the main function completes all the execution of statements include in it and reaches at the end of the curly brace which is also called as End of Scope of a function then the Destructor function is called.

Recommended Articles

1) What is Shallow Copy Constructor in C++ ?

1) What is Deep Copy Constructor in C++ ?

3) Write C programs to implement i) strncpy(), ii) strstr() iii) strrchr library function by yourself

4) Write C program to insert a node after a given node in a linked list

5) Article on different storage classes in C.

6) C Tutorial on Calloc() vs malloc() functions is explained in depth in this article.

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor