Programming in C DaleWeemsHeadington Chapter 14 Records C

  • Slides: 28
Download presentation
Programming in C++ Dale/Weems/Headington Chapter 14 Records ( C++ Structs ) 1

Programming in C++ Dale/Weems/Headington Chapter 14 Records ( C++ Structs ) 1

C++ Data Types Simple Structured Integral Floating array struct union class char short int

C++ Data Types Simple Structured Integral Floating array struct union class char short int long enum float double long double Address pointer reference 2

Limitation of array type In an array all elements (components) must be of the

Limitation of array type In an array all elements (components) must be of the same type. For example, 20 chars or 52 floats or 100 strings. l Often we have related information of various types that we’d like to store together for convenient access under the same identifier. For example. . . 3

another. Animal 6000. id 5281003 . name ‘l’ ‘a’ ‘m’ ‘a’ ‘�’ . .

another. Animal 6000. id 5281003 . name ‘l’ ‘a’ ‘m’ ‘a’ ‘’ . . genus ‘L’ ‘a’ ‘m’ ‘a’ ‘’ . . species ‘p’ ‘e’ ‘r‘ ‘u’ ‘a’ ‘n’ ‘a’ ‘’ . . country ‘P’ ‘e’ ‘r‘ ‘u’ ‘’ . . age 7 . weight 278. 5 . health Excellent 5

struct Animal. Type typedef char String 20 [ 21 ] ; enum Health. Type

struct Animal. Type typedef char String 20 [ 21 ] ; enum Health. Type { Poor, Fair, Good, Excellent } ; struct Animal. Type { long id ; String 20 name ; String 20 genus ; String 20 species ; String 20 country ; int age ; float weight ; Health. Type health ; }; Animal. Type // declares a struct data type // does not allocate memory struct members this. Animal ; // declare variables of Animal. Type 6 another. Animal ;

struct type declaration SYNTAX struct Type. Name { Member. List }; Member. List //

struct type declaration SYNTAX struct Type. Name { Member. List }; Member. List // does not allocate memory SYNTAX Data. Type Member. Name ; . . . 7

struct type declaration The struct declaration names a type and names the members of

struct type declaration The struct declaration names a type and names the members of the struct. It does not allocate memory for any variables of that type! You still need to declare your struct variables. 8

More about struct type declarations If the struct type declaration precedes all functions it

More about struct type declarations If the struct type declaration precedes all functions it will be visible throughout the rest of the file. If it is placed within a function, only that function can use it. It is common to place struct type declarations with Type. Names in a (. h) header file and #include that file. It is possible for members of different struct types to have the same identifiers. Also a non-struct variable may have the same identifier as a structure member. 9

Accessing struct members Dot ( period ) is the member selection operator. After the

Accessing struct members Dot ( period ) is the member selection operator. After the struct type declaration, the various members can be used in your program only when they are preceded by a struct variable name and a dot. EXAMPLES this. Animal. weight another. Animal. country 10

Valid operations on a struct member depend only on its type this. Animal. age

Valid operations on a struct member depend only on its type this. Animal. age = 18; this. Animal. id = 2037581; cin >> this. Animal. weight; cin. get ( this. Animal. species, 21 ); strcpy ( this. Animal. name, “giant panda” ); this. Animal. genus[ 0 ] = toupper (this. Animal. genus[ 0 ] ) ; this. Animal. age++; if ( strcmp ( this. Animal. country, “Ceylon” ) == 0 ) strcpy ( this. Animal. country, “Sri Lanka” ); 11

Aggregate struct operations l I/O and comparisons of entire struct variables are NOT ALLOWED!

Aggregate struct operations l I/O and comparisons of entire struct variables are NOT ALLOWED! l Operations valid on an entire struct type variable: assignment to another struct variable of same type, pass to a function as parameter (by value or by reference), return as value of a function. 12

Examples of aggregate struct operations another. Animal = this. Animal ; // assignment Write.

Examples of aggregate struct operations another. Animal = this. Animal ; // assignment Write. Out(this. Animal); // value parameter Change. Weight. And. Age(this. Animal); // reference parameter this. Animal = Get. Animal. Data( ); // return value of function NOW WE’LL WRITE THE 3 FUNCTIONS USED HERE. . . 13

void Write. Out( /* in */ Animal. Type this. Animal) // Prints out values

void Write. Out( /* in */ Animal. Type this. Animal) // Prints out values of all members of this. Animal // Precondition: // Postcondition: all members of this. Animal are assigned all members have been written out { cout << “ID # “ << this. Animal. id << this. Animal. name << endl ; cout << this. Animal. genus << this. Animal. species << endl ; cout << this. Animal. country << endl ; cout << this. Animal. age << “ years “ << endl ; cout << this. Animal. weight << “ lbs. “ << endl ; cout << “General health : “ ; Write. Word ( this. Animal. health ) ; } 14

void Change. Weight. And. Age( /* inout */ Animal. Type& this. Animal) // Accepts

void Change. Weight. And. Age( /* inout */ Animal. Type& this. Animal) // Accepts weight from keyboard and adds 1 to age // Precondition: this. Animal. age is assigned // Postcondition: this. Animal. age == this. Animal. age at entry + 1 // && this. Animal. weight == value entered at keyboard { String 20 weight. Str ; double value ; char response ; this. Animal. age++ ; do { cout << “Enter weight for ID# “ << this. Animal. id << “ : “ ; cin. get ( weight. Str, 21 ) ; cin. ignore (80, ‘n’ ) ; // consume newline value = atof (weight. Str) ; // convert string to double cout << “You entered: “ << value << endl ; cout << “Is this correct (Y/N) ? “ << endl ; cin >> response ; response = toupper (response) ; } while ( response != ‘Y’ ) ; this. Animal. weight = float (value ) ; } 15

Animal. Type Get. Animal. Data ( void ) // Obtains all information about an

Animal. Type Get. Animal. Data ( void ) // Obtains all information about an animal from keyboard // Precondition: None // Postcondition: // Function value == Animal. Type members entered at kbd { Animal. Type this. Animal ; char response ; do { // have user enter all members until they are correct. . . } while (response != ‘Y’ ) ; return this. Animal ; } 16

Array of structures const int MAX_SIZE = 500 ; enum Health. Type { Poor,

Array of structures const int MAX_SIZE = 500 ; enum Health. Type { Poor, Fair, Good, Excellent } ; struct Animal. Type { long id ; String 20 name ; String 20 genus ; String 20 species ; String 20 country ; int age ; float weight ; Health. Type health ; }; Animal. Type bronx. Zoo [ MAX_SIZE ] ; // declares struct data type // 8 struct members // declares array 17

Animal. Type bronx. Zoo[MAX_SIZE] ; bronx. Zoo [0] [1] . . . bronx. Zoo

Animal. Type bronx. Zoo[MAX_SIZE] ; bronx. Zoo [0] [1] . . . bronx. Zoo [ 0 ]. id 3456219 bronx. Zoo [ 0 ]. name “camel” bronx. Zoo [ 0 ]. genus “Camelus” bronx. Zoo [ 0 ]. species “dromedarius” bronx. Zoo [ 0 ]. country “India” bronx. Zoo [ 0 ]. age 10 bronx. Zoo [ 0 ]. weight 992. 8 bronx. Zoo [ 0 ]. health Fair [ 498 ] [ 499 ] 18

Animal. Type bronx. Zoo[MAX_SIZE] ; . id . name . genus . species .

Animal. Type bronx. Zoo[MAX_SIZE] ; . id . name . genus . species . country. age. weight. health bronx. Zoo [ 0 ] 3456219 “camel” “Camelus”“dromedarius” “India” 10 992. 8 Fair bronx. Zoo [ 1 ] bronx. Zoo [ 2 ] bronx. Zoo [ 3 ]. . . bronx. Zoo[498] bronx. Zoo[499] 19

Add 1 to the age member of each element of the bronx. Zoo array

Add 1 to the age member of each element of the bronx. Zoo array for ( j = 0 ; j < MAX_SIZE ; j++ ) bronx. Zoo[ j ]. age = bronx. Zoo[ j ]. age + 1 ; OR, for ( j = 0 ; j < MAX_SIZE ; j++ ) bronx. Zoo[ j ]. age++ ; 20

Write id and genus for each element of bronx. Zoo array from country “China”

Write id and genus for each element of bronx. Zoo array from country “China” for ( j = 0 ; j < MAX_SIZE ; j++ ) if ( strcmp (bronx. Zoo[ j ]. country , “China” ) == 0 ) cout << bronx. Zoo[ j ]. id << bronx. Zoo[ j ]. genus << endl ; 21

Find total weight of all elements of the bronx. Zoo array float total =

Find total weight of all elements of the bronx. Zoo array float total = 0. 0 ; for ( j = 0 ; j < MAX_SIZE ; j++ ) total += bronx. Zoo[ j ]. weight ; 22

Hierarchical structures The type of a struct member can be another struct type. This

Hierarchical structures The type of a struct member can be another struct type. This is called nested or hierarchical structures. Hierarchical structures are very useful when there is much detailed information in each record. FOR EXAMPLE. . . 23

struct Machine. Rec Information about each machine in a shop contains: an id. Number,

struct Machine. Rec Information about each machine in a shop contains: an id. Number, a written description, the purchase date, the cost, and a history (including failure rate, number of days down, and date of last service). 24

struct Date. Type { int month ; int day ; int year ; };

struct Date. Type { int month ; int day ; int year ; }; // Assume 1. . 12 // Assume 1. . 31 // Assume 1900. . 2050 struct Statistics. Type { float fail. Rate ; Date. Type last. Serviced ; int down. Days ; }; // Date. Type is a struct type struct Machine. Rec { int id. Number ; String 50 description ; Statistics. Type history ; Date. Type purchase. Date ; float cost ; }; // Statistics. Type is a struct type Machine. Rec machine ; inventory [ NUM_MACHINES ] ; 25 // array of struct type

struct type variable machine 7000 5719 “DRILLING…” . 02 1 25 1996 4 3

struct type variable machine 7000 5719 “DRILLING…” . 02 1 25 1996 4 3 21 1995 8000. 0 . month. day. year. failrate . last. Serviced . id. Number. description. history . downdays. month. day. year . purchase. Date . cost machine. history. last. Serviced. year has value 1996 26

Unions in C++ DEFINITION A union is a struct that holds only one of

Unions in C++ DEFINITION A union is a struct that holds only one of its members at a time during program execution. EXAMPLE union Weight. Type { long wt. In. Ounces ; int wt. In. Pounds; float wt. In. Tons; } ; only one at a time 27

Using Unions union Weight. Type { long wt. In. Ounces ; int wt. In.

Using Unions union Weight. Type { long wt. In. Ounces ; int wt. In. Pounds; float wt. In. Tons; } ; // declares a union type Weight. Type weight; // declares a union variable weight. wt. In. Tons = 4. 83 ; // Weight in tons is no longer needed. Reuse the memory space. weight. wt. In. Pounds = 35; 28