Structured Data Types struct class Structured Data Types




![Representation in Memory month day struct Employee { int id; char name[15] double salary; Representation in Memory month day struct Employee { int id; char name[15] double salary;](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-5.jpg)










![Arrays of Structures struct Prisoner. Record{ char last. Name[20]; char first. Name[20]; char Occupation[20]; Arrays of Structures struct Prisoner. Record{ char last. Name[20]; char first. Name[20]; char Occupation[20];](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-16.jpg)
![Arrays of Structures //Load data into array i =0; while (fin >>Prisoner[i]. last. Name Arrays of Structures //Load data into array i =0; while (fin >>Prisoner[i]. last. Name](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-17.jpg)


![Arrays of Structures and functions // prototypes struct Payroll { int id; char name[15]; Arrays of Structures and functions // prototypes struct Payroll { int id; char name[15];](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-20.jpg)

![Arrays of Structures void showarray(Payroll staff[ ], n. Items){ int i; cout << setiosflags(ios: Arrays of Structures void showarray(Payroll staff[ ], n. Items){ int i; cout << setiosflags(ios:](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-22.jpg)
![Nested Structures struct List. Data { int id; char name[15]; double payrate; }; struct Nested Structures struct List. Data { int id; char name[15]; double payrate; }; struct](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-23.jpg)










- Slides: 33

Structured Data Types struct class

Structured Data Types array – homogeneous container n collections of only one type struct – heterogeneous data type n collections of different types

Structured Data Types w struct is a user defined data type with a fixed number of components that are accessed by name, not by index. w Each component is called a field or a member

Structure Declaration struct My. Date. Type { int month; struct TAG int day; int year; }; for above, the struct members are month, day, and year
![Representation in Memory month day struct Employee int id char name15 double salary Representation in Memory month day struct Employee { int id; char name[15] double salary;](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-5.jpg)
Representation in Memory month day struct Employee { int id; char name[15] double salary; }; Employee Emp 1 = {1234, “Smith”, 5. 6}; name id 001234 S m i t h year each cell represents 1 byte, so an int is stored in 4 bytes, name has 15 bytes reserved and salary is a double 8 bytes members can be arrays! or other structs! salary 5. 6

w Structure declaration n n specifying a template like creating your own datatype w Object declaration or variable declaration n n or object instantiation using your own data type

Object Declaration/Instantiation int num; double x; data type variable name My. Date. Type my. Birth, today, tomorrow;

Declaration and Initialisation Date my. Birth = {4, 2, 1980}; Date today = {11, 25, 1999}; Date tomorrow = {3, 25, 2001};

Assigning Values Illegal ! my. Birth = {4, 2, 1980}; today = {11, 25, 1999}; tomorrow = {3, 25, 2001}; Legal ! my. Birth = today;

Assigning Values instance of Date. Type my. Birth. month = 4; my. Birth. day = 2; my. Birth. year = 1980; today. month = 3; today. day = 25; today. year = 1995; member of my. Birth instance Note dot, This is the member selection operator

Accessing struct Values // to assign to a basic variable year = today. year new_mo = today. month + 1 // to assign contents of a variable to a structure member today. month = some. Month;

keyboard input Date today, bill_date; cout << “Enter month, day and year: “; cin >> today. month >>today. day >> today. year; bill_date = today; // an aggregate action cout << bill_date. month <<“ “ << bill_date. day; it should be easy to see how you can read from a file

Structures used as function arguments Pass by value int Overdue(Date now, int purchaseyear); Overdue(today, bill_Date. year); Pass by reference int Overdue(Date &now, int purchaseyear); Overdue(today, bill_Date. year);

Pass by value and Pass by reference w The same rules apply to structure objects as to basic atomic data. w You can pass a structure by value, the argument passes a copy of individual members to the receiving parameter. n in previous slide the argument today was copied to parameter now. w You can specify the function to pass by reference by use of the reference symbol &.

Aggregate Operations Operation Arrays I/O No (except strings) No Assignment No Arithmetic No Comparison No Parameter pass. Ref. only Structs Funct. return value No Yes No No Either value or ref.
![Arrays of Structures struct Prisoner Record char last Name20 char first Name20 char Occupation20 Arrays of Structures struct Prisoner. Record{ char last. Name[20]; char first. Name[20]; char Occupation[20];](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-16.jpg)
Arrays of Structures struct Prisoner. Record{ char last. Name[20]; char first. Name[20]; char Occupation[20]; char Marital. Status[20]; int age; char Birth. Place[20]; }; // array of 100 elements, each of type Prisoner. Record Prisoner[100];
![Arrays of Structures Load data into array i 0 while fin Prisoneri last Name Arrays of Structures //Load data into array i =0; while (fin >>Prisoner[i]. last. Name](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-17.jpg)
Arrays of Structures //Load data into array i =0; while (fin >>Prisoner[i]. last. Name >> Prisoner[i]. first. Name >> Prisoner[i]. Occupation >> Prisoner[i]. Marital. Status >> Prisoner[i]. age >> Prisoner[i]. Birth. Place) { i++; } DEMO 1 CHRIS

Another example of Arrays of Structures Consider the structure below struct Payroll { int id; char name[15]; double payrate; }; // an array of 3 records of type Payroll employee[3];

Arrays of Structures // load array -- there are other ways to load the array Payroll employee[3] = { {11, “Bill”, 7. 25}, {12, “Paul”, 6. 50}, {13, “Maria”, 9. 00} }; // display array for(i = 0; i < 3; i++) { cout << ‘n’ << employee[i]. id << setw(20) << employee[i]. name << setw(20) << employee[i]. payrate; }
![Arrays of Structures and functions prototypes struct Payroll int id char name15 Arrays of Structures and functions // prototypes struct Payroll { int id; char name[15];](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-20.jpg)
Arrays of Structures and functions // prototypes struct Payroll { int id; char name[15]; double payrate; }; void loadarray(Payroll staff[ ], int n. Items); void showarray(Payroll staff[ ], int n. Items); int main(){ //declare array of 3 records of type Payroll and initialize the first record Payroll employee[3]; loadarray(employee, 3); showarray(employee, 3); cout << endl; return 0; } // calls

Arrays of Structures // load array - data typically entered via file input void loadarray(Payroll staff[ ], int n. Items) { int i; for(i = 0; i < n. Items; i++) { cout << "Enter the ID, name, and pay rate: "; cin >> staff[i]. id >> staff[i]. name >> staff[i]. payrate; cout << endl; } }
![Arrays of Structures void showarrayPayroll staff n Items int i cout setiosflagsios Arrays of Structures void showarray(Payroll staff[ ], n. Items){ int i; cout << setiosflags(ios:](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-22.jpg)
Arrays of Structures void showarray(Payroll staff[ ], n. Items){ int i; cout << setiosflags(ios: : fixed) << setprecision(2); for(i = 0; i < 3; i++) { cout << 'n' << setw(5) << staff[i]. id << setw(13) << staff[i]. name << setw(10) << staff[i]. payrate; } }
![Nested Structures struct List Data int id char name15 double payrate struct Nested Structures struct List. Data { int id; char name[15]; double payrate; }; struct](https://slidetodoc.com/presentation_image_h2/f921d16c4714ac00e38a6b91d5bd8b6b/image-23.jpg)
Nested Structures struct List. Data { int id; char name[15]; double payrate; }; struct List { int Max. Size; int n. Items List. Data Record[100] }; Called a Meta Structure

Nested Structures // declaration of an List object //List contains an array of List. Data structures List My. Employees; // Initialise list data My. Employees. Max. Size = 100; My. Employees. n. Items = 0; //Add a record Note syntax use of dots to refer to members. if a member is a struct, then refer to its members using dot notation My. Employees. Record[0]. ID = 123; strcpy(My. Employees. Record[0]. Name, “SMITH”); My. Employees. Record[0]. payrate = 10. 50; My. Employees. n. Items++;

Abstract data types ADT’s Example : A list ADT A list has max length number of objects currently stored objects Operations create list (initialise list members) add object display list remove object destroy list search list for an object sort list check if list is full check if list is empty etc.

Definition of ADT w Most languages have concrete data types- C++ has int , char, float, etc. n n The data type determines the range of legitimate values and the operations that can be performed. The data types provided by the language are called concrete data types. w During the design of a program the need for new data types may arise. for example a record in a database or indeed a whole data base. These objects have ranges of legitimate values and records and databases have legitimate operations. w A formal specification of such objects, defining legitimate ranges and valid operations is an abstract data type. See previous slide. NOTE it must not be specified in C++ form.

Implementation of ADT’s using structs declare a struct for the data declare a struct for the ADT, one member will be the data struct, other typical member is the number of items stored. Initialise/create an ADT object n sets the initial members Create functions that operate on the ADT object.

A List ADT Implementation const int MAXSIZE = 100; //declare a struct for the data struct List. Data { int id; char name[15]; double payrate; }; //declare a struct for the abstract data type struct List { int Max. Size; int n. Items; List. Data Record[MAXSIZE] }; //operation prototypes void Create. List(List & list); void Add. To. List(List & list, List. Data newdata); void Display. List(List & list);

A List ADT Implementation int main(void) { //declare two lists List A; List. Data temp; // a temporary variable to add records Create. List(A); //constructs lists by setting up size to MAXSIZE // and n. Items to zero cout << Enter employee ID, followed by Name and then rate of pay : “; cin >> temp. ID >> temp. name >> temp. payrate; Add. To. List(A, temp); Display. List(A); return 0; }

operation definitions //operation definitions for list “construction” void Create. List(List & list){ list. Max. Size = MAXSIZE; list. n. Items = 0; }

operation definitions //Notice how array subscript is a member of struct i. e list. n. Items is used to refer //to next free element in array. void Add. To. List(List & list, List. Data newdata) { list. Record[list. n. Items]. id = newdata. id; list. Record[list. n. Items]. payrate = newdata. payrate; strcpy(list. Record[list. n. Items]. name, newdata. name); //now increment n. Items member list. n. Items++; }

operation definitions void Display. List(List & list){ int i; cout << "There are " << list. n. Items << " Employees in the list" << endl; for (i=0; i<list. n. Items; i++) { cout << list. Record[i]. id <<" "<< list. Record[i]. name <<" "<< list. Record[i]. payrate << endl; } } DEMO 2

Things to come - binding data to functions that use the data. w. Functions are loosely associated with structure. w. It would be nice if the data and operations were bound together. w. We can! but C++ only, not C w. Add function prototypes to struct declaration. w. Even more tight connection between data and operations via classes.