Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Janice Regan, CMPT 128, Feb.. 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Structures.

Similar presentations


Presentation on theme: "© Janice Regan, CMPT 128, Feb.. 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Structures."— Presentation transcript:

1 © Janice Regan, CMPT 128, Feb.. 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Structures

2 © Janice Regan, CMPT 128, February 2007 1 Recall: Composite Data Types  Data Type includes  A collection of objects (can be numbers, characters …)  A group of operations that can be performed on those objects (the result of the operation should also be a member of the collection)  A way to represent object of the type in memory  Basic data types (int, double, …) identify the type of a single variable which references a single location in memory.  Composite data types identify a group of values located in successive memory locations. The group of values is referred to by a single identifier.  If all the component values have the same basic type – use an array  If component values have a variety of basic types – use a structure

3 © Janice Regan, CMPT 128, February 2007 2 Using Structures  Define Structure  Declare variables of type of the structure  Use variables declared above  Structures can be used as parameters and return values of functions

4 © Janice Regan, CMPT 128, February 2007 3 Defining Structures  Define Structure (A new composite type)  Must tell the compiler what the structure is before it can be used,  In particular you must specify the following for your structure: An identifier (name) A list of component variables or elements The basic types of each of the elements

5 © Janice Regan, CMPT 128, February 2007 4 Defining Structures: Example  Define Structure labMeasurement  labMeasurement contains all data regarding the running of a load test on a sample group of components  labMeasurement includes  An integer indicating the date the measurement was taken  An integer indicating the number of components to be tested in the experiment  An integer containing the duration of the experiment is seconds  A floating point number containing the measured temperature in degrees Celsius  An integer indicating the number of components that failed the load test in the recorded conditions

6 © Janice Regan, CMPT 128, February 2007 5 Defining Structures: Example struct labMeasurement { int measurementNum; int date; int numOfComponents; int duration; double temperature; int numFailures; } ;  ; at the end of the structure definition is required

7 © Janice Regan, CMPT 128, February 2007 6 Review: what is scope?  Local or function scope:  Variable exists and can be used only within the function in which it is defined int myFunct( double myParam1, in myParam2) { // start of body of function myFunct int myLocalVariable; // myLocal Variable has local or function scope // myLocal Variable exists and can be used only // within the body of function myFunct // end of body of function myFunct }

8 © Janice Regan, CMPT 128, February 2007 7 Review: what is scope?  Global scope:  Variable exists and can be used anywhere within the file in which it is defined int myGlobalVariable; // myGlobalVariable has global scope // myGlobalVariable exists and can be used within the body of any function // within the file in which it is defined Int main ( ) { // body of function main } int myFunct( double myParam1, in myParam2) { // body of function myFunct }

9 © Janice Regan, CMPT 128, February 2007 8 Defining Structures  Defining a structure creates a data type that can be used in your program. A structure may be used only within the scope of it’s definition.  Defining a Structure does not reserve memory for any variable with the new type  You need to define structures and arrays. This provides flexibility to define custom designed composite variable types for your specific needs  simple types like int and double are defined for you, they do not need this flexibility.

10 © Janice Regan, CMPT 128, February 2007 9 Defining Structures  Once a structure is defined the compiler can determine  How much memory must be set aside when a structure of the new type is declared  The programmer who defined the new structure type must know  How to declare and use structures of the new type  How to refer to the component variables and the the new structure data type

11 © Janice Regan, CMPT 128, February 2007 10 Defining Structures: Example struct labMeasurement { int measurementNum; int date; int numOfComponents; int duration; double temperature; int numFailures; } ;  ; at the end of the structure definition is required

12 © Janice Regan, CMPT 128, February 2007 11 Declaring structure variables  Defining a structure creates a new composite data type  Within any functions in the scope of a structure definition you can declare variables of the newly defined composite type.  If a variable of the new composite type is declared inside function myfunct then that variable has local scope inside myfunct  You may declare any number of structures in any number of functions in the scope of the structure definition. Each will be allocated it own memory area

13 © Janice Regan, CMPT 128, February 2007 12 Declaring structure variables  Declare variable of type studentRecord  labMeasurement firstMeasurement;  This declaration creates an instance of a structure of type labMeasurement,  Enough memory is allocated to hold one structure of type labMeasurement and is associated with identifier firstMeasurement

14 © Janice Regan, CMPT 128, February 2007 13 Declaring Structures: Example struct labMeasurement { int measurementNum; int date; int numOfComponents; int duration; double temperature; int numFailures; } ; int someFunction (void) { labMeasurement firstMeasurement;

15 © Janice Regan, CMPT 128, February 2007 14 Scope for declared structures int someFunction (void) { labMeasurement firstMeasurement; } int someOtherFunction (void) {labMeasurement oldMeasurement, firstMeasurement; }  oldMeasurement only exists and is available to be used within someOtherFunction  One variable firstMeasurement exists only in someFunction and can be used only within someFunction. Another different variable firstMeasurement exists only in someOtherFunction and can be used only in someOtherFunction. These are two different variables that just happen to have the same name.

16 © Janice Regan, CMPT 128, February 2007 15 Memory for declared structures  Memory is assigned when a structure is declared.  Memory for each element in the structure follows the memory for the previously defined element. measurementNum date numOfComponents duration temperature numFailures

17 © Janice Regan, CMPT 128, February 2007 16 Accessing Structure members (1)  Just as we needed to access individual elements of an array we need to be able to access individual members in a structure  Members of a structure have different types and are associated with variables that have specific meanings.  Each member of the structure has its own identifier. We wish to use these identifiers to access the members of the structure.

18 © Janice Regan, CMPT 128, February 2007 17 Accessing Structure members (2)  We can have multiple instances of the same structure type.  Using only the names of the members does not distinguish between the members in different instances of the structure.  We can have different types of structure that contain members with the same identifier  Using only the names of the members does not distinguish between the members of the different types of structures  A member of a structure may have the same identifier as a variable within the same scope  Using only the names of the members does not distinguish between the member of a structure and a simple variable with the same identifier

19 © Janice Regan, CMPT 128, February 2007 18 Accessing Structure members (3)  To refer to a member of a structure we need to refer to the instance of the structure and the identifier of the member.  In C the member of a structure is accessed using a 'dot' notation  The identifier of the instance of the structure is followed by a. then by the name of the member to be accessed structureIdentifer.memberIdentifier

20 © Janice Regan, CMPT 128, February 2007 19 Sample Structure  Recall the definition of structure labMeasurement struct labMeasurement { int measurementNum; int date; int numOfComponents; int duration; double temperature; int numFailures; };

21 © Janice Regan, CMPT 128, February 2007 20 Sample: accessing members  Consider two instances of this structure declared within a particular function labMeasurement M1, M2;  To access the elements of the labMeasurement structures M1 and M2 M1.measurementNum OR M2.measurmentNum M1.date OR M2.dat M1.temperature OR M2.temperature etc.

22 © Janice Regan, CMPT 128, February 2007 21 Initialization of sample structures  At time of declaration  Initialization of structures labMeasurement firstResult = { 1, 120303, 3, 300, 50.3, 0.2};  Partial initialization of structures labMeasurement firstResult = { 1, 120303, 3}; // fills structure with // { 1, 120303, 3, 0, 0.0, 0}

23 © Janice Regan, CMPT 128, February 2007 22 Initialization of sample structures  Initialized after declarations are complete firstResult.measurementNum = 1; firstResult.date = 120303; firstResult.numOfComponents= 3; firstResult.duration = 300; firstResult.temperature = 50.3; firstResult.numFailures = 2;

24 © Janice Regan, CMPT 128, February 2007 23 Using the Sample Structure  Using elements of structure of type labMeasurement firstResult.numOfComponents = firstnumOfComponents;  Will place the value of the integer variable firstnumOfComponents in the third element of the structure firstResult secondDuration = secondResult.duration;  Will place the value of the structure member duration in labMeasurement structure secondResult (1000) into the variable secondDuration secondResult = firstResult;  Copy the values of all members in firstResult into the corresponding members in structure secondResult

25 © Janice Regan, CMPT 128, February 2007 24 Printing the Sample Structure  Structures must be printed one element at at time.  There is no mechanism in C++ to print a structure using a single statement  It is usual to write a function to read or to write the members of a structure one by one, so that this does not have to be done repeatedly within your code

26 © Janice Regan, CMPT 128, February 2007 25 Printing a structure void printLabMeasurement(labMeasurement measure) { cout << "Number of measurements: " << measure.measurementNum; cout << endl << "Date MM/DD/YYYY: " << measure.date;cout << endl << "Number of components tested: " << measure.numOfComponents; cout << endl << "Duration of experiment: " << measure.duration; cout << endl << "Temperature for test: " << measure.temperature; cout << endl << "Number (of MAXEXP) of failed components: " << measure.numFailures << endl << endl; }}

27 © Janice Regan, CMPT 128, February 2007 26 Structures as Function Arguments  A structure can be passed like any simple data type (whole structure or any element)  Pass-by-value  Pass-by-reference (more later)  Or combination  Can also be returned by function  Return-type is the structure type  Return statement in function definition sends structure variable back to caller

28 © Janice Regan, CMPT 128, February 2007 27 Pass by value (C or C++) bool compareLabMeasurements(labMeasurement meas1, labMeasurement meas2) { if(meas1.measurementNum !=meas2.measurementNum ) { return false; } if(meas1.date != meas2.date ) {return false; } if(meas1.numOfComponents != meas2.numOfComponents ) { return false; } if(meas1.duration != meas2.duration ) {return false; } if(meas1.numFailures != meas2.numFailures ) {return false; } if(fabs(meas1.temperature - meas2.temperature) > DBL_EPSILON ) {return 1; } return true; }

29 © Janice Regan, CMPT 128, February 2007 28 Pass by reference (C++) bool compareLabMeasurements(labMeasurement &meas1, labMeasurement &meas2) { if(meas1.measurementNum !=meas2.measurementNum ) { return false; } if(meas1.date != meas2.date ) {return false; } if(meas1.numOfComponents != meas2.numOfComponents ) { return false; } if(meas1.duration != meas2.duration ) {return false; } if(meas1.numFailures != meas2.numFailures ) {return false; } if(fabs(meas1.temperature - meas2.temperature) > DBL_EPSILON ) {return 1; } return true; }

30 © Janice Regan, CMPT 128, February 2007 29 Pass using pointers (C) int compareLabMeasurements(labMeasurement *meas1, labMeasurement* meas2) { if(meas1->measurementNum != meas2 -> measurementNum ) { return1; } if(meas1->date != meas2->date ) {return 1; } if(meas1->numOfComponents != meas2->numOfComponents ) { return 1; } if(meas1->duration != meas2->duration ) {return 1; } if(meas1->numFailures != meas2->numFailures ) {return 1; } if(fabs(meas1->temperature - meas2->temperature) > DBL_EPSILON ) {return 1; } return 0; }

31 © Janice Regan, CMPT 128, February 2007 30 Returning a structure labMeasurement readLabMeasurement(ifstream& fileInput) { labMeasurement measure; fileInput >> measure.measurementNum; fileInput >> measure.date; fileInput >> measure.numOfComponents; fileInput >> measure.duration; fileInput >> measure.temperature; fileInput >> measure.numFailures; return measure; }


Download ppt "© Janice Regan, CMPT 128, Feb.. 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Structures."

Similar presentations


Ads by Google