Presentation is loading. Please wait.

Presentation is loading. Please wait.

Struct COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender,

Similar presentations


Presentation on theme: "Struct COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender,"— Presentation transcript:

1

2 Struct

3 COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender, start year, … n Bank account:  account number, name, currency, balance, … n Address book:  name, address, telephone number, … * In database applications, structures are called records. l Recall that an array is a collection of variables of same type l A collection of variables of different types is a ‘structure’

4 COMP104 Struct / Slide 3 ‘Date’ example * A ‘date’ type: n Day (integer) n Month (integer) n Year (integer) * Example: struct Date { int day; int month; int year; } ; The new composite type “Date” structure has 3 members.

5 COMP104 Struct / Slide 4 Date christmas; christmas.day = 25; christmas.month = 12; christmas.year = 2003 Define new variable of type ‘Date’ Access to member variables using dot operator

6 COMP104 Struct / Slide 5 ‘struct’ definition struct { ;... } ; Each identifier defines a member of the structure. It is a ‘global’ definition!

7 COMP104 Struct / Slide 6 * Example: struct BankAccount{ string Name; int AcountNo[10]; double balance; Date Birthday; }; * Example: struct StudentRecord{ string Name; int Id; string Dept; char gender; }; The “StudentRecord” structure has 4 members. The “BankAcount” structure has simple, array and structure types as members.

8 COMP104 Struct / Slide 7 ‘struct’ usage  Declaration of a variable of struct type: ; * Example: StudentRecord Student1, Student2; Student1 and Student2 are variables of StudentRecord type. Student1Student2 Name Idgender Dept Name Idgender Dept

9 COMP104 Struct / Slide 8 Name Idgender Dept Chan Tai Man 12345 M COMP Member access (dot operator) * The members of a struct type variable are accessed with the dot (.) operator:. ; * Example: Student1.Name = "Chan Tai Man"; Student1.Id = 12345; Student1.Dept = "COMP"; Student1.gender = 'M'; cout << "The student is "; if (Student1.gender = ‘F’){ cout << "Ms. "; else cout << "Mr. "; } cout << Student1.Name << endl; Student1

10 COMP104 Struct / Slide 9 Chan Tai Man 12345 M COMP struct-to-struct assignment  The value of one struct type variable can be assigned to another variable of the same struct type. * Example: Student1.Name = "Chan Tai Man"; Student1.Id = 12345; Student1.Dept = "COMP"; Student1.gender = 'M'; Student2 = Student1; Student1 Chan Tai Man 12345 M COMP Student2

11 COMP104 Struct / Slide 10 Example of Nested structures struct Point{ double x, y; }; struct Line{ Point p1, p2; }; struct Triangle{ Point p1, p2, p3; }; Point P; Line L; Triangle T; (P.x, P.y) (L.p1.x, L.p1.y) (L.p2.x, L.p2.y) (T.p2.x, T.p2.y) (T.p1.x, T.p1.y) (T.p3.x, T.p3.y)

12 COMP104 Struct / Slide 11 Arrays of structures * An ordinary array: One type of data * An array of structs: Multiple types of data in each array element. 0 1 2 … 98 99

13 COMP104 Struct / Slide 12  Example: StudentRecord Class[100]; Class[98].Name = "Chan Tai Man"; Class[98].Id = 12345; Class[98].Dept = "COMP"; Class[98].gender = 'M'; Class[0] = Class[98];... 0 1 2 … 98 99 Chan Tai Man 12345 M COMP

14 COMP104 Struct / Slide 13 Arrays inside structures * We can use arrays inside structures.  Example: struct square{ Point vertex[4]; }; square sq;  Assign values to Sq using the given square sq.vertex[0].x = 4; sq.vertex[0].y = 3; (4, 3)(10, 3) (4, 1)(10, 1) 4 3x y

15 COMP104 Struct / Slide 14 * Declaration of pointer to struct * ; * Example: StudentRecord Student1; StudentRecord* pStudent2; Student1.Name = "Bill Games"; Student1.Id = 666; Student1.Dept = "Bill"; Student1.gender = 'M'; pStudent2 = &Student1; (*pStudent2).Id = 444; pStudent2->Id = 555; pStudent2->gender = '!'; pStudent2->gender = '?'; Pointer to Struct Bill Gates 666 M Bill Student1 pStudent2 l Dot operator for ‘object’ l  for ‘pointer’


Download ppt "Struct COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender,"

Similar presentations


Ads by Google