Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.

Similar presentations


Presentation on theme: "COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo."— Presentation transcript:

1 COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo

2 Structure Structure – User defined data types  Collection of related data items Possibly of different types  In C++ Called struct Can be composed of data of different types Array  Can contain only data of the same type

3 Structure E.g.  Student record Student id, name, major, gender, start year, …  Bank account Account number, name, currency, balance, …  Address book Name, address, telephone number, …

4 Structure Individual components of a struct type  Called members (or fields) Members can be of different types  int, float, double, char, array, or even another struct A struct is named as a whole  While individual members are named using field identifiers Complex data structures  Can be formed by defining arrays of structs

5 struct Basics Definition of a structure E.g. struct { ; … }; struct date { int day; int month; int year; }; struct studentRecord { char gender; int id; char dept[5]; char name[20]; };

6 struct Basics Declaration of a variable of struct type  Syntax ; E.g.  studentRecord student1, student2; student1 and student2 are variables of studentRecord type

7 struct Basics The members of a struct type variable are accessed with the dot (. ) operator  Syntax. ; E.g.  student1.gender = 'M';  student1.id = 12345;  strcpy(student1.dept, "COMP");  strcpy(student1.name, "Chan Tai Man");

8 struct Basics Overview

9 struct-to-struct Assignment The values contained in one struct type variable  Can be assigned to another variable Of the same struct type E.g. student1 = student2; E.g.

10 Nested Structures Nest structures inside structures E.g. struct point{ double x, y; }; point P; struct line{ point p1, p2; }; line L; struct triangle{ point p1, p2, p3; }; 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) line p1 p2 x y

11 Arrays of Structures An array of structs  Multiple types of data in each array element E.g. studentRecord class[100]; class[98].gender = 'M'; class[98].id = 12345; strcpy(class[98].dept, "COMP"); strcpy(class[98].name, "Chan Tai Man"); class[0] = class[98];

12 Arrays inside structures We can use arrays inside structures E.g. Assign values to sq using the given square struct square { point vertex[4]; }; … square sq;

13 Initialize Data of struct Type Using assignment during declaration E.g. // student record struct StudentRecord { double totalgrade; char name[name_size]; char id[id_size]; int grade[no_grades]; }; StudentRecord student1 = { 0.0, "CHAN Tai Man", "123456", {80, 67, 34, 67} };

14 Enumerated Type The enumerated type ( enum )  Derived from the integer type  Each integer value is given an identifier Called an enumeration constant Syntax  enum type_name {enumeration_constants};  type_name variable_name;

15 Enumerated Type E.g. enum days {mon, tue, wed, thur, fri, sat, sun}; days day_of_week; // normal expression day_of_week = wed; // ERROR: miss match type day_of_week = 0; // CORRECT: 0 is converted to days day_of_week = (days) 0; // Use enumeration constant as an integer int x = tue; // same as x = 3; int day[7]; day[tue] = 0; // same as day[1] = 0;

16 enum and struct E.g. enum suit {club, diamond, heart, spade}; struct card { suit cardSuit; int value; };

17 SUMMARY By the end of this lab, you should be able to:  Declare and use of struct type Nested structure Array of struct  Declare and use of enum type

18 Lab12 Download the template from website Save/Load function is optional, no need to demo this function in lab


Download ppt "COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo."

Similar presentations


Ads by Google