Programming in C++ Examples of aggregate struct operations anotherAnimal= thisAnimal ∥ assignment WriteOut(thisAnimal); ∥ value parameter ChangeAge(thisAnimal) ∥ reference parameter thisAnimal= GetAnimalData(: //return value of function NOW WELL WRITE THE 3 FUNCTIONS USED HERE 16
16 Examples of aggregate struct operations anotherAnimal = thisAnimal ; // assignment WriteOut(thisAnimal); // value parameter ChangeAge(thisAnimal); // reference parameter thisAnimal = GetAnimalData( ); // return value of function NOW WE’LL WRITE THE 3 FUNCTIONS USED HERE . .
Programming in C++ void Writeout(/* in */AnimalType thisAnimal) l Prints out values of all members of thisAnimal // Precondition: all members of thisAnimal are assigned M Postcondition all members have been written out cout < D#<< thisAnimalid < thisAnimal name < end cout < thisAnimal genus < thisAnimal species < endl; cout < thisAnimal country < endl cout < thisAnimalage << years"<<endl cout < thisAnimal weight<<"lbs.<<endl; cout <<"General health: 3 Write Word( thisAnimalhealth );
17 void WriteOut( /* in */ AnimalType thisAnimal) // Prints out values of all members of thisAnimal // Precondition: all members of thisAnimal are assigned // Postcondition: all members have been written out { cout << “ID # ”<< thisAnimal.id << thisAnimal.name << endl ; cout << thisAnimal.genus << thisAnimal.species << endl ; cout << thisAnimal.country << endl ; cout << thisAnimal.age << “ years ” << endl ; cout << thisAnimal.weight << “ lbs. ” << endl ; cout << “General health : ” ; WriteWord ( thisAnimal.health ) ; }
Programming in C++ Passing a struct Type by Reference void ChangeAge(/*inout*/ AnimalType& thisAnimal) ∥ Adds 1 to age //Precondition: thisAnimal. age is assigned l/Postcondition: thisAnimal age = thisAnimal age(@entry 1 thisAnimalage++
18 void ChangeAge ( /* inout */ AnimalType& thisAnimal ) // Adds 1 to age // Precondition: thisAnimal.age is assigned // Postcondition: thisAnimal.age == thisAnimal.age@entry + 1 { thisAnimal.age++ ; } Passing a struct Type by Reference
Programming in C++ Animal Type GetAnimalData( void //Obtains all information about an animal from keyboard ∥ Postcondition: Function value = AnimalType members entered at kbd AnimalType thisAnimal char response 3 do t m have user enter all members until they are correct y while(response =Y); return thisAnimal 19
19 AnimalType GetAnimalData ( void ) // Obtains all information about an animal from keyboard // Postcondition: // Function value == AnimalType members entered at kbd { AnimalType thisAnimal ; char response ; do { // have user enter all members until they are correct . . . } while (response != ‘Y’ ) ; return thisAnimal ; }
Programming in C++ 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 20
20 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 . .