chapter 11- 1 structured types, data abstraction and classes
DESCRIPTION
Chapter 11- 1 Structured Types, Data Abstraction and Classes. Dale/Weems. Chapter 11 Topics. 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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/1.jpg)
1
Chapter 11- 1
Structured Types,Data
Abstraction and Classes
Dale/Weems
![Page 2: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/2.jpg)
2
Chapter 11 Topics 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
![Page 3: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/3.jpg)
3
C++ Data TypesC++ Data Types
structured
array struct union class
address
pointer reference
simple
integral enum
char short int long bool
floating
float double long double
![Page 4: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/4.jpg)
4
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 Each component can be accessed individually Used to bundle together related data of various
types for convenient access under the same identifier
For example . . .
![Page 5: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/5.jpg)
5
thisAnimal
5000
.id 2037581
.name “giant panda”
.genus “Ailuropoda”
.species “melanoluka”
.country “China”
.age 18
.weight 234.6
.health Good
![Page 6: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/6.jpg)
6
anotherAnimal
6000
.id 5281003 .name “llama”
.genus “Lama”
.species “peruana”
.country “Peru”
.age 7
.weight 278.5
.health Excellent
![Page 7: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/7.jpg)
7
struct AnimalTypeenum HealthType { Poor, Fair, Good, Excellent };struct AnimalType // 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; HealthType health;};// Declare variables of AnimalTypeAnimalType thisAnimal; AnimalType anotherAnimal; 7
![Page 8: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/8.jpg)
8
struct type DeclarationSYNTAX struct TypeName // Does not allocate memory {
MemberList };
MemberList SYNTAX
DataType MemberName;
DataType MemberName; . . .
![Page 9: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/9.jpg)
9
struct type DeclarationThe 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
![Page 10: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/10.jpg)
10
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
![Page 11: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/11.jpg)
11
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
EXAMPLESthisAnimal.weightanotherAnimal.country
![Page 12: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/12.jpg)
12
Operations on struct Members
The type of the member determines the allowable operations
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++;
![Page 13: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/13.jpg)
13
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
![Page 14: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/14.jpg)
14
Aggregate struct Operations
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)
Return as value of a function I/O, arithmetic, and comparisons of entire
struct variables are NOT ALLOWED!
![Page 15: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/15.jpg)
15
Aggregate struct Operations
anotherAnimal = thisAnimal; // Assignment
WriteOut(thisAnimal); // Value parameter
ChangeWeightAndAge(thisAnimal); // Reference parameter
thisAnimal = GetAnimalData(); // Function return value
![Page 16: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/16.jpg)
16
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);}
![Page 17: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/17.jpg)
17
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
![Page 18: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/18.jpg)
18
AnimalType GetAnimalData ()
// Obtains all information about an animal from keyboard// Postcondition:// Return value == AnimalType members entered at kbd{
AnimalType thisAnimal; char response; do
{ // Have user enter members until they are correct
.
.
.
} while (response != ‘Y’); return thisAnimal;
} 18
![Page 19: Chapter 11- 1 Structured Types, Data Abstraction and Classes](https://reader035.vdocuments.us/reader035/viewer/2022062305/568167ab550346895ddcf956/html5/thumbnails/19.jpg)
19
The End of Chapter 11 – Part 1