Structures Data Types A Data Type consists of: Unit 7 Gaddis: 11.2-8 set of vaues set of operations over those vaues CS 1428 Spring 2018 Ji Seaman exampe: Integer whoe numbers, -32768 to 32767 +, -, *, /, %, ==,!=, <, >, <=, >=,... Which operation is not vaid for foat? 1 2 Data Types (C/C++) 11.2 Structures Primitive Data Types atomic vaues, such as: Integers: Composite data type used to group mutipe variabes together into a unit. Exampe: student short, int, ong, char, boo ID Number Foating Points: Name foat, doube, ong doube Composite (or Aggregate) Types: vaues of these types are composed from other vaues. Arrays: sequence of vaues of the same type Age Major Each student has a vaue for each of these variabes (or attributes). Structures: named components of various types 3 4
Structures in C++ Define the student as a struct in C++: struct Student { int idnumber; int age; string major; NOTE: semicoon after ast cury bracket! A struct is a data type, and by convention the name is capitaized. The components are caed members (or fieds ). 5 Defining structure variabes So far we have defined a new data type, but we haven t defined any variabes of that type. To define a variabe of type Student: Student mystudent; Can define mutipe variabes of type Student: Student student1, student2, agradstudent; Each one has its own set of the member variabes in the Student data type 6 Defining structure variabes Each variabe of type Student has its own set of the member variabes from the Student data type student1 idnumber name age major Student student1, student2; student2 idnumber name age major 7 11.3 Accessing Structure Members Use dot operator to access members of a struct variabe: student1.age = 18; student2.idnumber = 123456; cin >> agradstudent.name; agradstudent.major = Rocket Science ; Member variabes of structures can be used just ike reguar variabes of the same type. student1.age++; //happy birthday myfunc(student2.idnumber); if (student1.age==student2.age) {... 8
Operations over structures: Vaid operations over entire structs: Outputting & comparing structure variabes Output the members one at a time: assignment: student1 = student2; function ca: myfunc(gradstudent,x); Invaid operations over entire structs: comparison: student1 == student2 cout << student1.idnumber << " "; cout << student1.name << " "; cout << student1.age << " "; cout << student1.major << end; Output: 11122 Chris Johnson 19 Chemistry output: cout << student1; input: cin >> student2; Comparing two structs: Must do these member by member! How is this different from Arrays? 9 if (student1.idnumber == student2.idnumber && student1.name == student2.name && student1.age == student2.age && student1.major == student2.major)... 10 11.4 Initiaizing a Structure Struct variabe can be initiaized when it is defined: Student student1 = {123456, John Smith,22, Math Must give vaues of members in order of the struct decaration. Can NOT initiaize members in structure decaration, ony variabe definition: struct StudentA { int id = 123456; //ILLEGAL string name = John Smith ; //ILLEGAL 11 struct EmpoyeePay { int empnum; doube payrate; doube hours; doube grosspay; // Empoyee name // Empoyee number // Houry pay rate // Hours worked // Gross pay int main() { EmpoyeePay empoyee1 = {"Betty Ross", 141, 18.75 EmpoyeePay empoyee2 = {"Ji Sandburg", 142, 17.50 cout << fixed << setprecision(2); // Cacuate pay for empoyee1 cout << "Name: " << empoyee1.name << end; cout << "Empoyee Number: " << empoyee1.empnum << end; cout << "Enter the hours worked by this empoyee: "; cin >> empoyee1.hours; empoyee1.grosspay = empoyee1.hours * empoyee1.payrate; cout << "Gross Pay: " << empoyee1.grosspay << end << end; // Cacuate pay for empoyee2 cout << "Name: " << empoyee2.name << end; cout << "Empoyee Number: " << empoyee2.empnum << end; cout << "Enter the hours worked by this empoyee: "; cin >> empoyee2.hours; empoyee2.grosspay = empoyee2.hours * empoyee2.payrate; cout << "Gross Pay: " << empoyee2.grosspay << end; Initiaizes ony name, empnum, and payrate 12
Sampe output from previous program: Name: Betty Ross Empoyee Number: 141 Enter the hours worked by this empoyee: 40 [Enter] Gross Pay: 750.00 Name: Ji Sandburg Empoyee Number: 142 Enter the hours worked by this empoyee: 20 [Enter] Gross Pay: 350.00 11.5 Arrays of Structures You can store vaues of structure types in arrays. Student roster[40]; //hods 40 Student structs Each student structure is accessibe via the subscript notation: roster[0] = student1; //copies student1 to first eem. Members of structure accessibe via dot operator cout << roster[0].name << end; 13 14 Arrays of Structures: initiaization To initiaize an array of structs: struct Student { int idnumber; int age; string major; int main() { Student roster[] = { {123456,"Ann Page",22,"Math", {111222,"Jack Spade",18,"Physics" Arrays of Structures Arrays of structures processed in oops: Student roster[40]; //input for (int i=0; i<40; i++) { cout << "Enter the name, age, idnumber and " << "major of the next student: \n"; cin >> roster[i].name >> roster[i].age >> roster[i].idnumber >> roster[i].major; //output a the id numbers and names for (int i=0; i<40; i++) { cout << roster[i].idnumber << end; cout << roster[i].name << end; 15 16
11.6 Nested Structures You can nest one structure inside another. struct Address { string street; string city; string state; int zip; struct Student { int idnumber; Address homeaddress; Nested Structures Use dot operator mutipe times to get into the nested structure: Student student1; student1.name = Bob Lambert ; student1.homeaddress.city = San Angeo ; student1.homeaddress.state = TX ; Or set up address structure separatey: Address a1; a1.street = 101 Main St. ; a1.city = San Angeo ; a1.state = TX ; a1.zip = 76903; 17 student1.name = Bob Lambert ; student1.homeaddress = a1; 18 11.7 Structures as function arguments Structure variabes may be passed as arguments to functions. void showstudent(student x) { cout << x.idnumber << end; cout << x.name << end; cout << x.age << end; cout << x.major << end; int main() { Student student1; //input information about student1 here Note: Student decaration must be goba!! Structures as function arguments By defaut, structure variabes are passed by vaue (ike most variabes). If the function needs to change the vaue of a member, the structure variabe shoud be passed by reference. void happybirthday(student &s) { s.age++; //or s.age = s.age+1; showstudent(student1); 19 20
11.8 Returning a Structure from a Function A function may return a structure. Student inputstudent(ifstream &fin) { Student resut; fin >> resut.idnumber; fin >> resut.name; fin >> resut.age; fin >> resut.major; return resut; int main() { ifstream infie; infie.open( students.dat ); Student student1 = inputstudent(infie); for (int i=0; i<40; i++) roster[i] = inputstudent(infie); infie.cose(); Note: aways pass iostreams by reference!! 21