Use of constructors in C++

Written by Hamza on. Posted in C++

A constructor in a class is a special function called at instantiation of an object, or declaration of an object. What it does is simple: it does everything that you require to be done automatically every time an object of your class is created. This helps us to avoid system crashes and makes sure that the object we create does not always create variable or data that is unacceptable for our program. This will become clearer to you as you read on. 

A constructor is a function that has the same name as the class itself, which has no return type, and which may or may not have parameters. There are three types of constructors:

1) Default constructor: one that uses no parameters.

2) Parameterized constructor: one that has parameters.

3) Copy constructor: a constructor that is used for deep copy. Find more info on how to write a copy constructor .

Firstly let us talk about default constructors. This is called automatically whenever an object is created. There are four types of default constructors:

1) Without any parameters written by us in the program.

2) With one parameter having default value.

3) With more than one parameters but all of them with default parameters.

4) Compiler generated default constructor.

We need not go into the details of all these. For now, it is enough to know that when we do not explicitly make a default constructor, the compiler automatically generates one. However, it is good to always define our default constructor.

A constructor that has parameters is also a very useful way of declaring an object directly with the values we require. Calling such a constructor is different, however. To clarify, let us look at the following class called fraction with two constructors:  a default constructor(which is explicitly stated) and another constructor with two parameters.

</p>
<p>class fraction</p>
<p>{</p>
<p>.  private:</p>
<p>.   int numerator;</p>
<p>.   int denominator;</p>
<p>.  public:</p>
<p>.   fraction()  //default constructor</p>
<p>.   {</p>
<p>.    denominator=1;</p>
<p>.   }</p>
<p>.   fraction(int x,int y)</p>
<p>.   {</p>
<p>.    numerator=x;</p>
<p>.    denominator=y;</p>
<p>.   }</p>
<p>};</p>
<p>int main()</p>
<p>{</p>
<p>.  fraction obj1;</p>
<p>.  fraction obj2 (2,3) ;</p>
<p>}</p>
<p>

 

In this class, I have defined the functions in line, i.e, within the class itself and not in another file as shown in the previous article.

Now when we create an object such as obj1 as we have done in the main, this default constructor is called and the value of the denominator of obj1 set to 1. In real life, we know that a fraction cannot have a denominator that is 0. So in our default constructor, we define that whenever an object is created, it is created with a denominator with a value of 1. If we had not written this default constructor, the compiler would have automatically generated it and maybe the denominator value maybe would have been set by default to 0, which would be something we do not require.

The second line in the main calls the second constructor that we have defined. It makes a fraction with a numerator 2 and a denominator 3. Now, at the moment of instantiation, we have created an object with the required values and gotten an object perfectly suited to what we want,i.e, a 2/3 fraction. Otherwise, we would first create the object like we have done for obj1, calling the default constructor, then called a member function that would set the values of denominator and numerator to what we require. This would not be an efficient produce, and thus this is the power of a paramterized constructor.

An understanding of when and what type of constructor is called in your code is very important for C++ programmers. If you feel confused over anything about them, please feel free to leave a comment or leave a line on facebook, twitter or google plus!