1 / 9

HKUST Summer Programming Course 2008

HKUST Summer Programming Course 2008. Structure. Overview. Structure Initialize a struct Quiz Semicolon in a struct definition Combining structs. Struct. A collection of values of different types (a structure) struct FruitCollection { int quantity;

karis
Download Presentation

HKUST Summer Programming Course 2008

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HKUST SummerProgramming Course 2008 Structure

  2. Overview • Structure • Initialize a struct • Quiz • Semicolon in a struct definition • Combining structs

  3. Struct • A collection of values of different types (a structure) struct FruitCollection { int quantity; char size; // (L)arege, (M)iddle and (S)mall }; • Usually be placed under global scope, it can also be put in another scopes. • Use dot operator (.) to access member variables. FruitCollection pineapple; pineapple.quantity = 6; pineapple.size = ‘L’; • Can have more than one struct in a single program

  4. Initialize a struct • Initialize by a list of values according to the sequence of variables in the struct definition. FruitCollection orange = { 100, ‘M’ }; • If number of values is MORE than the struct requires, error will occur. • If number of values is LESS than the struct requires, zero value will be given to the rest of variables. • In contrast, C++ won’t initialize local variables • Copy from other objects of the same type. FruitCollection apple; apple = orange; //apple is now {100, ‘M’}

  5. Quiz int main() { struct Date{ int month, day, year; } Date dueDate = { 31, 12, 2006 }; return 0; } • What’s wrong with the program?

  6. Semicolon in a struct definition • A semicolon must be put at the end of a struct definition, otherwise the program can’t compile. struct Date { int month, day, year; }; //<- remember to add this semicolon • A useful feature related to this semicolon. struct fruit { int quantity; char size; } apple, orange; // variables of fruit type • Variables of this struct can be declared immediately after the definition.

  7. Logical error • The order of initialization of data member is the same as the order of declaration of data member • The previous example will initialize the date as Year = 2006, Month = 31, Day = 12

  8. Combining structs • A struct can make use of another struct to be the type of its member variables. // fruitCollection was defined in previous slides struct fruitBasket { fruitCollection apple; fruitCollection orange; }; int main() { fruitBasket A; A.apple.quantity = 3; A.apple.size = ‘L’; A.orange.quantity = 6; … } • To access the member variable of a struct inside another struct, we can use the dot operator twice. (Dot operator also called member access operator)

  9. Combining structs • A struct can also be defined inside another struct. struct bookBorrow { string borrower; string bookTitle; // struct inside another struct struct Date { int day, month, year; } dueDate; }; • Be careful when you try to initialize it, bookBorrow bk001={"Adam", "The Wizard Book", {31,12,2006}}; cout << bk001.dueDate.day << endl; // output is 31

More Related