COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi
COMP102 Lab 112 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
COMP102 Lab 113 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, …
COMP102 Lab 114 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
COMP102 Lab 115 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]; };
COMP102 Lab 116 struct Basics Declaration of a variable of struct type Syntax ; E.g. studentRecord student1, student2; student1 and student2 are variables of studentRecord type
COMP102 Lab 117 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");
COMP102 Lab 118 struct Basics Overview
COMP102 Lab 119 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.
COMP102 Lab 1110 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
COMP102 Lab 1111 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];
COMP102 Lab 1112 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;
COMP102 Lab 1113 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} };
COMP102 Lab 1114 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;
COMP102 Lab 1115 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;
COMP102 Lab 1116 enum and struct E.g. enum suit {club, diamond, heart, spade}; struct card { suit cardSuit; int value; };
COMP102 Lab 1117 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