 Hello and welcome back. In this lecture we are going to continue with our study on structures. Here is a quick recap of some of the relevant topics we have already studied. We have seen a brief introduction to object oriented programming. We have seen that structures can be used as collections of variables of possibly different data types. We have seen how to access members of structures and we have also seen some examples of programming using simple structures. In this lecture we are going to continue our study of structures. We will start with some common conventions when speaking of structures. Then we will see some more features of structures. Specifically we will see how structures can be used as members of other structures. We will also see how to initialize members of structures inside other structures. We will also look at some disallowed structure definitions and we will see how the visibility of structure definitions in C++ programs is determined. Some examples in this lecture are from the book An Introduction to Programming through C++ by Aviram G. Ranade published by McGraw Hill Education in 2014. All such examples will be indicated in slides with the citation AGR book. Now, let us start with some common conventions when speaking of structures. Here I have a simple structure definition. The name of the structure type is mystruct type. It has two members named x and y, x is of type n and y is of type character. And here is a variable, my variable which is declared to be of the type mystruct type. Now, if I say this English sentence, the definition of structure mystruct type is given here. You will notice that the usage of structure over here is really to refer to a specific structure type. In this case mystruct type. So, when I say the definition of structure mystruct type, I am really using structure to refer to the specific structure type mystruct type. Alternatively, suppose I told you that the structure my variable is used in the program. Remember, my variable is really a variable of the structure type mystruct type. So, we will often call such variables which are really objects of a given structure type as structures themselves. So, here I have said the structure my variable is used in the program. So, here by structure I am referring to a specific object of a structure type. This is very common when we talk about programs. We often talk about variables of a structure type as structures themselves. Now, consider this third English sentence. A structure of type mystruct type needs 5 bytes of storage. So, in this sentence this word structure over here really refers to an arbitrary object of type mystruct type. So, as you can see that when we speak of structures in C plus plus depending on the context the word structure may either refer to a specific structure type. It may refer to an object of a specific structure type or it may refer to an arbitrary object of a given structure type. Now, here is a C plus plus assignment statement that is assigning the value 12 to the member x of the structure my variable. Now, here the member x of structure my variable for all practical purposes in the program can be treated like an object or variable of type integer. Why is it of type integer? Because in the definition of mystruct type the member x has been defined to be of type integer. So, we will often refer to members of structures as objects of a certain specified type. Now, let us see how we can define slightly more complex structures than what we have seen earlier. So, here is an example which is motivated by the discussion in the book AGR book. This is about representing points and discs in two dimensional space. Now, every point is going to have an x coordinate and a y coordinate. So, we may want to define a structure type named point which has two members x and y both of type double. And here is a pictorial representation of how an object of structure type point might look like. It has two members x and y each of them of type double. A disc will have a center which is itself a point and it will also have a radius. So, we can think of a structure disc which has a center of structure type point and it has a radius of type double. So, here is how an object of structure type disc might look like. It has a member radius of type double and a member center of structure type point. Now, because center is of structure type point, therefore within center I should be able to see a member x of type double and a member y of type double. Now, here I have shown these two structure definitions and let us say I have a variable p1 of type point and a variable d1 of type disc. What it really means is I have these two objects p1 which has two members named x and y and d1 which has two members named center and radius. The member named center itself is like an object of structure type point. So, it has two members named x and y. Now, I could use these assignment statements p1.x is assigned 0.5 and p1.y is assigned 0.9 to basically assign the values 0.5 and 0.9 to the two members of p1. Subsequently, if I say d1.center is assigned p1, then this entire structure is going to be copied to the corresponding member of d1 and remember this member of d1 named center is of the same structure type as p1. Therefore, from our earlier lecture on how one structure can be assigned to another structure of the same type, you will recall that each member of p1 will be copied to the corresponding member of the member center of d1. So, 0.5 will get copied here, 0.9 will get copied here and finally, if I say d1.radius is assigned 3.2, then the member radius of the object d1 will get the value 3.2. Now, in this slide, I am showing that I have only one object d1 of type disk and let us say I use this assignment statement d1.center.x is assigned 0.5. Let us see what this means. Well, d1 is an object of type disk. d1.center is referring to the member named center of the object d1. Now, the member named center of the object d1 can be used as an object of type point. It can be thought of as an object of type point. So, therefore, when I say d1.center.x, I am really talking about the member x of the member center of the object d1 and of course, this member x of the member center of the object d1 can be used as an object of type double. So, I can make this assignment over here. Similarly, if I say d1.center.y is 0.9, then 0.9 gets copied to center.y, where center is a member of d1. Of course, 0.5 was copied to center.x, where center is a member of d1 and 3.2 gets copied to the member radius of d1. So, here is one way by which we can initialize the various members of structures in structures in a C++ program. Here is another way by which the initialization can be affected. Here I am saying d1 is an object of type disk and it is assigned this initial value. How do I read off this initial value? Within the outermost pair of braces, I am going to specify the values, the initial values of the different members of the structure type disk in the same order in which they appear in the definition. So, the first entry here which is once again something enclosed within a nested pair of braces is the initial value of center. Note that center itself is like an object of structure type point. So, the initial value of center can be specified by specifying the initial values of the members x and y and that is exactly what happens here. So, 0.5 becomes the initial value of x in the member center of the object d1, 0.9 becomes the initial value of the member y in the member center of the object d1 and 3.2 is the initial value of the member radius of the object d1. Similarly, here is another initialization where I have declared an object of object d2 of type disk, but I have also specified that this is a constant object and I have initialized the various members of the structure d2 as well as of structures inside the structure d2 in the same way that I had done earlier and that is illustrated here. Note that because I have declared d2 to be a constant. Therefore, I cannot assign values to any of the members of d2 within my program. This is just like any other constant declarations we have seen earlier. Well, now that we have seen how structures can be used inside the definition of another structure, we must be a bit careful that we do not use a member of the same structure type as the original structure type which I was trying to define. So, in this case if struct type 1 is what I was trying to define, I cannot have a member of type struct type 1 within this definition. This is not allowed in C plus plus and if you just think about it, you will realize that the storage required for an object of such a structure type, struct type 1 if we were to allow this definition would indeed become infinite because an object of type struct type 1 must have space to store an integer and it must also have space to store another object of type struct type 1. Now, that object must have space to store an integer and it must also have space to store another object of type struct type 1 and this way you will continue add infinitum and indeed the storage required would be infinite. So, this is disallowed in C plus plus. Well, now we have seen how we can define more complex structures than we had seen earlier. The question is, where should we define structure types in our program file? Well, the answer is quite simple. If a structure type is going to be used only inside one function, it can be defined in the body of that function. For example, here is a function do something with disks and it is only inside this function that I am going to use the structure types disk and point. So, I can define the structure types within the body of this function and then write some code that does something with points and disks. On the other hand, if a structure data type is going to be used in multiple functions, it must be defined outside the body of those functions and before those functions appear in the program file. So, here there are three different functions which are doing something with points and disks. The third function is actually taking in two parameters of types, points and disk respectively. So, therefore, the definitions of structure point and structure disk must appear outside the bodies of these functions and before these functions are defined in the program file as is shown here. So, in summary in this lecture, we looked at some common conventions when speaking of structures. We looked at some additional features of structures beyond what we have studied earlier. Specifically, we saw how structures can be present as members in other structures and we have also seen how to access and initialize members of such structures inside structures. We have seen that it is illegal to have a structure with a member of the same structure type and we have also studied about the visibility of structure types in a C++ program. Thank you.