Structures

Structured Definition

struct EntryType
{
    string   firstName;
    string   lastName;
    int      areaCode;      // Range 100..999
    string   phoneNumber;
};

Structure Access

        EntryType entry;
        cout << entry.firstName << entry.lastName << endl;

Structures Initialization

    EntryType entryDefault;
    entryDefault.firstName = "San";
    entryDefault.lastName = "Chang";
    entryDefault.areaCode = 02;
    entryDefault.phoneNumber = "123-4567";

Aggregate Operations

Operation array struct
I/O No (except string) No
Assignement No Yes
Arithmetic No No
Comparison No No
Parameter passing By reference only By value or reference
Return as a function's return value No Yes

Complex Structures

Array of Structs

EntryType addressBook[150]; for (i=0; i<150; i++) addressBook[i] = entryDefault;

Struct of Structs

struct DateType { int month; // Range 1..12 int day; // Range 1..31 int year; // Range 1900..2100 }; struct EntryType { string firstName; string lastName; int areaCode; string phoneNumber; DateType date; }; DateType Today; date.month = 11; date.day = 28; date.year = 1996; EntryType entryNew; entryNew.firstName = "San"; entryNew.lastName = "Chang"; entryNew.areaCode = 02; entryNew.phoneNumber = "123-4567"; entryNew.date = Today;