structured data types struct class structured data types array – homogeneous container collections...

33
Structured Data Types struct class

Upload: cody-allen

Post on 13-Dec-2015

252 views

Category:

Documents


2 download

TRANSCRIPT

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

Structured Data Types

structclass

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

Structured Data Types

array – homogeneous container collections of only one type

struct – heterogeneous data type collections of different types

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

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

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

Structure Declaration

struct MyDateType {int month; int day; int year;

};for above, the struct members are month, day, and year

struct TAG

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

Representation in Memorymonth day year

struct Employee {

int id;

char name[15]

double salary;

};

Employee Emp1 = {1234, “Smith”, 5.6};

id name salary

001234 S 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!

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

Structure declaration specifying a template like creating your own datatype

Object declaration or variable declaration or object instantiation using your own data type

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

Object Declaration/Instantiation

int num;double x;

MyDateType myBirth, today, tomorrow;

data type variable name

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

Declaration and Initialisation

Date myBirth = {4, 2, 1980};

Date today = {11, 25, 1999};

Date tomorrow = {3, 25,

2001};

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

Assigning Values

myBirth = {4, 2, 1980};

today = {11, 25, 1999};

tomorrow = {3, 25, 2001};

Illegal !

Legal !

myBirth = today;

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

Assigning ValuesmyBirth.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

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

Accessing struct Values

// to assign to a basic variableyear = today.yearnew_mo = today.month + 1

// to assign contents of a variable to a structure member

today.month = someMonth;

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

keyboard inputDate 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

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

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

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

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

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

Aggregate Operations

Operation ArraysStructsI/O No (except strings) NoAssignment No YesArithmetic No NoComparison No NoParameter pass. Ref. only Either

value or ref.

Funct. return value No Yes

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

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 PrisonerRecordPrisonerRecord Prisoner[100];

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

Arrays of Structures

//Load data into arrayi =0;while (fin >>Prisoner[i].lastName >>

Prisoner[i].firstName >>Prisoner[i].Occupation >> Prisoner[i].MaritalStatus >>Prisoner[i].age >> Prisoner[i].BirthPlace) {

i++;}

DEMO1 CHRIS

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

Another example of Arrays of Structures

struct Payroll {int id;char name[15];double payrate;

};

// an array of 3 records of type PayrollPayroll employee[3];

Consider the structure below

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

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;

}

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

Arrays of Structures and functions

// prototypesstruct 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); // callsshowarray(employee,3);

cout << endl;return 0;}

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

Arrays of Structures

// load array - data typically entered via file inputvoid 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; }}

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

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;

}}

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

Nested Structuresstruct ListData {

int id;char name[15];double payrate;

};

struct List {int MaxSize;int nItemsListData Record[100]

};

Called a

Meta Structure

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

Nested Structures// declaration of an List object //List contains an array of ListData structuresList 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

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

Abstract data types ADT’sExample : A list ADTA list has

max lengthnumber of objects currently storedobjects

Operationscreate list (initialise list members)add objectdisplay listremove objectdestroy list search list for an objectsort listcheck if list is fullcheck if list is emptyetc.

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

Definition of ADTMost 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.

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

Implementation of ADT’s using structs

declare a struct for the datadeclare 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.

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

A List ADT Implementationconst int MAXSIZE = 100;//declare a struct for the datastruct ListData {

int id;char name[15];double payrate;

};//declare a struct for the abstract data typestruct List {

int MaxSize;int nItems;ListData Record[MAXSIZE]

};

//operation prototypesvoid CreateList(List & list);void AddToList(List & list, ListData newdata);void DisplayList(List & list);

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

A List ADT Implementationint main(void) {

//declare two listsList 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;}

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

operation definitions

//operation definitions for list “construction”void CreateList(List & list){

list.MaxSize = MAXSIZE;list.nItems = 0;

}

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

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 memberlist.nItems++;

}

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

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

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

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 CAdd function prototypes to struct declaration.Even more tight connection between data and operations via classes.