struct - Programming Structures 1 102 Prog. Fundamentals,...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Programming Structures
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 P102 Prog. Fundamentals, Structures / Slide 2 Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct . A struct is heterogeneous in that it can be composed of data of different types. In contrast, array is homogeneous since it can contain only data of the same type.
Background image of page 2
3 P102 Prog. Fundamentals, Structures / Slide 3 Structures Structures hold data that belong together . Examples: Student record: student id, name, major, gender, start year, … Bank account: account number, name, currency, balance, … Address book: name, address, telephone number, … In database applications, structures are called records.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 P102 Prog. Fundamentals, Structures / Slide 4 Structures Individual components of a struct type are called members (or fields ). Members can be of different types (simple, array or 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 .
Background image of page 4
5 P102 Prog. Fundamentals, Structures / Slide 5 struct basics Definition of a structure: struct <struct-type>{ <type> <identifier_list>; <type> <identifier_list>; ... } ; Example: struct Date { int day; int month; int year; } ; The “Date” structure has 3 members, Each identifier defines a member of the structure.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 P102 Prog. Fundamentals, Structures / Slide 6 struct examples Example: struct StudentInfo{ int Id; int age; char Gender; double CGA; }; Example: struct StudentGrade{ char Name[15]; char Course[9]; int Lab[5]; int Homework[3]; int Exam[2]; }; The “StudentGrade” structure has 5 members of different array types. The “StudentInfo” structure has 4 members of different types.
Background image of page 6
P102 Prog. Fundamentals, Structures / Slide 7 struct examples Example: struct BankAccount{ char Name[15]; int AcountNo[10]; double balance; Date Birthday; }; Example: struct StudentRecord{ char Name[15]; int Id; char Dept[5]; char Gender; }; The “StudentRecord” structure has 4 members. The “BankAcount”
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/30/2010 for the course COMP COMP102 taught by Professor Lrr during the Fall '09 term at HKUST.

Page1 / 29

struct - Programming Structures 1 102 Prog. Fundamentals,...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online