Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures COMP104 Structs / Slide 2 Motivation: a new type * Structures hold data that belong together. * Examples: n Student record  student id, name,

Similar presentations


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

1

2 Structures

3 COMP104 Structs / Slide 2 Motivation: a new type * 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. Remember that an array is a collection of variables of same type, a collection of variables of different types is a ‘structure’.

4 COMP104 Structs / 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 Structs / Slide 4 Date chrismas; chrismas.day = 25; Chrismas.month = 12; Chrismas.year = 2003 Define new variable of type ‘Date’ Access to member variables using dot operator

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

7 COMP104 Structs / Slide 6 struct Student { string Name; int Id; string Dept; char gender; }; The “Student” structure has 4 members. Example:

8 COMP104 Structs / Slide 7 ‘struct’ usage  Declaration of a variable of struct type: ; * Example: Student student1, student2; student1 and student2 are variables of Student type. student1student2 Name Idgender Dept Name Idgender Dept

9 COMP104 Structs / 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 Structs / 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 Structs / Slide 10 ‘struct’ initialization Student a = { “John Wong";, 98765, "COMP“, ‘M’; };

12 Putting things together …

13 COMP104 Structs / Slide 12 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) “ Nested ” structures

14 COMP104 Structs / Slide 13 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

15 COMP104 Structs / Slide 14 Example: Student 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

16 COMP104 Structs / Slide 15 Structures of arrays * 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

17 COMP104 Structs / Slide 16 * Defining types is fundamental  objects! n A simple example of ‘enum’ * ‘struct’ is obsolete, welcome to ‘class’!

18 COMP104 Structs / Slide 17 Enum Dept {CSE,ECE, MATH}; Dept d; … If (d==CSE) … if (d==0) … Enum Month {Jan=1,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC}; Month m; 012 Enumeration type


Download ppt "Structures COMP104 Structs / Slide 2 Motivation: a new type * Structures hold data that belong together. * Examples: n Student record  student id, name,"

Similar presentations


Ads by Google