1 / 22

Chapter 11- 2 Structured Types, Data Abstraction and Classes

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

genna
Download Presentation

Chapter 11- 2 Structured Types, Data Abstraction and Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 . . .

  3. 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)

  4. 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

  5. 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

  6. 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; };

  7. 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

  8. 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

  9. 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)

  10. 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?

  11. Data Type set of values (domain) allowable operations on those values FOR EXAMPLE, data type int has operations +, -, *, /, %, >>, << domain -32768 . . . 32767

  12. 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 . . .

  13. 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

  14. 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

  15. 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

  16. 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

  17. Some Possible Representationsof ADT ComplexNumber struct with 2 float members 2-element float array -16.2 5.8 .real .imag -16.2 5.8

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

  19. 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

  20. 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

  21. 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

  22. The End of Chapter 11 – Part 2

More Related