 In the previous segment we talked about software components. In this segment we are going to talk about constructors. So as motivation for this let us examine the queue struct in TaxiDispatch. So this is the definition of the queue, the queue struct and if you remember there was an array of elements storing the weighting drivers IDs, then a few variables, then an initialize member function and insert and reboot member functions. The main program looks something like this, so we started off by declaring, by defining the queue, creating the queue and then we called queue.initialize. And then there was a fairly long, well there was a loop which I have not put up over here. I want to look at these two statements. There is a simple problem over here which is that a programmer may forget to call initialize. People forget. What do you do? It turns out that the designer can ensure that n weighting and queue front will become zero which was the job of initialize if you remember even if the programmer forgets to write this. How that happens we are going to see in a minute but that is the idea that somehow when we create the queue we will execute something automatically which will cause n weighting and front to become zero. So this special code that we execute at the time of the construction of an object is called the constructor code. So basically the constructor is called whenever an instance of a struct or a variable of a struct type is created. Now inside the body of the structure definition, the structure type definition, a constructor has the same name as the struct and it does not have a return type. So we will see that. For struct queue these were the members and here is the constructor. So queue there could be parameters but this particular constructor does not have a parameter and then we are going to give the code that is to be executed. That is it. So this is what the constructor is. And in main the moment I define queue, I define small queue as an object of type, structure type queue, this code is automatically going to be called. So the variables will be created and then this code will be called. So the code inside the constructor is expected to perform initialization of the members. But of course it is any code so you can make it do whatever you want but that is why, but its purpose really is it was intended, the notion of a constructor was created so as to enable initializations and enable initialization. So it is really the designer who is keeping track and saying that oh look when you create you need to initialize and therefore I will give you a way of making that happen automatically. And then the user just writes this and the initialization happens. So the constructor is called automatically. So in general we can have constructors in any struct. So here is a struct x which is being defined and here is a constructor. So this is the body of the constructor. And in main I can have, I am declaring little x to be an object of type capital X and I am supplying these arguments and this will cause this constructor to be called. So the constructor can take arguments and the creation of an object x in the main can be thought of as happening in two steps. First the memory is allocated for x in the activation frame of main and then the constructor is called on x with the given arguments. So this code executes but with respect to these arguments and if you have members referenced over here they are considered to be the members of this structure, of the constructed structure. You can have many constructors provided they have different signatures and we are going to see examples of this as well. So here is an example of constructors for v3. So this first constructor does not take any arguments and it just sets all the members to 0. This constructor takes a single argument and it sets all the members to that single argument. Are these useful? Well if they are not useful you can define your own but I am just showing this just to show you what the possibilities are. So how does the main program work? Well when the main program works when defining v1 here an argument has been given. So the constructor which takes a single argument so since a single argument is given the constructor taking the single argument is called. So this is the constructor which takes a single argument. So thus it means that v1.x, v1.y, v1.z are set to the value 5, set to value a which in this case is 5. When defining v2 no arguments have been given. Well note that we are not putting parenthesis either. So this is kind of a non-uniformity but that is what it is. We are not putting parenthesis. So we are defining, we are just giving the name of the variable which is being defined and since no parenthesis argument, no arguments are given we look in the definition of v3 and see if there is a constructor without arguments. So this is the one. So this constructor will get called v2.x, v2.y, v2.z will be set to zeros. So that is how constructors work. So these constructors work. So now you might have wondered we have been creating all these structures without even knowing about constructors. So what was going on then? Well it turns out that C++ defines a constructor for which takes no arguments and does nothing. So if and only if you do not define a constructor. So in the earliest examples we did not define any constructor but you could think of it this way that C++ supplied a constructor which was really doing nothing. So you could say that look there always is a constructor either something provided by C++ or something that you provide. Now there is a technical term which is very commonly used which is the notion of a default constructor, a constructor that does not take arguments and this constructor might be defined by you or by C++ is called a default constructor. If you define an array of struct each element is initialized using the default constructor. So if you want to construct an array of a certain kind of struct then you had better have a default constructor. So now look at this line. So if you define a constructor then C++ does not define a constructor. So if you define a two argument constructor then C++ will not even define a zero argument constructor but if you want an array of struct then that is not a satisfactory state. So if you define a two argument constructor and you want to define arrays then you had better also define a zero argument constructor and it could do nothing but that is fine. That is what C++ would define on its own anyway. One more remark. So if a member itself is a struct then its constructor is called first and even this is the default constructor. So or however that constructor is used. So for example struct point XXI I have struct disk point center so when constructing disk center will be constructed according to the constructor of point. So what have we discussed in this segment? So we have said that constructors can be defined so that variables get initialized in the initialized the moment they are created automatically. There can be many constructors with different signatures. Next we are going to talk about something called operator overloading but before that we will take a quick break.