1 / 33

Structured Data Types

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

mmccartney
Télécharger la présentation

Structured Data Types

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. Structured Data Types • struct • class

  2. Structured Data Types • array – homogeneouscontainer • collections of only one type • struct – heterogeneousdata type • collections of different types

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

  4. Structure Declaration struct MyDateType { int month; int day; int year; }; struct TAG for above, the struct members are month, day, and year

  5. Representation in Memory month day year struct Employee { int id; char name[15] double salary; }; Employee Emp1 = {1234, “Smith”, 5.6}; 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 name id 5.6 001234 S m i t h \0

  6. Structure declaration • specifying a template • like creating your own datatype • Object declaration or variable declaration • or object instantiation • using your own data type

  7. data type variable name Object Declaration/Instantiation • int num; • double x; MyDateType myBirth,today, tomorrow;

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

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

  10. member of myBirth instance Assigning Values myBirth.month = 4; myBirth.day = 2; myBirth.year = 1980; instance of DateType today.month = 3; today.day = 25; today.year = 1995; Note dot, This is the member selection operator

  11. 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 = someMonth;

  12. 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

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

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

  15. Aggregate Operations Operation Arrays Structs I/O No (except strings) No Assignment No Yes Arithmetic No No Comparison No No Parameter pass. Ref. only Eithervalue or ref. Funct. return value No Yes

  16. Arrays of Structures struct PrisonerRecord{ char lastName[20]; char firstName[20]; char Occupation[20]; char MaritalStatus[20]; int age; char BirthPlace[20]; }; // array of 100 elements, each of type PrisonerRecord PrisonerRecord Prisoner[100];

  17. Arrays of Structures //Load data into array i =0; while (fin >>Prisoner[i].lastName >> Prisoner[i].firstName >> Prisoner[i].Occupation >> Prisoner[i].MaritalStatus >> Prisoner[i].age >> Prisoner[i].BirthPlace) { i++; } DEMO1 CHRIS

  18. 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 Payroll employee[3];

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

  20. Arrays of Structures and functions // prototypes struct Payroll { int id; char name[15]; double payrate; }; void loadarray(Payroll staff[ ], int nItems); void showarray(Payroll staff[ ], int nItems); int main(){ //declare array of 3 records of type Payroll and initialize the first record Payroll employee[3]; loadarray(employee,3); // calls showarray(employee,3); cout << endl; return 0; }

  21. Arrays of Structures // load array - data typically entered via file input void loadarray(Payroll staff[ ], int nItems) { int i; for(i = 0; i < nItems; i++) { cout << "Enter the ID, name, and pay rate: "; cin >> staff[i].id >> staff[i].name >> staff[i].payrate; cout << endl; } }

  22. Arrays of Structures void showarray(Payrollstaff[ ], nItems){ 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; } }

  23. Nested Structures struct ListData { int id; char name[15]; double payrate; }; struct List { int MaxSize; int nItems ListData Record[100] }; Called a Meta Structure

  24. Nested Structures // declaration of an List object //List contains an array of ListData structures List MyEmployees; // Initialise list data MyEmployees.MaxSize = 100; MyEmployees.nItems = 0; //Add a record MyEmployees.Record[0].ID = 123; strcpy(MyEmployees.Record[0].Name, “SMITH”); MyEmployees.Record[0].payrate = 10.50; MyEmployees.nItems++; Note syntax use of dots to refer to members. if a member is a struct, then refer to its members using dot notation

  25. 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.

  26. Definition of ADT • Most languages have concrete data types- C++ has int , char, float, etc. • 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. • 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. • 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.

  27. 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 • sets the initial members • Create functions that operate on the ADT object.

  28. A List ADT Implementation const int MAXSIZE = 100; //declare a struct for the data struct ListData { int id; char name[15]; double payrate; }; //declare a struct for the abstract data type struct List { int MaxSize; int nItems; ListData Record[MAXSIZE] }; //operation prototypes void CreateList(List & list); void AddToList(List & list, ListData newdata); void DisplayList(List & list);

  29. A List ADT Implementation • int main(void) { • //declare two lists • List A; • ListData temp; // a temporary variable to add records • CreateList(A); //constructs lists by setting up size to MAXSIZE • // and nItems to zero • cout << Enter employee ID, followed by Name and then rate of pay : “; • cin >> temp.ID >> temp.name >> temp.payrate; • AddToList(A,temp); • DisplayList(A); • return 0; • }

  30. operation definitions //operation definitions for list “construction” void CreateList(List & list){ list.MaxSize = MAXSIZE; list.nItems = 0; }

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

  32. operation definitions void DisplayList(List & list){ int i; cout << "There are " << list.nItems << " Employees in the list" << endl; for (i=0;i<list.nItems; i++) { cout << list.Record[i].id <<" "<< list.Record[i].name <<" "<< list.Record[i].payrate << endl; } } DEMO2

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

More Related