 Hello and welcome back. In this lecture, we are going to continue our study on constructors in C++ programs. Here is a quick recap of some of the topics we have already studied and that are relevant to this lecture. We have studied about object-oriented programming with structures and classes. We have seen how data members and member functions can be written in structures and classes. We have seen how to access these members and also how to control access to these members and we have also studied about constructor and destructor functions for classes and similarly for structures. In this lecture, we are actually going to take a closer look at constructors and we are going to study some example usage of constructors in C++ programs. Much of this lecture is motivated by the treatment in the book and introduction to programming through C++ by Abhiram G. Ranade published by McGraw Hill Education in 2014. All examples taken from this book are indicated in the respective slides by the citation AGR book. Now recall that in our study of constructors and destructors, we had basically said that given a class, the constructor function for that class is going to get invoked automatically whenever an object of the class is allocated. In fact, the object is going to be allocated first and then the constructor is going to be invoked on that object and we are also seen that this is actually a very convenient way to initialize data members of an object of the class. We had also studied that a destructor function of a class gets invoked automatically whenever an object of the class is deallocated. In fact, the destructor function is first invoked on the object and once the destructor function returns having completed its task, then the object is deallocated. And we had also studied that this can be a convenient way to do some bookkeeping and cleaning up operations before actually deallocating an object. Here is an example of the constructors and destructor of the class v3 for representing three dimensional vectors that we have already studied in the last few lectures. So, the class v3 has three data members x, y and z each of type double and all of these are private. It also has some private member functions and some public member functions. Specifically, we are interested in the two constructor functions for the class v3 that we had studied in our last lecture and also one destructor function for the class v3 that we had studied earlier. Recall also that with studying about member functions, we had said that if a member function of a class is public, then that member function can be invoked on a receiver object of the same class anywhere inside the actual main program or anywhere inside another function in your program. So, for example, here is the class v3 and this has several public member functions. Here I have shown just two such public member functions named scale and print length and here is our main program in which I am first allocating an object a of class v3 and of course, the constructor function for v3 is going to be invoked on the receiver object a with the parameters 1, 2 and 3 as shown here and then once I get that object a, I am going to invoke the member function scale on the receiver object a with the parameter 4.0 and the resulting object is going to be stored as object b of class v3 and then I am invoking the member function print length on the receiver object b. Note that both scale and print length are public member functions of the class v3 therefore, I can freely invoke them in the main program on receiver objects of the appropriate class wherever I want. The question now is that since we have seen in the last lecture that constructors and destructors are also public member functions of a class can we do the same with constructors and destructors? Can we invoke them explicitly in the main program or in some other function just like other public member functions? Well, the answer to that is in C++ it is usually an error to call a destructor explicitly in a program. In fact, the destructor must get automatically called whenever you are trying to deallocate an object of the class you cannot call it explicitly in your program. On the other hand it is actually ok to call a constructor explicitly in a program. For example, here is a small program fragment where I have first allocated an object a of class v3 and initialized it through its constructor with the values 1.0, 1.0 and 1.0 and now I am calling the member function sum on the receiver object a and as parameter to the member function sum I am passing the result of invoking the constructor of the class v3 explicitly with the parameters 2.0, 2.0 and 2.0. So, this is an example of an explicit constructor invocation. I was not trying to allocate a variable or object of the class v3 over here. I directly invoked the constructor of the class v3 and passed it appropriate parameters and I expect to get here a temporary object of class v3 with its data members initialized appropriately by the constructor function. So, if you look at this explicit constructor invocation over here this looks just like any other normal function call. I have a function name and I have the values of the parameters. However, in this case the name of the function is actually the name of the constructor function and we have already studied that the name of a constructor function is really the same as the name of the class of which it is a constructor. So, really this explicit constructor invocation happens by using the name of the class of which this is a constructor and of course these are the parameters passed to that constructor function and what this is going to do is it is going to create a temporary object of class v3. It is going to invoke the constructor on that object with these parameters and whatever that constructor will initialize that object to be that resulting object will be now used as a parameter of the member function sum called on the receiver object a. Here is an interesting implementation of the member function sum using an explicit constructor invocation in the class v3. This example is taken from AJR book. So, recall that in the class v3 we also needed to define a public member function sum which was supposed to add the x, y and z components of the object on which it has been called to the x, y and z components of a v3 object that we pass to the member function as a parameter. So, here is one interesting way of doing it. I first figure out what the x, y and z components of the result v3 object should be. So, that should be x plus p dot x, x being the x coordinate of the receiver object on which this function has been called and p dot x is the x coordinate of the parameter b that has been passed to this function and similarly y plus p dot y and z plus p dot z. So, once I have figured out the x, y and z coordinates of the resulting sum what I am going to do is I am simply going to call the constructor function of the class v3 with these three as parameters. What that is going to do is it is going to allocate a temporary object of class v3 invoke the constructor function on that temporary object with these three parameters and therefore the temporary object will have its x, y and z coordinates initialized to the desired x, y and z coordinates of the sum and that temporary object of class v3 will be now returned by this member function sum. Now in C++ you can also specify default values of parameters for constructors and not only for constructors, but also for other member functions. For example, here is a constructor definition in which here is a constructor definition for the class v3 in which I have taken three double valued parameters and for each of these double valued parameters I have also specified what the default value of that parameter is. So, if in a particular invocation of the constructor a particular parameter is not specified I can use the default value for that parameter and in the body of the constructor function I basically assign the values of vx, vy and vz to the data members x, y and z. Now with this constructor definition where default values are specified all of the following constructor calls are legitimate are correct. So, in this constructor call v3a I have not specified the value of any of the three parameters. So, I can basically take the values of the default parameters. So, this is equivalent to v3a 0.0, 1.0, 2.0. In this case I have specified the value of only one parameter. So, that will be taken to be the value of the first parameter and the values of the second and third parameters will be taken to be the default values. So, here I am basically calling the constructor with parameters 1.2, 1.0 and 2.0. In the third case I have specified values of two parameters. So, these two parameters will get their values directly while the third parameter will get its default value 2.0 and the final case of course I have specified the values of all three parameters. So, all the three parameters will get their values directly from the parameters passed in this constructor invocation. Now in C++ there is also another very interesting way by which you can specify how different data members of the receiver object are going to be initialized before the execution of the constructor begins. For example, here is the class v3 once again having three data members x, y and z. Here is my constructor function which takes three parameters vx, vy, vz and after that I have put a colon here and then I have specified an initialization list here. What does this initialization list look like? It specifies the name of a data member and then within parenthesis it specifies what should be the initial value of that data member in terms of the parameters passed to this constructor function. So, here it is saying that the initial value of x should be the value of vx passed as parameter to this constructor function, the initial value of y should be the value of vy passed to this constructor, the initial value of z is the value of vz passed to this constructor and indeed the values with which you are going to initialize the data members could be any expressions in the parameters passed to the constructor function. Here is a very interesting example of usage of initialization lists from AGR book. Here I have the class point which represents a point in two dimensional space. So, it has x and y coordinates and it also has a constructor function which takes in two double valued parameters and assigns them to x and y and then I have a class disk which has a member named center of type point and a member named radius of type double. So, the constructor function for disk can take in three parameters x, y and r all of which are double valued and then it can basically say that the data member center is to be initialized with whatever the constructor function for point returns when you invoke that constructor function with the values with the parameters x and y of the constructor function for disk and similarly the data member radius is going to be initialized with the parameter r passed to the constructor function for disk. So, here you can see a data member is initialized with the result of another constructor function to which I am passing the parameters straight away from the parameters of the constructor function for disk. So, in summary in this lecture we took a closer look at constructor functions we saw how to invoke them explicitly in your program. We saw how to specify default values of parameters of constructor functions and the same mechanism also works for specifying default values of parameters of other member functions of classes and we also saw the usage of initialization lists to specify initial values of different data members of objects before the constructor function actually starts executing. Thank you for your attention.