programmer defined structures (records)

21
Programmer Defined Structures (Records) CS362

Upload: lila-ford

Post on 30-Dec-2015

31 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programmer Defined Structures (Records)

Programmer Defined Structures (Records)

CS362

Page 2: Programmer Defined Structures (Records)

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

Page 3: Programmer Defined Structures (Records)

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

Page 4: Programmer Defined Structures (Records)

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;

};

Page 5: Programmer Defined Structures (Records)

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 };

Page 6: Programmer Defined Structures (Records)

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

Page 7: Programmer Defined Structures (Records)

Initializing A Structure Variable

Like Other Variables Structures Can be Initialized at Declaration:

struct-type variable_identifier = {member1-value, member2-value,

….memberN-value};

Page 8: Programmer Defined Structures (Records)

Structure Examplestruct studentRec{

int idNumber;string lname;string fname;char initial;char sex;double gpa;

};

Page 9: Programmer Defined Structures (Records)

Structure VariablestudentRec stu1;

stu1

InitializedstudentRec stu1 = {123456,”Sanders”,”Mark”,’S’,’M’,3.50};

stu1

idNumber lname fname initial sex Gpa

Page 10: Programmer Defined Structures (Records)

Accessing Structure Data A Structure is a Single Data Unit Members Can Be Accessed Individually To Access a Member Use The DOT notation:

struct variable_name.member_name

studentRec.lname = “Sanders”;

Members Can Be Used Like Any Other Variable

Page 11: Programmer Defined Structures (Records)

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;

Page 12: Programmer Defined Structures (Records)

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;

}

Page 13: Programmer Defined Structures (Records)

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;

}

Page 14: Programmer Defined Structures (Records)

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

studentRec studentList[MAX];

Declares Array to Store MAX Quantity of Structures Each Cell Contains One Record With Six Members

Page 15: Programmer Defined Structures (Records)

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

Page 16: Programmer Defined Structures (Records)

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;

}

Page 17: Programmer Defined Structures (Records)

Access Structure Array Data (cont.)

Swapping Structures is like Swapping Any Variables:

studentRec temp;

temp = studentList[top];studentList[top] = studentList[minPosition];studentList[minPosition] = temp;

Page 18: Programmer Defined Structures (Records)

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

Page 19: Programmer Defined Structures (Records)

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;

Page 20: Programmer Defined Structures (Records)

Nested Structure (cont.) Accessing Nested Structure

Need to Use Entire Name:

studentData.lnamestudentData.fnamestudentData.agestudentData.gpastudentData.campus.streetstudentData.campus.citystudentData.campus.statestudentData.campus.zipstudentData.home.streetstudentData.home.citystudentData.home.statestudentData.home.zip

Page 21: Programmer Defined Structures (Records)

Nested Structure (cont.) Arrays of Nested Structures

student studentList[MAX];

studentList[n].lnamestudentList[n].fnamestudentList[n].agestudentList[n].gpastudentList[n].campus.street“ “ ““ “ “studentList[n].campus.zipstudentList[n].home.street“ “ ““ “ “studentList[n].home.zip