220 likes | 379 Views
Chapter 11- 2 Structured Types, Data Abstraction and Classes. Dale/Weems. Hierarchical Structures. The type of a struct member can be another struct type This is called nested or hierarchical structures
E N D
Chapter 11- 2Structured Types,Data Abstraction and Classes Dale/Weems
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 . . .
struct MachineRec Information about each machine in a shop contains: an idNumber, a written description, the purchase date, the cost, and a history (including failure rate, number of days down, and date of last service)
struct DateType { int month; // Assume 1 . . 12 int day; // Assume 1 . . 31 int year; // Assume 1900 . . 2050 }; struct StatisticsType { float failRate; DateType lastServiced; // DateType is a struct type int downDays; }; struct MachineRec { int idNumber; string description; StatisticsType history; // StatisticsType is a struct DateType purchaseDate; float cost; }; MachineRec machine; 4
struct type variable machine 7000 5719 “DRILLING…” 3 21 1995 8000.0 .02 1 25 1999 4 .month .day.year .month .day .year .failrate .lastServiced .downdays .idNumber .description . history .purchaseDate .cost machine.history.lastServiced.yearhas value 1999
Unions in C++ DEFINITION A union is a struct that holds only one of its members at a time during program execution. EXAMPLE union WeightType { long wtInOunces; int wtInPounds; Only one at a time float wtInTons; };
Using Unions union WeightType // Declares a union type { long wtInOunces; int wtInPounds; float wtInTons; }; WeightType weight; // Declares a union variable weight.wtInTons = 4.83; // Weight in tons is no longer needed // Reuse the memory space weight.wtInPounds = 35; 7
Abstraction • Abstraction isthe separation of the essential qualities of an object from the details of how it works or is composed • Focuses on what, not how • Is necessary for managing large, complex software projects
Control Abstraction • Constrol abstraction separates the logical properties of an action from its implementation Search (list, item, length, where, found); • The function call depends on the function’s specification (description), not its implementation (algorithm)
Data Abstraction • Data abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES IMPLEMENTATION What are the possible values? How can this be done in C++? What operations will be needed? How can data types be used?
Data Type set of values (domain) allowable operations on those values FOR EXAMPLE, data type int has operations +, -, *, /, %, >>, << domain -32768 . . . 32767
Abstract Data Type (ADT) • An abstract data type isa data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how) For example . . .
ADT Specification Example TYPE Time DOMAIN Each Time value is a time in hours, minutes, and seconds. OPERATIONS Set the time Print the time Increment by one second Compare 2 times for equality Determine if one time is “less than” another
Another ADT Specification TYPE ComplexNumber DOMAIN Each value is an ordered pair of real numbers (a, b) representing a + bi OPERATIONS Initialize the complex number Write the complex number Add Subtract Multiply Divide Determine the absolute value of a complex number
ADT Implementation • ADT implementation • Choose a specific data representation for the abstract data using data types that already exist (built-in or programmer-defined) • Write functions for each allowable operation
Several Possible Representations of ADT Time “10” “45” “27” 10 45 27 10 45 27 3 int variables 3 strings 3-element int array Choice of representation depends on time, space, and algorithms needed to implement operations
Some Possible Representationsof ADT ComplexNumber struct with 2 float members 2-element float array -16.2 5.8 .real .imag -16.2 5.8
floating address float double long double pointer reference C++ Data Types simple structured integral enum array struct union class char short int long bool
class Time Specification // Specification file (Time.h) class Time // Declares a class data type {// does not allocate memory public : // Five public function members void Set (int hours ,int mins , int secs); void Increment (); void Write () const; bool Equal (Time otherTime) const; bool LessThan (Time otherTime) const; private : // Three private data members int hrs; int mins; int secs; }; 19
C++ classType • Facilitates re-useof C++ code for an ADT • Software that uses the class is called a client • Variables of the class type are called class objects or class instances • Client code uses class’s public member functions to manipulate class objects
Client Code UsingTime #include “time.h” // Includes specification of the class using namespace std; int main () { Time currentTime; // Declares two objects of Time Time endTime; bool done = false; currentTime.Set (5, 30, 0); endTime.Set (18, 30, 0); while (! done) { . . . currentTime.Increment (); if (currentTime.Equal (endTime)) done = true; }; } 21