Chapter 11 1 Structured Types Data Abstraction and

Chapter 11 - 1 Structured Types, Data Abstraction and Classes Dale/Weems 1

Chapter 11 Topics l l l l Meaning of a Structured Data Type Declaring and Using a struct Data Type C++ union Data Type Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification and Implementation Files Invoking class Member Functions in Client Code C++ class Constructors 2

C++ Data Types simple integral enum structured floating array struct union class char short int long bool float double long double address pointer reference 3

Structured Data Type A structured data type is a type in which each value is a collection of component items The entire collection has a single name n Each component can be accessed individually n Used to bundle together related data of various types for convenient access under the same identifier n For example. . . 4

this. Animal 5000. id 2037581 . name “giant panda” . genus “Ailuropoda” . species “melanoluka” . country “China” . age 18 . weight 234. 6 . health Good 5

another. Animal 6000. id 5281003 . name “llama” . genus “Lama” . species “peruana” . country “Peru” . age 7 . weight 278. 5 . health Excellent 6

struct enum Health. Type Animal. Type { Poor, Fair, Good, Excellent }; struct Animal. Type // Declares a struct data type { // does not allocate memory long id; string name; string genus; string species; struct members string country; int age; float weight; Health. Type health; }; // Declare variables of Animal. Type this. Animal; Animal. Type another. Animal; 7 7

struct type Declaration SYNTAX struct Type. Name { Member. List }; Member. List // Does not allocate memory SYNTAX Data. Type Member. Name; . . . 8

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 9

More about struct type declarations Scope of a struct 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 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 10

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 11

Operations on struct Members The type of the member determines the allowable operations this. Animal. age = 18; this. Animal. id = 2037581; cin >> this. Animal. weight; getline (cin, this. Animal. species); this. Animal. name = “giant panda”; this. Animal. genus[0] = toupper(this. Animal. genus[0]); this. Animal. age++; 12

Aggregate Operation An aggregation operation is an operation on a data structure as a whole, as opposed to an operation on an individual component of the data structure 13

Aggregate struct Operations l Operations valid on struct type variables are Assignment to another struct variable of the same type § Pass as an argument (by value or by reference) § § l Return as value of a function I/O, arithmetic, and comparisons of entire struct variables are NOT ALLOWED! 14

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(); // Function return value 15

void Write. Out( /* in */ Animal. Type this. Animal) // Prints out values of all members of this. Animal // Precondition: all members of this. Animal are assigned // Postcondition: 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); } 16

Passing a struct Type by Reference void Change. Age(/* inout */ Animal. Type& this. Animal) // Adds 1 to age // Precondition: this. Animal. age is assigned // Postcondition: this. Animal. age == // this. Animal. age@entry + 1 { this. Animal. age++; } 17

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

The End of Chapter 11 – Part 1 19
- Slides: 19