()
,
.
.
String:
String inBuf;
// read in a String object
while ( cin
>> inBuf ) // iterate over the elements of inBuf
{ char ch;
while (
ch = inBuf() ) // ... do something with ch }
: >>() String
operator().
String
:
istream& operator>>( istream& is,
String& s )
{ char inBuf[ STRING_SIZE ];
is >> inBuf;
s =
inBuf; // String::Operator=( char * )
return is; }
String char* String.
String::Operator=(char*).
String& String::Operator=( const char *s
)
{ len = strlen( s );
delete str;
str = new char[ len + 1
];
strcpy( str, s );
return *this; }
() ,
.
, 0.
:
while ( ch = inBuf() ) // ... code
String -
index. index , .
String index 0.
char
String::Operator() ()
{ // provide for an iterator operator
if ( index
< len ) return str[ index++ ];
// still here? completed
iteration
return ( index = 0 );
}
,
String, ,
.
#include "String.h"
const LINESIZE = 40;
enum
{PERIOD=.,COMMA=,,SEMI=;,COLON=:};
main() {
String inBuf(
STRING_SIZE );
int lineSize = 0;
// operator>>( istream&,
String& )
while ( cin >> inBuf ){
char ch;
int i =
0;
// String::Operator() ()
while ( ch = inBuf() ) { switch(ch)
{
case PERIOD: case COMMA: case SEMI: case COLON:
//
String::Operator[](int);
inBuf[ i ] =
�;
break;
}
++i;
++lineSize;
}
if ( lineSize >=
LINESIZE )
{ cout << "n";
lineSize = 0;
}
cout << inBuf
<< " ";
}
cout << "n";
}
:
We were her pride of ten; she named us: benjamin,
phoenix, the prodigal, and perspicacious, pacific Suzanne. Benjamin, hush now.
Be still, child. People are never just.
:
We were her pride of
ten she named us benjamin phoenix the prodigal and perspicacious pacific Suzanne
Benjamin hush now Be still child People are never just
new
delete
new
( 5.1). ,
. ,
StringList.
#include "String.h"
#include "StringList.h"
// maintain
a pointer table indexed by length
const maxLen = 25;
StringList *stbl[
maxLen ];
main() {
// read in and sort strings by length
const
inBuf = 512;
char st[ inBuf ] StringList *p;
while ( cin >> st
)
{ p = new StringList( st );
int sz = p->getLen();
if ( sz >=
maxLen ) // issue error message continue;
p->next = stbl[ sz ];
stbl[
sz ] = p;
}
for ( int i = maxLen - 1; i > 0; --i )
{ StringList
*tp;
p = stbl[ i ];
while ( p != 0 )
{ cout << *p <<
"n";
tp = p;
p = p->next;
delete tp;
}} }
StringList
, String, entry
String, next.
char*. String
entry.
StringList::StringList( char *s ) : entry( s ), next( 0 ) {
}
getLen() .
entry.
StringList::getLen() { return entry.getLen(); }
String() getLen() StringList
String.
StringList
:
StringList::getLen() { // illegal: private member
retrun
entry.len }
StringList:
ostream& operator << ostream& os,
StringLien& s )
{ retrun ( os << s.entry ); }
:
A class may provide new and delete
operator functions
:
functions operator provide delete class and
new may A
,
.
. , new
. ,
24 -
.
new delete . ,
.
.
,
StringList,
StringList. .
freeStore stringChunk:
class StringList {
private:
enum { stringChunk = 24; }
static StringList * freeStore;
//
... };
stringChunk StringList,
. freeStore
StringList.
new
void* typedef
size_t, stddef.h.
.
new,
. 5.3
new. new
. ,
; .
new
.
new
StringList. freeStore.
, . ,
new stringChunk .
:
#include <stddef.h>
StringList
*StringList::freeStore = 0;
void *StringList::Operator new( size_t size
)
{ register StringList *p;
// if the free store is exhausted
// grab a
new chunk of memory
if ( !freeStore )
{ long sz = StringChunk *
size;
freeStore = p = new char[ sz ];
// the global new operator
(StringList *)
// initialize the StringList freeStore
for ( ; p !=
&freeStore[ StringChunk-1 ]; p->next = p+1, p++ );
p->next =
0;
}
p = freeStore;
freeStore = freeStore->next;
return p;
}
delete StringList
. :
void StringList::Operator
delete( void *p, size_t )
{// restore p to
freeStore
((StringList*)p)->next = freestore;
freeStore = (StringList
*)p; }
delete void*.
typrdef
size_t ( stddef.h). ,
, .
delete void.
new
, . ,
StringList *p = new
StringList;
new StringList,
StringList *pia = new StringList[10];
,
. , ,
StringList s;
new.
new
. ,
StringList *ps = ::new
StringList;
new .
,
::delete ps;
delete
. new delete
, .
, , this
. ( 7.6 ).
(new)
(delete).
X::Operator=(const X&)
;
, 7.2 .
:
X& X::Operator=( const X& );
. ,
String:
String article( "the" );
String
common( "For example" );
common = srticle;
:
String& String::Operator=( const String& s ) {
len = s.len;
str = s.str;
index = s.index;
}
, .
1.
article common
. String
.
2. , For example
. .
3.
String
. 7.3 index String,
String.
index 0
String - , , .
String.
. String
:
String& String::Opperator=( const
String& s )
{ index = 0;
len = s.len;
delete str;
str = new
char[ len + 1 ];
strcpy( str, s.str );
return *this; }
StringList,
, String, entry.
StringList .
StringList StringList,
entry.
,
#include "StringList.h"
main() {
StringList sl1( "horse"
);
StringLIst sl2( "carriage" );
// sl1.next = sl2.next
//
sl1.entry.String::Operator=(sl2.entry) sl1 = sl2;
}
StringList
, String
Stirng. ,
StringList& StringList::Operator=( const
String& s ) {
// String::Operator=(const String&)
entry =
s.entry;
next = 0;
return *this; }
entry =
s.entry;
, - entry
.
, .
.
, :
class X {
public:
X();
X( int );
X( const X& );
X& operator=( const
X& );
// ... class Y { public:
Y();
private:
X
x;
};
Y:
Y::Y() { x = 0; }
X X:
1.
X::X()
Y x . 0 x
X
, int.
:
2.
X::X( int );
0 X. (
7.5
).
3. X X
X::Operator=( const X& )
Y
x:
Y::Y() : x( 0 ) {}
X::X( int )
Y.
->
(->)
,
.
,
-> e . ,
String*.
#include "String.h"
class Xample {
public: String *operator->();// ...
private: String *ps; // ...
};
E . ps
, .
String*
Xample::Operator->()
{ if ( ps == 0 ) // ... initialize ps
// ...
process ps
return ps; }
Xample.
,
void ff( Xample x, Xample &xr, Xample *xp )
{ int
i;
// invoke String::getLen()
i = x->getLen(); // ok:
x.ps->getLen()
i = xr->getLen(); // ok: xr.ps->getLen()
i =
xp->getLen(); // error: no Xample::getLen()
Xample
.
(=) (&).
.
.
.
?
.
.
. +, , . +
.
, String
+.
. isEmpty()
NOT,
operator!();
isEqual
,
operator==();
copy()
,
operator=();
..
. String,
, :
String s1( "C"
);
String s2( "++" );
s1 = s1 + s2; // s1 <== "C++"
, , :
s1
+= s2;
.
7-15.
Screen, 6
.
7-16. ,
.
7-17. 4.2
- , IntList.
.
: isEmpty(), isEqual(), print(), addNode(),
build(), copy() deleteNode().
7-18.
?
?
7-19. ,
.
7-20. INodes
BinTree. ,
new delete.