 Hello and welcome. In this lecture we are going to discuss about constructor and destructor functions of classes and structures. Here's a quick recap of some of the relevant topics we've already studied. We've studied about structures and classes, about data members and member functions of structures and classes. We've studied how to access members in programs and we've also studied how to control access to members using the public and private keywords. In this lecture we're going to discuss about two special member functions of every structure and class. These are called constructor functions and destructor functions. Much of this lecture is motivated by the treatment in the book, an introduction to programming through C++ by Abhiram G. Ranade published by Micro Hill Education in 2014. Recall that when we discussed about member functions and their usage, we said that a class can have public as well as private member functions. We had discussed about a class V3 for representing three-dimensional vectors in Cartesian coordinate and let us say that we're going to augment the class V3 we have studied earlier with a new member function called print length whose job it will be to print the Euclidean length of a three-dimensional vector. So here we have three data members x, y and z, all of type double and all of them private. We have the usual member functions which are public. These member functions were studied in an earlier lecture and now here's the new member function print length which computes the Euclidean length of the vector and prints it. How does it compute the Euclidean length? It actually calls another member function called length which is declared as private. However, any private function of a class can be invoked from any other member function of the class. So therefore print length can make use of an invocation of length to compute the length and print it out. Here's the main function which makes use of this class definition. Here's a variable a of class V3 and here is a variable PTR whose type is a pointer to the class V3. After some code here, suppose we execute a.printlength. I'm basically invoking the member function print length which is public and therefore can be invoked from main on the receiver object a and therefore this is going to print the Euclidean length of a. Then I'm allocating a new object of class V3 and storing the pointer of that in this variable PTR and of course if new failed and PTR is null then we return minus 1. Otherwise after some code here I invoke the member function print length of the object pointed 2 by PTR by using the arrow operator. Once again print length public I can invoke this and finally delete the pointer and return C. So here we basically invoke the public member function print length both using the dot operator and the arrow operator. Now in C++ there are two special member functions that every class and structure has and we're going to look at each one of them in turn. The first one is called constructor function. So this is actually invoked automatically whenever an object of the class is allocated and in fact this is a very convenient way to initialize the data members of the class. This acts just like any other member function with some minor differences which we will speak about. It accepts optional input parameters and it can be used to perform computational tasks other than initialization as well as we will just see. So here is an example of a constructor of the class V3. So note that I have put this constructor functions definition within the public block and what does this constructor function do? It takes three double-valued parameters and it assigns them to the private data members X, Y and Z. Note a few things about this constructor function. This looks just like any other member function of the class V3 except that this V3 that we have written over here is actually not its return type but this is the name of the constructor function. So the name of the constructor function is the same as the name of the class and in fact the constructor function does not have a return type YC++ convention. It takes optional input parameters as we have already seen and as is the case here it is mostly used for initialization. So this is one constructor function of the class V3. Here is the second constructor function of the class V3. So interestingly note that second constructor function once again has the same name as the name of the class so it has the same name as the first constructor function does not have a return type but in this case it does not accept any input parameters unlike the first constructor function and of course in this case this constructor function simply initializes the three data members to 0.c. The important point to note here is that although these two constructor functions share the same name they have different type lists of parameters and in fact in C++ the same class can have multiple constructors as long as each one has a distinct list of parameter types just like we had for V3. So when allocating an object of the class V3 which of this constructor function should be invoked well the types of the parameters that are going to be passed to the constructor when allocating the object will determine which of this constructor functions is invoked. So if I declare a variable or object my obj1 of class V3 I have not specified the values of parameters to the constructor function here so therefore this constructor function will be invoked because it does not require any parameters. On the other hand if I was executing this statement where this variable myobj2 which is of type pointer to the class V3 is assigned the value returned by new V3 1.0 2.0 3.0 then this basically allocates an object of class V3 in the heap and it passes these three parameters as the parameters to the constructor of that object. So since we now have three double value parameters as parameters to the constructor therefore this constructor function of the object of class V3 will be invoked. It is also important to note that when I am allocating an object and invoking the constructor function then the allocated object itself serves as the receiver object for the constructor function. Recall that whenever we call any member function of an object of a particular class we need a receiver object for that member function so in this case the allocated object on which the constructor function is going to be invoked itself serves as the receiver object. Here is an example of how we might use constructors in a C++ program so this is the class V3 with the two constructor functions that we've just seen. In this program I have first declared a variable or object A of class V3 and passed these three double values as parameters. So since I have passed three double value parameters therefore this constructor function is going to be invoked and the data members x, y and z of the object A will all be initialized to 0. However they will be initialized to 0 by this constructor function not by this constructor function. Note the public declaration here why do I need to put the public declaration here this is because the constructor function will be invoked on the object A from the main function. Now recall that we had said that if a member function of an object has to be invoked from the main function or from any other function in the program then that member function must be declared as public. So since the constructor function is being invoked on the object A from the main function here therefore this constructor function must be declared as public. Here the object B of class V3 is allocated but there are no parameters passed to its instructor therefore this constructor function is going to be invoked and all the data members of the object B will be initialized to 0. In this statement an object of class V3 is dynamically allocated in the heap and these three double value parameters are passed to its constructor. So once again this constructor function will be invoked and finally in this case an object of class V3 is dynamically allocated on the heap but no parameters are passed to its constructor function. So this constructor function will be invoked. So as you can see that although there are two constructor functions for the same class there is really no ambiguity about which constructor function is being invoked when an object of that class is being allocated. Just like constructor functions there is also another special member function of every class in structure this is called the destructor function. This is invoked automatically whenever an object of the class is deallocated and in fact there's a very convenient way to do some bookkeeping and cleaning up before you deallocate an object. A destructor function by definition does not accept any parameters and it can be used to perform other computational tasks as well before deallocating an object. So here is an example of a destructor function of the class V3. So in this destructor function we first figure out if the length of the three dimensional vector is zero if so the destructor function simply prints zero vector with three exclamation marks on standard output and returns. So this is a fairly simple-minded destructor function. Now note some characteristics of this destructor function just like a constructor function this is also a member function of the class V3 just like a constructor it does not have any return type and the name of the destructor function is actually obtained by prefixing a tilde to the name of the class. A destructor function does not accept any input parameters and it is mostly used for bookkeeping and cleaning up before the allocation of objects but here it is doing something very trivial there is no bookkeeping on cleaning up to be done here so it simply checks the length of a vector of the vector and if it is zero it prints out zero vector. Note that multiple destructors of the same class are not allowed in C++. Here is an example of how we might use the destructor function of the class V3 here I have my main program here is the same class V3 with the destructor function highlighted here. Now here I have allocated an object A of class V3 with the data members initialize to 1.0, 2.0, 3.0 through the constructor function of course. Here in this block I have allocated another object B of class V3 and there are no parameters passed to its constructor function therefore its data members will be initialized to 0 as we have seen earlier and now I'm copying B to A so now the data members of A are also 0 and after that I come out of this block since this object is defined only within this block so now the object B will get deallocated and because we had all its data members set to 0 therefore the destructor function of the object V will now print out zero vector. In this statement a new object of class V3 is dynamically allocated on the heap and through its constructor function its data members are set to the values 1, 1 and 1. Here I'm copying that dynamically allocated object to A so now the data members of A are also set to 1, 1 and 1 and then finally I'm deleting the dynamically allocated object of class V3 but because its data members were initialized to 1, 1, 1 therefore when the destructor function is called we figure out that the length is not zero and zero vector does not get printed out. Finally after we execute return zero the object A itself is deallocated but before deallocating that object the destructor function of V3 is again called once again since the length is not zero zero vector does not get printed out so there are three deallocations here one here where zero vector gets printed out the other here and the third here where the zero vector string does not get printed out. Once again note the public declaration here this is needed because whenever we are deallocating the objects we are prior to deallocation we are calling the destructor function from the function main and therefore unless the destructor function is public we cannot call it from the function main. So in summary in this lecture we looked at constructor and destructor functions of classes we also saw simple usage of these functions in C++ programs. We'll see more complex usage later. Thank you for your attention.