 Hello and welcome back. This is the first part of a multi-part lecture which illustrates how structures can be used when programming in C++. Here is a quick recap of some of the relevant topics that we have already studied. We have seen a brief introduction to object oriented programming and we have seen how we can define structures as examples of objects in C++. We have also studied how to access various members of structures and how to initialize and copy structures. In this lecture, we are going to get our hands dirty and jump into C++ programming with structures. Some examples in this lecture are from the book, an introduction to programming through C++ by Abhiramji Ranade published by McGraw-Hell Education in 2014. All such examples will be indicated in slides with the citation AGR book. If you recall from the past few lectures, we have been trying to design a simple library information management system which is inspired by the discussion in AGR book. And in our simple library information management system, we want every pattern to have a unique numerical ID. We want every book to have a unique accession number and patterns and books where the two primary entities that are going to be present in the working of our library information management system. And there are three kinds of operations that we have discussed. One is the checkout operation in which a pattern checks out a particular book and let us say that we are going to allow a pattern to check out up to three books at any time. Then there is the claim operation and this operation is as follows. If pattern X has not already checked out three books, then she can claim a book that is checked out by some other pattern Y. And what this means is that when pattern Y returns the book, it is going to be held for pattern X and will not be lent to others. We are going to impose the restriction that a pattern can make a claim for at most one book. So, if she has already made a claim for a book, she cannot make a claim for that book again, for another book again. And of course, we have the return operation where a pattern can return a book that she had checked out at some earlier point of time. If you also recall from the previous lectures, we had identified books and patterns as the major entities in the operation of the library information management system. And we had decided to represent the entity book as a C++ structure. So, here is the structure book and as various members of the structure, we have the title of the book, the authors of the book, the price of the book, its accession number, checkout status and the ID of any pattern who might have made a claim to this book. And the collection of books in the library would be represented by an array called library shelf. The size of this array is 1000. We had decided that there are going to be 1000 books in the library. Therefore, the size of this array is 1000. And each element in this array is an object of type structure book. We are going to assume for purposes of the programs discussed in this lecture that for every element of this array library shelf, every such element is such a structure. And we are going to assume that for every such element, the member's checkout status and claimant ID have been initialized to false and minus 1 respectively. The checkout status of a book being false denotes that the book has not yet been checked out. The claimant ID of a book being minus 1 denotes that there are no claimants as yet for that book. We had also decided that we are going to have a structured patron which is going to store all the information that is relevant to a patron. So, as members, we had name, address, unique ID, the number of books checked out by this patron and the accession number of any book that might have been claimed by this patron. The collection of all patrons of the library is going to be represented in this array library patrons which is of size 200. Let us assume that we are going to allow 200 patrons for this library. And each element of this array is an object of type structured patron. And once again for purposes of programming in this lecture, we are going to assume that for every element in this array library patrons, every such element being a structure. We are going to assume that for every such element, the fields, num books checked out and claimed book accession number have been initialized to 0 and minus 1 respectively. Num books checked out for a patron being 0 means that the patron has not yet checked out any book or whatever the patron has checked out, she has already returned all of them. And the claimed book accession number for a patron being minus 1 means there are no pending claims that have been made by this patron for any book. Now, let us see how the C++ function for checking out a book might look like. So, what is this functionality of checking out a book? We want a particular patron, say current patron of type patron to check out a particular book, say current book of type book. What is involved in this process of checking out? Well, we have to first check if current patron has already checked out 3 books. If so, we cannot let her check out any additional book. So, we should print an appropriate message and return. Otherwise, we should try to see if current patron can be allowed to check out the book that she wants to check out. But if that book has already been checked out by somebody, then of course, current patron cannot get that book. So, we must print an appropriate message and return. Now, if current patron has not already checked out 3 books and the book she wants to check out has not already been checked out by somebody, can we directly go ahead and let current patron borrow that book? It turns out that we have to do one more check. Remember, we had the system of claims in our library. So, we have to check if the current book has not already been claimed by a different patron. And if so, then we can allow current patron to check out current book in which case we increment the value of current patron dot num books checked out because current patron now has one more book checked out against her ID. And we have also got to update the check out status of current book to true. Note that in this case, all the updates that are happening to the various members of current patron and current book should persist after the function for checking out a book returns. Why is it so? Because if we have set the check out status of current book to true, then after the function returns, we want to remember this value. So, that if another patron comes to check out the same book, then we can tell that patron that this book has already been checked out. Similarly, after we have incremented num books checked out of current patron, we want to remember this value after the function for checking out returns because if the same patron comes again and tries to check out another book, then we must first check whether she has already checked out 3 books or not. So, since we want these updates to the different members of current patron and current book to persist after the function for checking out a book returns, therefore, this is suggestive that we need functions with parameters passed by reference. So, you would need a function which would take current patron and current book as parameters, but we would like to pass these parameters by reference. So, that whatever updates this function is doing to the members of current patron and current book, these are actually visible to the calling function after the function for checking out a book returns. So, this is how our program might look like. This is the main program and in this I am calling this function check out book in which just as an example, I am passing these two parameters library patrons i, library shelf j, presumably the code here has figured out the right index j in the array library patrons to figure out who current patron should be and also the right index j in the array library shelf to figure out what current book should be. Now, once these two objects, these two structures are passed as parameters to check out book, check out book is going to take these parameters as parameters passed by reference, recall this ampersand symbol that we had used earlier to pass parameters by reference. So, check out book takes these two parameters by reference and then what does it do? It is going to implement the code for checking out a book. So, the precondition for this function will be that the members of current patron and current book are properly initialized, there are no garbage values and as post condition we are going to try to ensure that if current book is indeed lent to current patron, remember there are several checks that we will have to do before we can let current patron borrow current book. But if current book is indeed lent to current patron, then we would want the members of current patron and current book to be appropriately updated and because we are passing current patron and current book as parameters by reference. Therefore, all of these updates to the members of current patron and current book are going to be visible in the calling function and they are going to persist after the function check out book returns. So, how does the code for the function check out book look like? We are first going to check if the number of books checked out by current patron is less than 3, if so we are going to proceed and try to see if current patron can check out current book. But if the number of books checked out by the current patron has already reached the number 3, then we have to print out an appropriate message saying sorry 3 books have already been checked out by the current patron. Note here that I have used current patron dot name to print out the name of the current patron. Recall that in the structure patron, name is a member which is basically a character array. So, when I am printing out current patron dot name, I am saying print out the character array basically the string stored in the character array whose name is name in the structure patron and current patron is an object of that structure. Now, how does the code for this part which is when the number of books checked out is indeed less than 3 look like? Well, here is how it might look like even if the number of books checked out by current patron is less than 3, we have to first check if the book has already been checked out by somebody. We have to first check whether the check out status of current book is indeed true and if so it means that the book has already been checked out. So, we have to say sorry this book with this accession number has already been checked out. Once again note that I am printing out a string for the book title by accessing the member title of the object current book of the structure type book and similarly I am printing out the accession number of the book by accessing the member acts num of the object current book of structure type book. Well, if the book has not already been checked out then what should we do? Can we go ahead and let the patron check out the book? Not yet, we have to first see whether there is any claimant for that book and in particular if there is a different claimant from the book for the book compared to the current patron. So, we have to first check if there is any claimant for the book, claimant ID of current book being minus 1 means that there are no claimants, claimant ID being not the same as minus 1 means that there is some claimant and if there is a claimant then is the claimant the same as current patron if so we can allow the current patron to check out the book, but if the claimant ID is different from current patron's ID then we cannot allow current patron to check out the book because somebody else has already claimed this book. So, we are going to say sorry there is already a pending claim on this book by a different patron and we are going to return. Now, if none of these checks succeed then indeed we can let current patron check out current book and so that is what we are going to do now over here we will set the check out status of current book to true we will increment the num books checked out number of current patron by 1 and then if the current patron had indeed registered a claim for the current book then we are going to remove this claim now because now the current patron has been allowed to check out the current book. So, how do we figure out if current patron had indeed registered a claim for this book we are going to check if the claimant ID associated with current book is indeed equal to the unique ID of current patron and if so it means that current patron had registered a claim for current book and now this book is being lent to current patron. So, we are going to remove that claim how do we remove that claim by resetting claimed book accession number of current patron to minus 1 and by resetting claimant ID of current book to minus 1 and then we can return. So, note that since we passed current patron and current book by reference all these updates to the different members that happening are going to be visible in the calling function even after the function for checking out a book returns these are actually updates to members of the structures that were used as parameters in the calling function. So, in summary in this lecture we saw how to use structures in programming specifically we saw how structures can be passed as parameters to functions and how within those functions we can access members of structures passed as parameters. However, we are not done yet with our implementation of different functions that are needed in the library information management system the subsequent lecture is going to cover these. Thank you.