Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type."— Presentation transcript:

1

2 Structured Data Types struct class

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

4 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

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

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

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

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

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

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

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

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

13 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

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

15 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 &.

16 Aggregate Operations OperationArrays Structs I/ONo ( except strings )No AssignmentNo Yes ArithmeticNo No ComparisonNo No Parameter pass.Ref. only Either value or ref. Funct. return value No Yes

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

18 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

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

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

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

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

23 Arrays of Structures void showarray(Payroll staff[ ], 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; } }

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

25 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

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

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

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

29 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);

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

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

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

33 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

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


Download ppt "Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type."

Similar presentations


Ads by Google