17
COMP102 Lab 11 1 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

Embed Size (px)

DESCRIPTION

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, …

Citation preview

Page 1: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 1

COMP 102

Programming Fundamentals I

Presented by : Timture Choi

Page 2: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 2

StructureStructure – 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

Page 3: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 3

StructureE.g. Student record

Student id, name, major, gender, start year, …

Bank account Account number, name, currency,

balance, … Address book

Name, address, telephone number, …

Page 4: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 4

StructureIndividual components of a struct type

Called members (or fields)Members can be of different types

int, float, double, char, array, or even another structA 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

Page 5: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 5

struct BasicsDefinition of a structure

E.g.

struct <struct-type>{ <type> <identifier_list>; <type> <identifier_list>; …};

struct date { int day; int month; int year;};

struct studentRecord { char gender; int id; char dept[5]; char name[20];};

Page 6: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 6

struct BasicsDeclaration of a variable of struct type Syntax

<struct-type> <identifier_list>;

E.g. studentRecord student1, student2;

student1 and student2 are variables of studentRecord type

Page 7: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 7

struct BasicsThe members of a struct type variable are accessed with the dot (.) operator Syntax

<struct-variable>.<member_name>;

E.g. student1.gender = 'M'; student1.id = 12345; strcpy(student1.dept, "COMP"); strcpy(student1.name, "Chan Tai Man");

Page 8: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 8

struct BasicsOverview

Page 9: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 9

struct-to-struct Assignment

The values contained in one struct type variable Can be assigned to another variable

Of the same struct typeE.g. student1 = student2;E.g.

Page 10: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 10

Nested StructuresNest structures inside structuresE.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 x y

Page 11: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 11

Arrays of StructuresAn array of structs Multiple types of data in each array

elementE.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];

Page 12: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 12

Arrays inside structuresWe can use arrays inside structuresE.g.

Assign values to sq using the given square

struct square { point vertex[4];};…square sq;

Page 13: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 13

Initialize Data of struct TypeUsing assignment during declarationE.g.

// student recordstruct 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}};

Page 14: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 14

Enumerated TypeThe 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;

Page 15: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 15

Enumerated TypeE.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;

Page 16: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 16

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

Page 17: COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab 11 17

SUMMARYBy 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