Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures Lesson xx In this module, we’ll introduce you to structures.

Similar presentations


Presentation on theme: "Structures Lesson xx In this module, we’ll introduce you to structures."— Presentation transcript:

1 Structures Lesson xx In this module, we’ll introduce you to structures.

2 Objectives Introduction to structures Structure definition
Structure declaration Structure initialization Structure containing arrays Strings in structures Array of structures Nested structures Our goal is to cover these basic elements of structures: definition, declaration, initialization. Then, we’ll go on to structure containing arrays, strings in structures and array of structures.

3 Arrays int a [10] = {3,6,4,1}; a a[0] 3 6 a[1] 4 a[2] 1 . . . a[9]
a[0] a int a [10] = {3,6,4,1}; a[1] a[2] Before we can talk about structures, we need to review arrays. int a[10] = {3,6,4,1}; is the declaration and initialization of array a. Shown here on the right is a picture of array a. The members of array a are a[0], a[1] all the way to a[9]. Arrays allow you to group items of the same data type together. Notices that all the elements of array contain ints. You cannot have an int in one element and a float or a char in another element. Structures, on the other hand, allow you to group items with different data types together. a[9]

4 Record Id : Age: 23 Sex: F In business, we might have a typical record that keeps track of an employee’s id,age and sex. Notice that the data types of the fields are different. Id is a long, age is an int, and sex is a char. A structure allows us to group unlike data types together and to create our own data types.

5 Structure Definition Syntax
struct tag { //data members }; This is how you create a structure definition: you write the keyword struct, then you put a structure tag which is the name of the structure. Inside a set of {}s you specify the data members. At the end of the structure definition, don’t forget to put a ;

6 Example of Structure Definition
struct student { //data members long id; int age; char sex; }; Here is an example of a structure definition. We are defining a structure called student which contains 3 data members. The id is a long integer, age is an int and we use a char for sex. See? a structure allows you to group different data types together. A structure also allows you to create user defined data types like student.

7 Structure Declaration
struct student { //data members long id; int age; char sex; }; int main ( ) int x; student s; return 0; } The statement in red: student s; is an example of how you declare a structure variable. Declaring a structure variable is like declaring an int variable. You put the data type and then the name of the variable. Now, s is a structure variable of the type student. In the next slide, we’ll draw you a picture of s and x.

8 Structure Picture struct student { //data members long id; int age;
char sex; }; int main ( ) int x; student s; return 0; } x s.id s.age s.sex s Here is a picture of what the variable x and s look like in memory. x is a variable that holds an int. s is a structure variable of the form student. s has 3 parts s.id, s.age and s.sex . They hold a long, int and char respectively. The . is called the member operator and is used to specify the different members of the structure.

9 Operations with Structure Variables
int main ( ) { int x = 55; student s1, s2; cin >> s1. id; s1.age = x; s1.sex =‘f’; return 0; } struct student { //data members long id; int age; char sex; }; Let’s look at the different operations that can be performed using structure variables. 1) cin >> s1.id; allows you to read a long integer into the id part of the structure s1. Notice that it is illegal to write: cin >> s1; In other words, you must use the . operator to specify the member. 2)s1.age = x; this statement allows you to do assignment. Since x = 55, 55 will be stored in s1.age. 3) s1.sex = ‘f’; stores the character ‘f’ in s1.sex. Basically, any operation that can be done on regular variables can be used with structure variables. The important thing to remember is that you must use the . operator with structure variables.

10 More Operations with Structures
int main ( ) { int x = 55; student s1, s2; cin >> s1. id; s1.age = x; s1.sex =‘f’; s2 = s1; if (s2 == s1) cout << “same”; return 0; } struct student { //data members long id; int age; char sex; }; There are a couple of other special operations that can be done with structure variables of the same data type: 1) you can assign the value of one structure variable to another as in: s2 = s1; This will do a memberwise copy. Another words, s1.id will be copied into s2.id, s1.age will be copied into s2.age and etc. 2) you can check to see if 2 structure variables contain exactly the same items as in: if (s2 == s1). The computer compares every bit in the structure and if they all match, the true branch will be executed.

11 Structure Initialization
struct student { //data members long id; int age; char sex; }; int main ( ) student s = { 06013,19, ‘M’}; return 0; } 06013 19 ‘M’ s.id s.age s.sex s Initializing a structure is similar to initializing an array. student s = {06013, 19, ‘M’}; is how you declare the structure variable s and initialize it at the same time. We have drawn a picture of the structure s for you. The entire structure is called s, the parts of s are: s.id, s.age and s.sex.

12 Structure Containing Array
struct student { //data members long id; int age; char sex; int g [3]; }; int main ( ) student s = { 60613, 19, ‘M’ , 92, 67, 85}; return 0; } 60613 19 ‘M’ s.id s.age s.sex 92 67 85 s.g[0] s.g[1] s.g[2] s Let’s say that we are interested in keeping track of a student’s id, age, sex and 3 grades. This student structure is one way to do that. Looking at the picture, we have the following data members: s.id, s.age, s.sex. Since the g data member is an array, we also have s.g[0], s.g[1] and s.g[2]. This is an example of a structure that contains an array.

13 Structure Containing a String
struct student { //data members long id; int age; char sex; char n [10]; }; int main ( ) student s = { 60613,19, ‘M’, “Sam”}; return 0; } 60613 19 ‘M’ s.id s.age s.sex s “Sam” s.n A structure that contains a string is similar to the previous slide where we had a structure that contained an array. This structure called s has 4 parts: s.id, s.age, s.sex and s.n. Technically, you have s.n[0], s.n[1] and etc. but, how often do you need to refer to the 5th letter of person’s name.

14 Array of Structures struct student { //data members long id; int age;
}; int main ( ) student s1, s2, s3; student S [ 100]; return 0; } S[0].id S[2].id S[0] S[0].age S[1].id S[1] It is often necessary to have an array of structures. Let’s say you need to keep track of 100 students. You could write something like: student s1, s2, s3; as we have in the 1st line of main. Of course, you’d have to list 100 variables. The next line: student s [100]; allows you to set up 100 structure variables of the form student. We have illustrated the 1st - 3 elements of the array s for you. s[0] has the parts s[0].id and s[0]age. S[1].age S[2] S[2].age

15 Multiple Structures struct time { int h, m; }; struct date int m,d,y;
int main( ) struct date dw; struct time hw; . . . } Let’s say that you are writing a payroll program and you needed to keep track of the date a person worked and how long the person worked. For example, Joe worked 3 hours and 20 mins. On 3/17/2012. You might set up 2 structures, one for the time and one for the date. In main(), we have declared 2 structure variable, dw is of the for date and hw is of the form time.

16 Nested Structures struct time int main(); { { int hh, mm;
}; struct date int m,d,y; struct tnd date da; time ti; int main(); { struct tnd ev; . . . } ev.da.y ev.da.d ev.ti.mm ev.da.m ev.da A nested structure is a structure inside of another structure. In this example, we have 2 structures, one called time and once called date. We have a 3rd structure called tnd which is a nested structure. tnd contains 2 data members which happens to be structures. It’s a lot easier to understand nested structures if we look at the picture. In main, we declare a structure variable called ev and it is of the form tnd. Looking at the picture,, ev has 2 parts: ev.da and ev.ti. Ev.da has 3 parts which are ev.da.m, ev.da.d and ev.da.y. ev.ti has 2 parts, ev.ti.hh and ev.ti.mm. Why would you want to do something like this? Thing of it this way, you want to record the time and date that an event occurred. ev.ti.hh ev.ti

17 Summary Introduction to structures Structure definition
Structure declaration Structure initialization Structure containing arrays Strings in structures Array of structures Nested structures We have covered a lot about structures in this module, structure definition, declaration, and initialization. Then we talked about structures that contain arrays and/or strings. Arrays of structures and nested structures are fairly common in C++. These are the basic building blocks and we’ll be using this material in the next modules to write some practical programs.


Download ppt "Structures Lesson xx In this module, we’ll introduce you to structures."

Similar presentations


Ads by Google