 Hello and welcome back to this lecture on simple operations on structures. Here is a quick recap of some of the relevant topics we have already studied. In the last lecture, we had seen a brief introduction to object oriented programming and we had also seen how to define structures as examples of objects in C++. In this lecture, we are going to study how to access different members of a structure and we are also going to see how to initialize and copy structures. Some examples in this lecture are from the book, An Introduction to Programming through C++ by Abhiram Jiranade published by McGraw-Hill Education in 2014. All such examples will be indicated in slides with the citation AGR book. Now if you recall from the last lecture, we were trying to design a simple library information management system which was motivated by the discussion in AGR book and the simple information management system was supposed to keep track of books checked out by patrons in the library, books returned by patrons in the library and books claimed by patrons in the library. So to recap, here is how we wanted our system to work. Every patron would have a unique numerical ID, every book would have a unique accession number and a patron can check out up to 3 books at any time and if a patron X has not already checked out 3 books then she is allowed to claim one book that is already checked out by some other patron Y and what this would mean is that when Y returns the book it would be held for X and would not be lent out to others and of course a patron can return a book that she had checked out sometime earlier and this being a benevolent library there were no late charges. Now if you also recall from the previous lecture that in trying to understand how to design the library information management system, we observed that there were 2 key entities that were participating in the functioning of the library information management system and one of these entities was the patron entity, the other was the book entity and all the different operations were basically interactions between these 2 entities. So if you recall from the previous lecture we had defined these 2 entities, book and patron as C++ structures and each of these structures had several information items stored within them. For example the book structure had the title of the book, the authors of the book, the book's price, its accession number, checkout status and the ID of any claimant of the book stored within the structure and similarly the patron structure had the name of the patron, the address of the patron, the patron's unique ID, the count of the number of books checked out by the patron and the accession number of any book that might have been claimed by the patron. And in the last lecture we saw that these individual information items that are stored in a structure are basically stored in what are called members of the structures. So for example title would be a member of the structure book and similarly num books checked out would be a member of the structure patron. We have also seen in the last lecture that in C++ it is possible to define variables and arrays of structure types just like we can declare variables and arrays of primitive data types like integer, double, float, character and so on. So as an example this declaration is saying that library shelf is an array of size 1000 where each element of this array is of the structure type book. Similarly this declaration is saying that there are two variables named my choice and your choice and each of these variables have the structure type book. This declaration says that library patron is an array of size 200 where each element of this array is of structure type patron and similarly current patron and previous patron are two variables each of them having the structure type patron. Now what we want to ask is that suppose this is the definition of the book structure and suppose I have declared a variable my choice of structure type book. Now how do we access a particular member let us say the member named price of this object or entity named my choice which is of structure type book. How do I access a particular member of a structure? Now it turns out that C++ actually provides a special operator called the dot operator for this purpose. So for example if you say my choice dot price what it would mean is look at the object my choice look at the structure type of this object here the structure type is book and then access the member named price of the object my choice which is of structure type book. So when we say my choice dot price we are really accessing the price named member of my choice which is an object of this structure book and because my choice is an object of structure book therefore it will have all of these members and therefore we can indeed access the member named price. Now once you use this dot operator and you have something like my choice dot price you can actually use this description my choice dot price in your C++ program like any other double variable why am I saying it is a double variable because the member named price is of type double in the structure book. So for example here are some examples C++ program statements which are using my choice dot price for example I could read in the value of my choice dot price just like I can read in any double variable using CN greater than greater than my choice dot price I could add 20 to the value of my choice dot price by just saying my choice dot price plus equals 20 note that this is just like adding 20 to the value of any double variable and similarly I could print out the value of my choice dot price as a double variable using the C out statement C out less than less than and then the string rupees standing for Indian rupees less than less than my choice dot price and note once again that the usage of my choice dot price here is exactly the same as I would do if I were to treat my choice dot price not as a member of a structure but as any other double variable. So this is important once you use the dot operator what you get can be treated just like any other variable of the corresponding data type of course we could access arrays as well using the same dot operator for example if this is the structured pattern and if I have defined a variable current pattern of the structure type pattern then when I use current pattern dot name I am basically accessing the member named name in the object current pattern whose type is a structure named pattern the structure type pattern is the type of current pattern and I am accessing the member named name in that structure. Now it turns out that name here is actually a character array therefore current pattern dot name can be used in a C plus plus program like any other character array just like earlier we had seen that current book dot price could be used like any other double variable so similarly current pattern dot name can be used like any other character array. So for example here is some program statement which is using current pattern dot name as a character array so what this program statement is doing is it is saying that if the first character in the character array current pattern dot name is capital S then we are going to print out pattern name and then we are going to print out the entire member name of the object current pattern which is of type structure pattern so note that the usage of current pattern dot name here is just like that of any other character array similarly the usage of current pattern dot name here is just like that of any other character array I can access the zeroth element of it I can compare it with another character I can print out the entire string in the character array current pattern dot name well so far good for accessing members of different structures now we would like to ask is it possible to initialize structures just like we can declare and initialize variables of simple data types so for example here I am declaring a variable of type integer and initializing it to 0 here I am declaring a variable command of type character and initializing it to the lower case character X so the question is can we do similar initialization for structures and it turns out that we can do this so here is our favorite structure pattern and suppose I declare a variable current pattern of type structure type pattern and suppose I initialize it like this now what does this initialization do remember current pattern is a variable of structure type pattern and the structure pattern has several members in it so when I am initializing this variable I have to specify an initialization for each of these members so here I am specifying an initialization for name and initialization for address and initialization for unique ID and initialization for num books checked out and an initialization for claimed book accession number the important point to notice that the order in which you specify these initial values are exactly the same as the order in which the members are defined within the structure so after this initialization we are going to have the current pattern objects members as the member named name will be initialized to Shashi Dave the member named address will be initialized to IIT Bombay comma space India the member named unique ID will be initialized to 2345 the member named num books checked out will be initialized to 0 and the member named claimed book account accession number would be initialized to minus 1 once again the order in which these initialization values will be taken is exactly the same as the order in which the members are declared in the definition of the structure pattern so you would recall that for primitive data types we can also copy the value of one variable to another variable of the same data type so for example here I am declaring two variables I and J of type integer I am initializing the value of I to 27 and then copying the value of I to J just by saying J is assigned I the question is since we can do this for the primitive data types like integer or character can we do a similar thing for structures can we copy one object of a particular structure type to another object of the same structure type and the answer to that is yes we can indeed do that so for example here current pattern and prep pattern are both objects of the structure type pattern here I am initializing current pattern to whatever we had seen earlier and notice this assignment statement this is assigning current pattern as a structure of type pattern to previous pattern which is also an object of the same structure type so what this assignment statement will do is each member of the object current pattern will be copied to the corresponding member of the object previous pattern as a result of executing this assignment statement so if current pattern before copying had these values for its different members prep pattern after copying will have exactly the same values for the corresponding members so note that the value of name here is the same as the value of name here the value of unique ID here is the same as the value of unique ID here and so on so therefore an assignment operator can also be used with structures just like we can use it for simple data types so in summary in this lecture we looked at the dot operator to access members of structures we saw how to initialize structures and we also saw how to copy structures thank you.