1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems
2 Hierarchical Structures l The type of a struct member can be another struct type l This is called nested or hierarchical structures l 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 int day;// Assume int year; // Assume }; struct StatisticsType { float failRate; DateTypelastServiced;// 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.idNumber.description. history.purchaseDate.cost.month.day.year 5719 “DRILLING…” failrate.lastServiced.downdays month.day.year machine.history.lastServiced.year has 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 l Abstraction is the separation of the essential qualities of an object from the details of how it works or is composed n Focuses on what, not how n Is necessary for managing large, complex software projects
9 Control Abstraction l Constrol abstraction separates the logical properties of an action from its implementation Search (list, item, length, where, found); l The function call depends on the function’s specification (description), not its implementation (algorithm)
10 Data Abstraction l Data abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIESIMPLEMENTATION 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 domain operations +, -, *, /, %, >>, <<
12 Abstract Data Type (ADT) l An abstract data type is a 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 l ADT implementation n Choose a specific data representation for the abstract data using data types that already exist (built-in or programmer-defined) n Write functions for each allowable operation
Several Possible Representations of ADT Time 3 int variables 3 strings 3-element int array Choice of representation depends on time, space, and algorithms needed to implement operations “10” “45” “27”
17 Some Possible Representations of ADT ComplexNumber struct with 2 float members 2-element float array real.imag
18 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double
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 l Facilitates re-use of C++ code for an ADT l Software that uses the class is called a client l Variables of the class type are called class objects or class instances l Client code uses class’s public member functions to manipulate class objects
21 Client Code Using Time #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