string. ++, ,
( ) . (
, ). string
.
Sctring ,
. -
.
.
. String, ,
:
1. str, char*,
.
2. len, int, ,
str.
String, ,
str , len.
class String {
public:
String( int );
String( char* );
private:
int
len;
char *str;
};
,
, .
.
String:
String::String( char *s ) {
// #include
<string.h>
len = strlen( s );
str = new char[ len + 1 ];
strcpy(
str, s );
}
. ,
,
. strlen() strcpy()
++ .
String. str
s:
str = s;
, s.
,
, , s :
-
, .. ,
, - - str
. ,
String *readString() {
// example of a
string with local extent
char inBuf[ maxLen ];
cin >>
inBuf;
String *ps = new Sting( inBuf );
return ps;
}
String *ps
= readString();
- , ..
,
. delete ,
new
. ,
, ,
.
, ,
.
String:
String::String( int ln ) {
len =
ln;
str = new char[ len + 1 ];
str[0] = �;
}
.
.
:
,
, (
).
, String
,
.
. String .
, . ,
.
int len =
1024;
String myString; // error: no arguments String
inBuf( &len ); //
error: bad type:
int* String search("rosebud", 7); //error: two
arguments
- .
String :
// explicit form: reflects actual
invocation
String searchWord = String( "rosebud" );
// abbreviated
form: #1
String comonWord( "the" );
// abbreviated form: #2
String
inBuf = 1024;
// use of new required explicit form:
String *ptrBuf =
new String( 1024 );
new
. new
.
0. ,
String *ptrBuf = new String( 1024 );
if ( ptrBuf
== 0 ) cerr << "free store exhaustedn";
String .
,
String tmpStr;
, .
.
. - ,
, .
. String
:
#include "String.h"
String::String() {
//
default constructor
len = 0;
str = 0;
}
:
String st();
st String, . -
st() String.
String:
String
st;
String st = String();
7-1.
String, :
String s1( "rosebud", 7
);
String s1( "rosebud", 8 );
String s2( "", 1024 );
String s3("The Raw
and the Cooked" );
String s4;