Programming in C++ More about struct type declarations y 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 y It is common to place struct type declarations with TypeNames in a (h) header file and #include that file y 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
11 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 TypeNames 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
Programming in C++ 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 thisAnimal weight anotherAnimal country 12
12 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 thisAnimal.weight anotherAnimal.country
Programming in C+ Valid operations on a struct member depend only on its type thisAnimal age 18 thisAnimal id 2037581 cin > thisAnimal weight; getline(cin, thisAnimal species thisAnimal. name="giant panda thisAnimal genus[0]= toupper (thisAnimal genus[0]); thisAnimalage++ 13
13 Valid operations on a struct member depend only on its type thisAnimal.age = 18; thisAnimal.id = 2037581; cin >> thisAnimal.weight; getline ( cin, thisAnimal.species ); thisAnimal.name = “giant panda”; thisAnimal.genus[ 0 ] = toupper (thisAnimal.genus[ 0 ] ) ; thisAnimal.age++;
Programming in C++ Aggregate Operation is an operation on a data structure as a whole, as opposed to an operation on an individual component of the data structure 14
14 Aggregate Operation ❖is an operation on a data structure as a whole, as opposed to an operation on an individual component of the data structure
Programming in C++ Aggregate struct Operations lO, arithmetic, and comparisons of entire struct variables are NOT ALLOWED! s valid operations on an entire struct type variable i, assignment to another struct variable of same type ii. pass to a function as argument (by value or by reference) ii. return as value of a function 15
15 Aggregate struct Operations ❖ I/O, arithmetic, and comparisons of entire struct variables are NOT ALLOWED! ❖ valid operations on an entire struct type variable: i. assignment to another struct variable of same type ii. pass to a function as argument (by value or by reference) iii.return as value of a function