Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
C++ Container 4

C++ Container 4

Ratings: (0)|Views: 31 |Likes:
Published by toanthang87

More info:

Published by: toanthang87 on Nov 03, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

06/16/2009

pdf

text

original

 
Object-OrientedDesignand Programming C++ContainerClasseOutlin
IntroductioContainerClassObjectives ClassLibraryArchitecturParameterizedTypes PreprocessorMacros genclass 
voi
PointerMethod 
voi
PointerExampl
Introductio
 
Containerclassesareanimportantcate- goryofADT
 
Theyareusedtomaintaincollectionsofele- mentslikestacks,queues,linkedlists,tablestrees,etc. 
 
ContainerclassesformthebasisforvariousC++classlibraries 
 
Note,makingclasslibrariesisapopularwaytlearnC+
::
 
C++containerclassescanbeimplemented usingseveralmethods: 
0 
Adhoc
rewritefromscratcheachtim1PreprocessorMacros 2
genclass 
Facility 
e.g
,GNUlibg 
+
 3ParameterizedTypes 4 
voi
PointerMethod 
 
Note,methods
3applyt
homogeneous 
collections;method4allow
heterogeneous 
collection
ContainerClassObjectives 
 
ApplicationIndependence 
 
Transparentlyreusecontainerclasscodefor variousapplication
 
EaseofModication 
 
Relativelyeasytoextendclassestotsmoothlintoanewapplication 
 
EaseofManipulation 
 
Implementationmusthiderepresentationde- tails, 
e.g
,iterators 
ContainerClassObjectives cont'd 
 
TypeSafet
 
Insurethatthecollectionsremaintypesaf
 
Thisiseasyforparameterizedtypes,harder fo
voi
pointer
::
 
Run-TimeEciencyandSpaceUtilizatio
 
Dierentschemeshavedierenttradeo
 
e.g
,extraindirectionvsexibilit
 
Object-OrientedClassLibrarArchitectur
 
Twogeneralapproachesar
tre
v
forest 
dierintheiruseofinheritance
Tree 
:createasinglerootedtreeofclassesde- rivedfromacommonbaseclass
e.g
object 
 
e.g
,standardSmalltalklibrariesorNIHC
Forest 
:acollectionofgenerallyindependentclasses availableforindividualselectionanduse 
 
e.g
,GNUlibg 
+
library,BorlandC++claslibrary,Boochcomponents,RogueWaveUSLStandardcomponent
 
Tradeos
1
Uniformit
Treevs 
exibility 
Forest 2
Sharing 
Treevs 
eciency 
Forest 
 
Forestclassesdonotinheritunnecessaryfunc- tion
Object-OrientedClassLibrarArchitecturecont'd 
Object Sorted Unsorted Container NumeriShape Vector Stack BaBinary SrchTreSquare Rectangle Circle 
 
Tree-basedclasslibrar
Object-OrientedClassLibrarArchitecturecont'd 
Vector Binary SrchTree Stack Queue BaHasTable SplayTree 
 
Forest-basedclasslibrar
ParameterizedTypes 
 
Parameterizedlistclass 
template 
 
class 
 
class 
List 
publi
List 
voi
:head0 
f
voi
prependT&item 
Nod
 
 
*tem
ne
Nod
 
 
item
thi
 
head
thi
 
head 
temp; 
::
* 
private 
template 
 
class 
 
class 
Nod
private 
TvalueNod
 
 
*next
publi
NodeT&v,Nod
 
 
*n :valuev,nextn 
f
Nod
 
 
*head
in
main 
voi
 
List 
 
in
 
list; list.prepend20;  
::
 
ParameterizedTypescont'd 
 
ParameterizedVectorclas
template 
 
class 
in
in
SIZ
10
 
class 
Vector 
publi
Vector 
voi
:sizeSIZE 
f
T
operator 
 
sizeti 
returnthi
 
bu
 
 
private 
Tbu
 
SIZ
 
sizetsize
in
main 
voi
 
Vector 
 
doubl
 
d;10
doubl
Vector 
 
in
,100
 
d;1000 
in
 
1
3.1416
PreprocessorMacros 
 
StackexampleusingGNU
+
 
 
ifndef 
stack 
dene 
stack 
dene 
name2a,bgEnErIc2a,b  
dene 
gEnErIc2a,ba 
dene 
StackTYPEname2TYPE,Stack  
dene 
StackDeclareTYPE 
 
class 
StackTYPE 
 
publi
 
StackTYPEsizetsize:sizesize 
 
thi
 
bottom 
ne
TYPE 
 
siz
 
 
thi
 
to
thi
 
bottom+size
 
 
TYPEpop 
voi
 
 
retur
thi
 
to
+
 
 
voi
pushTYPEitem 
 
-
thi
 
to
item
 
 
bool 
isempty 
voi
 
 
returnthi
 
to
=
thi
 
bottom 
 
thi
 
size
 
 
bool 
isfull 
voi
 
 
returnthi
 
bottom 
=
thi
 
top
 
 
StackTYPE 
voi
 
deletethi
 
bottom
 
private 
 
TYPE*bottom
 
TYPE*top
 
sizetsize
 
 
endi
1
PreprocessorMacroscont'd 
 
Stackdriver 
 
includ
 
stream.
 
 
includ
"stack.h" StackDeclare 
char 
typedef 
Stack 
char 
CHARSTACK; 
in
main 
voi
 
constint 
STACKSIZE 
100; CHARSTACKsSTACKSIZE
char 
ccout 
 
"pleaseenteryournam
.
:"
whil
!s.isfull&&cin.getc&&c
 
n' s.pushccout 
 
"Yournamebackwardsi
.
:"
whil
!s.isempty cout 
 
s.popcout 
 
 
n"
 
Mainproblems
1Ugly;- 2Codebloa3Notintegratedwithcompiler 
1
genclass 
 
TechniqueusedbyGNUlibg 
+
 
Uses 
se
toperformtextsubstitution 
sed-e"sT$T1g"-e"sT&$T1$T1ACCg" 
 
SingleLinkedListclass 
class 
 
 
SLLis
publi
 
 
SLList 
voi
 
 
SLList 
 
 
SLList&a
~ 
 
SLList 
voi
 
 
SLList
operator 
 
 
 
SLList&a
in
empty 
voi
in
length 
voi
voi
clear 
voi
Pixprepend 
 
T
 
item; Pixappend 
 
T
 
item; 
::
* 
protected 
 
 
SLListNode*last
1

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->