Chapter 4 Implementing
ADTs
In
The base language
• 4.1 The aggregate type struct
• 4.2 structure pointer Operator
• 4.3 anexamp1e: Stack
• 4.4 unions
• 4.5 Complex Numbers
• 4.6 example: A flush
• 4.7 bit Fields
• 4.8 An example: Two-dimen1_dynamic Arrays
• 4.9 pragmatics
• Summary
• Exercises
Chapter 4 Implementing
ADTs
In
The base language
• This chapter introduces the reader to struct
S t r u C t
As used in the kernel language.
• A large part of the OOP Design Process
Involves thinking up the appropriate ADTs
For a problem. Good ADTs not only model
Key features of the problem, but also are
Frequently reusable in other code.
4.1 The aggregate type struct
• The structure type allows the programmer
To aggregate components into a single
Named variable. A structure has
Components, called members,
M e m be r s,
That are
Individually named.
• Demo on page 108: poker1.cpp
-In C ++, the structure name, or tag name,
T a G n a m e,
Is
Type.
4.1 The aggregate type struct
-The Declaration struct card can be thought of
A blueprint; it creates the type card, but no
Actual instances are allocated.
Card c l, C2;
C ar d c l, C 2;
-Access members
A C E S M E M B E R S
Structure-Variable
S t r u C t u r e-V A R I A B L E
. Member-name
M e m B e r-n a m e
4.1 The aggregate type struct
-A unique structure variable identifier, there is no
Confusion between two members that have
Same name in different structures.
4.1 The aggregate type struct
-In general, a structure is declared with
Keyword s t r u C t followed by an identifier (tag
Name), followed by a brace-enclosed list
Member declarations. The tag name is optional
But shoshould Be expressive of the ADT Concept
Being modeled. When the tag name is not
Present, the structure declaration is anonymous
A n o n y m O U S
And can be used only to declare variables of that
Type immediately, as in:
4.2 structure pointer Operator
• We have already seen the use of the member
Operator. in accessing members.
• In this section we introduce the structure pointer
Operator->.
• C ++ provides the structure pointer operator->
Access the members of a structure via a pointer.
Pointer-to-structure
P o I n t e r-t o-s t r u C t u r e
-> Member-name
M e m B e r-n a m e
(
(
*
*
Pointer-to-structure)
P o I n t e r-t o-s t r u C t u r e)
. Member-name
M e m B e r-n a m e
4.2 structure pointer Operator
• Example code
4.3 anexamp1e: Stack
• The stack
S t AC K
Is one of the most useful Standard
Data structures.
•
Data structure whose chief purpose is
Hold information is called a container.
C On T ai n e r.
•
A
Stack allows as operations push, Pop, top,
Empty, and full.
• The stack is a typical ADT.
4.3 anexamp1e: Stack
• Demo on page 111: ch_statc1.h
• Demo on Page 114: ch_stac1.cpp
-Decleration
-Initialization
-Opration Definition
4.4 unions
• A union is a derived type whose syntax is
The same as for structures, doesn't that
Keyword Union replaces struct. The Member
Declarations share storage, and their values
Will be overlaid.
• Demo on page 115: Union. cpp
4.4 unions
•
Union can be anonymous, as in
Following code.
• Demo on page 115: weekend. cpp
-This allows the individual member identifiers
Be used as variables.
-Note that an anonymous Union declared in file
Scope must be static.
4.4 unions
• Let us develop a stack as a union
Different types.
4.4 unions
4.5 Complex Numbers
• Define scientific computations require
Complex Numbers. Let us write an ADT
Complex Numbers.
• Demo on page 117: complex1.cpp
-Notice how the default argument is
Convenient way to assign a double to
Complex.
-The following Code adds a real number and
Complex Number.
-Can't use conventional expression notation
4.5 Complex Numbers
4.6 example: A flush
• We Want to estimate the probability
Being dealt a flush. A flush occurs when
Least Five cards are of the same suit. We
Simulate shuffling cards by using a random
Number Generator to shuffle the deck.
• Demo on page 119: poker1.cpp
-Structure deceleration and definition
-Operation deceleration and definition
4.6 example: A flush
4.6 example: A flush
4.6 example: A flush
4.6 example: A flush
4.6 example: A flush
• Demo on page 121-124: poker1.cpp
4.7 bit Fields
• A member that is an integral type can
Consist of a specified number of BITs. Such
Member is called a bit field
Bi t f I e l d
, And the number
Of associated bits is called its width
W I dt H
.
4.7 bit Fields
• The compiler will attempt to pack the bit
Fields sequentially within memory.
• It is at liberty to skip to a next byte or word
For purposes of alignment.
• Arrays of bit fields are not allowed
N ot al l ow e d
.
• Also, the address operator &
&
Cannot
C an n ot
Be
Applied to bit fields.
• Demo on page 125: Set. cpp
4.7 bit Fields
• We can overlay word and unsigned within
Union to create a data structure
Manipulating bits:
4.8 An example: Two-dimenple
Dynamic Arrays
• The Kernel Language does not allow general
Dynamic
Dy n am I c
Multidimen1_arrays
.
• Scientists, engineers, and others make
Heavy use of general two-dimensional
Arrays called Matrices
M at R I C E S
.
• Demo on page 126: array_2d.cpp
4.8 An example: Two-dimenple
Dynamic Arrays
• The pointer Base
Bas E
Is a pointer
Double.
• The base pointer contains the starting
Address of an array of pointers, and each
Pointer is a starting address of a row
Doubles.
4.8 An example: Two-dimenple
Dynamic Arrays
4.8 An example: Two-dimenple
Dynamic Arrays
• Notice in the following code how
A11ocate () uses new to allocate,
4.8 An example: Two-dimenple
Dynamic Arrays
• Demo on page 129: array_2d.cpp
4.9 pragmatics
• The use of s t r u c t in the Kernel Language
Is in each ways superseded by creating
ADTs using the class construct with private
Access for hiding implementation and
Member functions for implementing ADT
Behavior.
• The material here is transitional and allows
The programmer to understand more
Primitive encapsulation techniques that are
Still in widespread use.
Summary
• 1. A structure is a declaration using
Keyword struct.
• 2. The structure type allows the programmer
To aggregate components into a single
Named variable.
• 3.
A union is a derived type whose syntax is
The same as for structures, doesn't that
Keyword Union replaces struct.
• 4. A member that is an integral type can
Consist of a specified number of bits.
Summary
• 5. The operator new can be used to create
Run-time determined two-dimened (and
Higher) arrays. functions using new can
Work regardless of the row and column size.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.