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. This constructor will initialize the height to the first provided value and the width to the second one. If we are using this "Rectangle" class with these three constructors together, we now have three possible ways to declare and initialize a rectangle, a "Rectangle"-type object. The first we have showed you corresponds to the call to the default constructor. The second corresponds to a declaration such as this one, expecting an initialization value. In this case we would thus initialize the height to 2 and the width to 2 times 2, that is 4. And, finally, the last possible way is to declare a rectangle while providing two values which obviously correspond to the call to this constructor here, expecting two values. 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? 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 like this, since we have not provided any initialization value, we will seek the default constructor of the class. It is this one because it is possible to call this constructor without giving it any value. In this case, it will take 1. Concretely, this means that we will build, thanks to this call, a rectangle which value for the height member will be the value provided by default here, that is 1. Also, the value for the width member will be two times the default value, that is 2. By the way, please note that using default values for the parameters has allowed us to write both previous constructors in one single constructor. We write a single constructor, callable with an empty list of arguments, and this one, which is callable with an argument. This constructor can very well be called like this. We are declaring a variable, but, this time, precising another value than the default value. This means that we cannot keep the default value. We are using the one-parameter constructor. Instead of using the default value for "C", we wish to use the value 5. We may call this constructor either with this syntax or this one. You now know what a default constructor is. Let us now focus on the second point of this sequence: What does happen if a class does not contain any explicitly programmed constructor? Indeed, the initialization of such an object is so important that we cannot do without it. Therefoew, in C++, even if you do not declare any constructor in a class, a basic constructor will be automatically generated. This default constructor does not require any initialization value and is automatically generated. We could call it the default default constructor. It is furnished by default if we do not declare any constructor for the class. It is obviously a default construcor since it does not expect any initialization value. What will this default default constructor concretely do? It will initialize the member attributes. If these attributes are objects, they will be initialized through their associated default constructors. On the other hand, if the attributes are basic-type objects, they will remain uninitialized. Let us take a concrete example. Let us imagine that we are writing a "Rectangle" class with, as attributes, a height and a width of basic types. We could imagine that we also wish to associate another attribute to this rectangle. This would be an object permitting to modelize, for example, the position of the rectangle on a screen. If we proceed to the declaration of a rectangle coded this way, we can do so, even without any explicit constructor in the class. That means that we will call the default default constructor. What does this constructor do? It uses a minimal version of of the initialization. The object built like this will have its fields height and width remain uninitialized since they are of a basic type. On the other hand, the position member will have a value provided by the default constructor of the "position" class -if the class has one. For example, we could imagine that the position is a two-dimensional coordinates and that the default constructor initializes to the position (0, 0). In this case, we would have these values here in the position attribute. As a reminder, the basic types are the types int, double, char and bool. However, be careful regarding default default constructors. They have the following specificity. As soon as we specify a specific constructor in a class, be it a default constructor or not, the default constructor is not provided anymore. This concretely means that if, in a class, we specify an explicit constructor which is not a default constructor, then we cannot build objects of this class without providing an explicit initialization value for the attributes. This typically corresponds to something we want. When we go through the trouble of programming an explicit constructor we do not wish C++ to sneakily add a default constructor without our permission. We will see later that C++ 2011 lets us, if we so desire, reactivate the default default constructor. We will also see with which syntax this can signified to the compiler. Now we will, through concrete examples, examine different variants of constructor declaration in a same class. We will see what these variants let us do or not regarding the declaration- initialization of an instance. In the A variant, the "Rectangle" class provides attributes, as before, for the height and the width. It could also provide other things but no explicit constructor for this class. In the B variant, the "Rectangle" class provides an explicitly programmed default constructor. This constructor will initialize the different attributes to 0. In the third variant, the C variant, the "Rectangle" class provides a constructor where all the parameters have a default value. This constructor can be explicitly called without associating any initialization value. In this case, 0 will be put in each attribute. This is thus an explicitly programmed default constructor. Finally, in the last variant, the D variant, the "Rectangle" class provides an explicit constructor expecting 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, since they are basic-type attributes (they are doubles), the default default constructor will not put any initialization values into these attributes. Finally, our "r1" rectangle will have its members height and width remain uninitialized. The object exists but its attributes are not initialized. In a class where no explicit constructor exists, the only usable constructor is the default default constructor. It can only be called with this syntax, without any initialization value. Therefore, any attempt to initialize an object using initialization values is absolutely invalid. Concretely, this would result in an error upon the compiling. 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. This way here signifies that we are calling the constructor while accepting the default values. Thus, our rectangle will have 0 and 0 as the values of its attributes. According to this second variant, we signify that we are taking the value 2.5 as the value of the first parameter and that we accept the default value 0 for the second parameter. Finally, in this last variant, it is also possible to declare a rectangle, providing two values. This means that we are not using the value 2.5 for this parameter and the value 3.4 for this parameter. In the C variant, there is thus a default constructor which is one of the three constructors. The one we can call with this syntax will give the value 0 to both attributes. Therefore, this line is clearly valid will correspond to the initialization of both attributes to 0. 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 initializing the width member without doing anything for the height. This constructor is called through such a twist. The "r" object built this way will thus have a width member initialized to this value through the default constructor, that is, 5. However, nothing has been specified for the height member in the constructor. We will thus search this value specified here. The height will thus be 0. Actually, it is preferable to let the constructors deal with the initialization rather than using these tricks. Why? Because this lets us have a clear grasp on the initialization process only by reading the constructors. We do not need to search for possible default values in a section of attribute declaration. Voilà! This concludes our sequence on the default constructors. We will have the chance to come back to them during the inheritance episodes. In the meantime, you will discover new ways to build objects; for example through the copy constructors.