CS202 Topic #21 Introduction to C++ Data Abstraction w/ Classes Topic #2.
Chapter 11- 2 Structured Types, Data Abstraction and Classes
description
Transcript of Chapter 11- 2 Structured Types, Data Abstraction and Classes
1
Chapter 11- 2
Structured 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 MachineRecInformation 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
.idNumber .description . history .purchaseDate .cost
.month .day .year
5719 “DRILLING…” 3 21 1995 8000.0
.failrate .lastServiced .downdays
.02 1 25 1999 4.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
Abstraction is the 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 operationson those values
FOR EXAMPLE, data type int has
domain
-32768 . . . 32767
operations
+, -, *, /, %, >>, <<
12
Abstract Data Type (ADT)
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
TYPETime
DOMAINEach Time value is a time in hours, minutes, and seconds.
OPERATIONSSet the timePrint the timeIncrement by one secondCompare 2 times for equalityDetermine if one time is “less than” another
14
Another ADT Specification
TYPEComplexNumber
DOMAIN
Each value is an ordered pair of real numbers (a, b) representing a + bi
OPERATIONSInitialize the complex numberWrite the complex numberAdd SubtractMultiplyDivideDetermine 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
10 45 27
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
“10” “45” “27”
17
Some Possible Representationsof ADT ComplexNumberstruct with 2 float members
2-element float array
-16.2 5.8
-16.2 5.8
.real .imag
18
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
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-use of 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 Using Time#include “time.h” // Includes specification of the classusing 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