Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures putting data together.

Similar presentations


Presentation on theme: "Structures putting data together."— Presentation transcript:

1 Structures putting data together

2 Structure Definitions
aggregate construct allows to manipulate several data items as a single whole structures are an aggregate construct; what other aggregate constructs have we studied? example: bank’s certificate of deposit (CD) - has balance (int), rate (int) and months until maturity (int) structure definition: struct CDAacct{ int balance; int rate; int term; }; note the semicolon at the end of the structure definition! elements of the structure are called members or member variables structure definition is not executable – good candidate for a header file members of the same structure must have different names, different structures can contain members with the same name that is: the scope of member name is the structure definition

3 Preprocessor Directives (Review)
each definition (e.g. global constant def.) can be encountered only once during compilation when definition is placed in a header file, it may be included multiple times header file must structured so it is safe in case of multiple inclusion; term – multiple inclusion protection mechanism - preprocessor directives #define name value note that substitution is textual problem: #define press 50+5 int myvar = press * 20; changes order of operations, do not use #define instead of global constants #ifdef name - true if name defined, #ifndef name - true if not #endif - completes #if header file myheader.h containing definitions usually has the following structure: #ifndef MYHEADER_H #define MYHEADER_H // text of the header file goes here #endif

4 Structure Variables when structure CDAcct is defined, structure variables can be declared as any other variables: CDAcct myacct, youracct; each structure variable of CDAcct contains three member variables programmer can refer to member variable by specifying structure variable name “dot” member variable name for example myacct contains: myacct.balance myacct.rate myacct.term member variables are of type specified in the structure definition member variables can be used as any other (scalar) variables myacct.balance=1000; myacct.rate = youracct.rate+1; cin >> myacct.term;

5 Assigning Values to Structure Variables
initialization structure variable can be initialized: CDAcct myacct={1000, 9, 12}; order of initialization corresponds to the order member of member variables in structure definition more values than variable members – error less values than variable members – the rest initialized to 0 structure variable can be assigned the value of another structure variable of the same structure type CDAcct youracct; youracct=myacct; structure variables cannot be directly compared if (youracct == myacct) // error

6 Passing Structures as Parameters, Returning Structures
structures can be passed by value and by reference, a function can return a structure: CDAccount setCD(int b, int r, int t){ CDAccount tmp; tmp.balance=b; tmp.rate=r; tmp.term=t; return tmp; }

7 Complex Structures a member may be of basic type or of type structure (referred to as substructure): // example structure struct example{ int a; string b; }; // example complex structure struct compExample{ int c; example d; // substructure if there is a declaration compExample me; how do we access substructure member variables?

8 Structures and Arrays a member may be an array
struct exampleWArray{ // complex structure definition int a; int b[5]; // member array }; exampleWArray se; // declaring structure variable se.a = 123; se.b[3]=456; // accessing members an array of structures may be declared struct example{ //structure string b; example as[4]; // declaring array of structures as[3].a=123; as[3].b=”Hello”; // accessing members how do we access characters of string b? more complicated constructs (arrays of structures with substructures, etc.) are possible


Download ppt "Structures putting data together."

Similar presentations


Ads by Google