0:03
In this new episode, we will focus on
the so-called default constructor.
This includes two aspects.
Firstly, what does happen when we declare an object
without associating any initialization value?
Secondly, what happens in classes where we have not
declared any explicit constructor?
Let us by begin with the first point.
A default constructor is either a constructor
without any parameter -as you can see in this example here-
or a constructor for which all parameters have default values.
We will see an example of it a litte later.
Concretely, if a constructor does not have any parameter
or has default values for all his parameters, this will mean that this
constructor can be called without initialization values.
For example, as we proceed to the declaration of a "Rectangle" object,
like this, we are not associating any initialization value
to this rectangle-type variable "r".
Concretely, this means that the initialization task will be supported
by constructor which does not need any parameter in order to function.
THIS is the default constructor.
We could imagine we wish to allow three possible ways to
initialize a rectangle of the "Rectangle" class.
That means that we are anticipating the presence of three constructors.
A constructor which does not require any parameter and will initialize
by default the height to 1 and the width to 2.
A second constructor -not by default since
it expects one parameter-
which will initialize, for example, the height to the provided value
and the width to two times the provided value.
And the third constructor, as we have seen up until now,
taking to parameter and is not a default constructor either
since its parameter list is not empty.
2:53
Please be careful, though! Seeing the syntax
employed to call
the two-parameter constructor or the one-parameter constructor,
it could be tempting to call the default constructor
resorting to this syntax.
Declaring the "r" rectangle signifying, with a pair of empty parentheses,
that we are calling this constructor.
This is NOT the case.
In C++, this syntax does not correspond to a call to the default constructor.
If you wish to call the default constructor, you must resort to
this syntax here, without the parentheses.
By the way, if you write this, the compiler will not produce an error.
In your opinion, how does the compiler interpret this line of code?
3:48
We have just seen that a default constructor in a constructor
we can call without providing any initialization values.
This is the case of constructors with an empty parameter list.
This is also the case of constructors for which all parameters
have a default values. Here is an example thereof.
Similarly to ordinary functions or methods,
a constructor may very well have default values
for some of its parameters -or all of them, for that matter.
This constructor is a default constructor
since all its parameters have a default value.
By the way, please remember that a default values is a value
we may associate with a parameter according to this syntax
in the prototype of the function or the method.
When a function or method has a default value,
this means that we may
call this function or method without providing the value.
In this case, the default value is taken.
Concretely here, if we declare a rectangle
11:26
two initialization values. There is thus no explicitly programmed
default constructor in this "Rectangle" class.
Now, we will ask ourselves :
For each variant of the class, what is the default constructor?
Is this declaration syntax valid?
Is this alternative syntax for the declaration initialization of a
"Rectangle"-type object valid?
As a remainder, in the A variant, this was class schema
regarding the constructor declaration.
Here, we are not precising the attribute declaration
since it is the same for all variants.
Here, in the A variant of the "Rectangle" class, there was
not explicitly declared constructor.
This means that, regarding the default constructor in the A variant,
since there is no explicitly specified constructor,
we will have the default default constructor
which is automatically generated.
When we proceed to such a declaration of a rectangle,
we are not specifying any initialization value.
Since there is no explicit constructor in our "Rectangle" class,
the default default constructor will be called.
Also, we know that this constructor initializes things in a minimalistic way.
Here, for our height and width attributes,
13:40
Let us now move on to the B variant. In the case of B,
our "Rectangle" class has its default constructor
explicitly declared.
Its task is to initialize the height and width to 0 both.
In this case, the default constructor is this the default constructor
which has been explicitly declared.
It is therefore possible to resort to this syntax which does not require
any initialization value. Unlike the previous case though,
this time, the default constructor, called here,
will properly initialize the height and width assigning them both 0.
Since there is no constructor expecting values as parameters
in the "Rectangle" class, this way to declare/initialize a rectangle object
is invalid in this case.
The C variant is a litttle bit more complex than the previous ones.
Here we have an explicitly programmed constructor
for which all parameters have a default value.
Concretely, this means that we can call this constructor
in three possible ways.
15:52
This line, corresponding to this kind of calls is obviously
valid aswell. In this case,
it will let us initialize the width to 1 and the height to 2.
For the D variant, we have, in the class, an explicit constructor
which is not a default constructor.
Its parameter list is not empty
and its parameters do not accept any default value.
As we have seen previously, as soon as an explicit constructor is programmed,
the default default constructor vanishes.
Concretely, this means that there is no default constructor
at all in this D variant.
Consequently, a declaration of this nature becomes invalid.
Of course, in this variant, it is possible to declare/initialize
a rectangle like this.
Indeed, the sole existing constructor expects two initialization values.
Ultimately, we will build here a rectangle similar to the previous one;
its height is 1, its width is 2.
As we have illustrated in the D variant, the default default
constructor ceases to exist as soon as we define
an explicit constructor in the class -be this constructor a default constructor or not.
If we wish to reactivate the default default constructor
(this is possible in C++ 2011), we need to resort to the following syntax.
We declare in the class the fact that the default constructor
will be the default variant provided by the compiler.
If, in the case of our previously seen D variant, we wish to reactivate
the default default constructor
since it had disappeared upon the declaration
of an explicit constructor,
we must clearly specify our wish.
By the way, please note that it is not particularly interesting here.
Whyso? Because we have learnt that the default default constructor
did not initialize the basic type attributes.
It is never truly recommandable to have the possibility to build an object
where certain attributes are non-initialized.
That said, in all situations where it is relevant to reactivate
the default default constructor
-typically when all the members are object-
we will have to resort to this syntax.
Reactivating a default variant is actually generalizable to other methods;
we will see other examples in upcoming episodes.
This is also generalizable for the suppression of a method;
in this case, we will use the syntax " = delete ".
For example, let us suppose that, for a given class, there is
a particular method expecting, as parameter, a double-type value.
We know that C++ let us §§store an int in a double
Normally it is thus possible to call "pas_d_int" (TN: means "no_int)
providing, as argument, an integer value.
We wish to deactivate this possibility, thus forcing "pas_d_int"
to systematically be called with a double.
In this case, we may use this clause,
thus triggering a "delete" of the method called with an int.
This will render it impossible to call the "pas_d_int" method with an integer value
as argument.
Also, please note that C++2011 lets us arrange for
a constructor to call another constructor of the same class.
This is done in the colon section.
Here, the default constructor of the "Rectangle" class calls.
in the colon section, the two-parameter constructor
of the same class.
This will let the default constructor initialize the height
and the width to 0.
Please note that this way to proceed is much better
than the previous where we reactivated
the default default constructor.
Indeed, we have seen that reactivating the default default construtor
had the disadventage of letting basic type
attributes uninitialized.
This will not be the case here.
Concluding on the initializtion, please note that, in C++11,
it is possible to assign default values to attributes
directly where they are declared.
Namely, outside any constructor.
If it so happens that a called constructor will not modify the value
of an attribute which has a default value so specified,
then this default value will be used.
Let us imagine, for example, that, in the "Rectangle" class,
we have declared a constructor