1 / 21

Programmer Defined Structures (Records)

Programmer Defined Structures (Records). CS362. C++ Structures. A Structure (Record) Contains Data About a Single Entity: Each Data Item is Called a Member (or Field) Members Can Be Of Different Data Types Member Data Types Can Be Basic or Complex Basic – int, float, double, string

lila-ford
Download Presentation

Programmer Defined Structures (Records)

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. Programmer Defined Structures (Records) CS362

  2. C++ Structures • A Structure (Record) Contains Data About a Single Entity: • Each Data Item is Called a Member (or Field) • Members Can Be Of Different Data Types • Member Data Types Can Be Basic or Complex • Basic – int, float, double, string • Complex – array, other structures • In The End, Any Valid Data Type Can Used to Define a Member in a Structure

  3. C++ Structures • Data About a Student May Consist of: • Last Name string • First Name string • Middle Initial char • Id Number int • Sex char • GPA float • Putting This All Together We Can Define A Structure

  4. Defining A Structure • Structures Require a Two Step Definition Process: • First – Define a struct Data Type That Defines the Members of the Required Structure • Second – Use the struct to Declare Variables Needed for the Program • struct Data Type Definition: struct struct-name { type member1-name; type member2-name; : type memberN-name; };

  5. Defining A Structure • Definition Rules: • The Structure Definition Must Begin With Reserved Word – struct • Each Member Must Have a Type (the Type Can Be Different Then Other Members) • Semicolons Separate the Structure Members • Structure Definition Must End With };

  6. Defining A Structure Variable • The struct Definition Only Defines a Data Type • To Use the Structure, Memory Must be Allocated Via Variable Declaration: struct-type variable_identifier; • Where struct-type is the Structure Type Declared • Where variable identifier is a Valid Identifier Name

  7. Initializing A Structure Variable • Like Other Variables Structures Can be Initialized at Declaration: struct-type variable_identifier = {member1-value, member2-value, ….memberN-value};

  8. Structure Example struct studentRec { int idNumber; string lname; string fname; char initial; char sex; double gpa; };

  9. Structure Variable studentRec stu1; stu1 Initialized studentRec stu1 = {123456,”Sanders”,”Mark”,’S’,’M’,3.50}; stu1

  10. Accessing Structure Data • A Structure is a Single Data Unit • Members Can Be Accessed Individually • To Access a Member Use The DOT notation: structvariable_name.member_name studentRec.lname = “Sanders”; • Members Can Be Used Like Any Other Variable

  11. Accessing Structure Data (cont.) • More Examples cout << “Enter student GPA: “; cin >> stu1.gpa; stu1.gpa = stu1.gpa + 0.5; cout << fixed << showpoint << setprecision(2); cout << stu1.lname << “, “ << stu1.fname << “ “ <<; cout << stu1.gpa << endl;

  12. Using Structure Data • Once You Have Data in the Structure, it Can Be Used Like Any Other Variable: if (stu1.sex == ‘F’) { cout << stu1.fname << ‘ ’ << stu1.lname << “ is Female.”; cout << endl; } if (stu1.gpa < 2.5) { cout << stu1.lname << “, ” << stu1.fname; cout << “ is on probation.” << endl; }

  13. Structures As Arguments • Structures Can Be Passed as Data to Functions • If Data is to be Modified, Pass By Reference void readStuInfo (studentRec& stu1) { cout << “Enter student last name: “; cin >> stu1.lname; : cout << “Enter student GPA: “; cin >> stu1.gpa; }

  14. Arrays of Structures • Rather Than Using Parallel Arrays, we can use an Array of Structures • Does Not Violate Single Data Type Rule • Each Cell In The Array Will Hold One Structure struct-type array_name[size]; studentRecstudentList[MAX]; • Declares Array to Store MAX Quantity of Structures • Each Cell Contains One Record With Six Members

  15. Initializing Structure Array • Same As In The Past, Only Thing New Is the Subscript studentRec studentList[MAX] = { {123456,”Sanders”, “Mark”, ‘S’, ‘M’, 3.50}, {234789, “Blow”, “Joe”, ‘J’,‘M’, 2.50}, {367890, “Smith”, “Sally”, ‘S’,‘F’, 4.00} }; • All Remaining Cells Are Initialized to “blank” or 0

  16. Accessing Structure Array Data • Just Need To Use Subscript: arrayName[index].member_name studentList[1].idNumber = 234567; studentList[2].gpa = studentList[3].gpa; for (num = 0; num < MAX; num++) { cout << studentList[num].lname << “, ” <<; cout << studentList[num].fname << endl; }

  17. Access Structure Array Data (cont.) • Swapping Structures is like Swapping Any Variables: studentRec temp; temp = studentList[top]; studentList[top] = studentList[minPosition]; studentList[minPosition] = temp;

  18. Nested Structure • A Nested Structure is a Structure that has a Member that is a Structure • Used to Organize Data, and to Allow for the Reuse of Variable Names • To Declare a Nested Structure You Must First Declare the Structure(s) that will be Nested • Once Declared the Structure can be used as Data Type for Members of Other Structures

  19. Nested Structure (cont.) struct address { string street; string city; string state; string zip; }; struct student { string lname; string fname; int age; double gpa; address campus; address home; }; student studentData;

  20. Nested Structure (cont.) • Accessing Nested Structure • Need to Use Entire Name: studentData.lname studentData.fname studentData.age studentData.gpa studentData.campus.street studentData.campus.city studentData.campus.state studentData.campus.zip studentData.home.street studentData.home.city studentData.home.state studentData.home.zip

  21. Nested Structure (cont.) • Arrays of Nested Structures student studentList[MAX]; studentList[n].lname studentList[n].fname studentList[n].age studentList[n].gpa studentList[n].campus.street “ “ “ “ “ “ studentList[n].campus.zip studentList[n].home.street “ “ “ “ “ “ studentList[n].home.zip

More Related