Presentation is loading. Please wait.

Presentation is loading. Please wait.

91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction Structures are somewhat like an array in that.

Similar presentations


Presentation on theme: "91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction Structures are somewhat like an array in that."— Presentation transcript:

1 91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction Structures are somewhat like an array in that both consist of a collection of elements. The elements are names instead of being numbered, however, and need not all be of the same type. int [0] [1] [2] [3] [4] int double.student_no.year.program_code.GPA Array: all elements are of the same type elements are identified by position Structure: elements can be of different types elements are identified by name

2 91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 2 Defining A Structure Type Introducing structures into a program is a two step process. First a new type must be defined. Then variables of this type can be declared. struct student_data { int student_no; int year; int program_code; double GPA; }; // Note the semi-colon here!! The above declaration defines a new type called “student_data”. Variables of this type consist of four elements named “student_no” and so on. Now, in addition to the “built-in” types (int, double, char, bool, long), we have a new one that we’ve defined ourselves. Structure definitions are normally placed at the start of a program, so makng them globally accessible (the usual “scope” rules apply).

3 91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 3 Declaring Structure Variables Once a structure type has been defined, it may be used in declaring variables. All always, a variable consists essentially of a type name followed by one or more variable names. int a; // type = “int”, variable name = “a” student_data s1, s2; // type = “student_data” // variable names = “s1”, “s2” The above declarations give us a single “int” variable called “a” and two “student_data” variables called “s1” and s2”. int doubleint double int a s1 s2

4 91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 4 Structure Operations Structure variables may be assigned to other structure variables of the same type. s1 = s2; // legal - each element of s1 gets the // value of the corresponding element // of s2 Otherwise structures are like arrays in that one can’t do very much with them (unless we define the operations ourselves, which is beyond this course…). cout << s1; // illegal cin >> s2; // illegal if (s1 == s2) { // illegal... s1 = s1 + s2; // illegal

5 91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 5 The Dot Operator The individual elements of a structure variable may be accessed by using the ‘.’ (dot) operator. structure_var. name the name of the element to be accessed. a hard and fast name must be used - nice as it might be, a string variables are not permitted. a structure variable (or a reference to such a variable) Once an element has been selected, it may be used in any of the ways a simple variable of the same type might be used (exactly as with arrays and the [] operator). Thus s1.GPA may be used wherever a double variable is permitted. s1.GPA = 3.9; if (...) { // caught cheating s1.GPA -= 1.0;

6 91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 6 Structures and Functions (1) Structures may be passed to functions. Either call by value or call by reference may be used, but call by reference is more typical, and using call by value may produce a “structure passed by value” warning message. Call by reference is preferred because it is more efficient, especially when dealing the large structures. // the structure type is defined in the “global scope” struct point { double lat, lng; // note short form }; // in this case call be reference is essential because the // function is supposed to modify the variable supplied void read_position (point &pnt) {... } void main (void) { point pnt_1, pmt_2;... read_position (pnt1);...

7 91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 7 Structures and Functions (2) When call by reference is used only for efficiency, and the function does not modify the structure, it is a good idea to precede the parameter declation with “const”. This works exactly as it does with strings. The “const” indicates that the function does not modify the structure supplied, and the compiler will reject any attempts to do so. // computing the distance between two points does not // involve modifying either of them double find_distance (const point &pnt_1, const point &pnt_2) {... // if we put “pnt_1.lat = 0;” here, we’d get a compiler // error message (“cannot modify const object”). In this particular case, because the structure is so small, call by value would also be a reasonable choice. double find_distance (point pnt_1, point pnt_2) { See sample program dstnce3.cpp.

8 91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 8 Structures And Arrays The elements of an array may be structures (or other arrays…) and the elements of a structure may be arrays (or other structures). Whenever we have a structure, we can use the ‘.’ operator, and whenever we have an array, we can use the ‘[]’ operator. Assume: struct sample { int a [4]; double b[5]; point p; // point is as defined a few slides back… }; sample x, y[100]; // x is a structure of type “sample” // y is an array of such structues x.a is an array of integers x.a[1] is the second element of this array y[0] is a structure of type sample y[0].p is a structure of type point y[0].p.lat is a double etc.

9 91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 9 Brokerage Example A broker who buys and sells bonds needs a program to help him keep track of his inventory. The fact that we’re dealing with bonds doesn’t really matter. If you prefer, you can think in terms of, say, an ice cream dealer who buys and sells different types of ice cream. Bonds (as simplified for this example) are characterized by a maturity date (month and year) and a coupon rate (in per cent). If two bonds have the same maturity date and the same coupon rate, they are identical. Every new kind of bond added to the inventory is to be assigned a unique id. The program is to support three operations. The broker can add bonds to his inventory (necessary inputs: maturity date, coupon rate, number of units), list the bonds in the inventory (no inputs required) he has available, and sell bonds (necessary inputs: id of bond, number of units). The code which handles the list operation should behave reasonably if there are no bonds in the inventory. The sell operation has four possible outcomes. Either there is no bond having the specified id (error message) there are not enough units available (error message, no other action), the sale is possible but leaves some units unsold, or the sale completely wipes out the units available. See sample program broker.cpp.


Download ppt "91.166 Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction Structures are somewhat like an array in that."

Similar presentations


Ads by Google