Вы находитесь на странице: 1из 512

681.3.068+800.

92++
32.973.26-018.1
24

. .
24 C++. -. - .: -, 2004. - 512 .: .
ISBN 5-94157-358-8
C++,
, -
, -
, , .
,
,
- ,
,
C++. , "" ,
, -
.
, -
, -
.

.3.068+800,92++
32.973.26-018.1

:

.
.




.

No 02429 24.07.00. 02.12.03.


70x1 OO'/ie. . . . . 41,28.
5000 . 1264
"-", 198005, -, ., 29.
, Me 77.99.02.953..001537.03.02
13.03.2002 . .

S ""
199034, -, , 12.

ISBN 5-94157-358-S . .. 24
, "-", 2004

9

; 11
12
13

I. C++ 15

1. C++ 17
22
- 27
29
break continue 33
34
34
40
46
49
51
52
57
" " undefined behavior 59

2. 61
62
63
66
68
70
71
: 73
4 ^

" " 77
82
84
85
89
92
95
inline- 99
101

3. 105
- 105
109
116
119
f 120
123
125
127
129
132
134
, 135
140

4. , , 143
- 143
144
147
148
150
151
153
* 155
157
158
string.li 159
161
164
166
168
169
169
173
175
177
main 179
5

5. 183
184
184
189
191
, 195
196
203
204
209
214
215

6. C++ 221
C++ 222
223
225
227
228
231
234
235
238
240
242
() 246
247
249
250
254
257

7. , 259
"" 259
"" '. 262
264
270
272
275
278
284
284
286
289
6

II. -
291

8. ! 293
293
295
300
306
311
312
316
? 319
322

9. 327
327
330
332
"" 335
339
340
"" 343
atttojttr 345

10. 347
347
348
349
, 350
352
353
, 354
357

. 361
361
363
365
368
370
RTT1 dynamic_cast 374
376
, 377
379
380
382
384
7

12. .387
388
'. 391
- 393
393
398
- 401
405
- 407
409
410

III. WINDOWS 415

13. Windows . 417


417
Unicode 420
425
"" 425
430
431
433
436
441

14. 443
I 443
2 444
447
4 450
5 451

. ....457

IV. 459

1. Borland 461
Borland C++ 3.1 461
IDE - 462
463
465
- 465
, 466
466
466
467
5

() 467
467
467
Borland C++ 5 470
IDE 470
470
474
474
476
476
Borland C++ Builder 6 479
IDE 479
479
482
, 482
482
483

2. Microsoft Visual C++ 6 485


IDE 485
485
489
490
490

3. C++ 493

4. ..- 495

499

[49], "
2 768 942 C++".
2 768 943- .
C++?
C++ -
, ,
C++ .
"" , ,
. . ,
,
. ,
"C++ ", -
.
C++ :
C++, Bor-
land C++ Builder Visual C++, -
. , , -
C++.
,
.
-,
, -
. -
C++,
.
,
,
.
10

-, , -
, ,
. -
" ", ""
. /
. ,
, / C++ -
.
, . , -
,
, . , -
! ,
, -
.
-
. , , ,
Windows.
,
. -
, ,
, Windows.
- .
, , . -
, -
C++ . , ,
, . , . 4 -
maiioc "" , . 6
/ stdio.h. He -
.
, ,
, (,
). , , -
, . , -
.
, -
. ""
, . ,
-
, .
, .
.
11



C++ Borland Visual C++.
, , -
, C++. -
( ), (
) . -
, , .


4- . I C++.
. 1 , -
. , , -
, .
. 2 .
, , .
, -
. ,
,
. 3 ,
, . "-
" , .
C++ -
. . 4.
. 5 -
. .
. 6 -
/ C++.
. / . :
, , "-
" .
II -
C++.
. . 8 -
, ,
.
. 9 . -
,
12

. -
.
. 10 ,
.
. 11 -
,
; RTTI.
.
. 12 STL, -
.
III Windows. . 13
Win32 API -
.
. 14 -
C++ Builder.
( IV) -
, -
, ,
.


1, 2 .

Microsoft Visual C++ 6, STL DinkumWare
STLport 4.5.3. -
Visual C++ 7. ,
, -
. -
-
Borland: Borland C++ 3.1, Borland C++ 5,
Borland C++ Builder 6. Borland,
. , ""
, Visual C++ 6
MSDN.
Borland C++ 3.1.
.
Borland C++ 3.1,
"
", .
.
13

Borland C++ 3.1 Borland (Turbo) Pascal 7


, " ", . .
, "", C++ (
1- 2- ), -
.
3.1 , "-
" , . .
.
Borland C++ 3.1 -
. -
STL . -
. , - -
,
: " ANSI ...
3.1 4.5, MS 4.I. .
5 ,
". www.rsdn.ru
.
-
, IBM,
: Borland (Turbo) Pascal 7
Borland C++ 3.1.
( )
.
,
, .
, , Windows, . .
.

-
"-", -
. ,
. -
RSDN, ,
C++.
I


C++
1. C++
2.
3.
4.
5.
6. ++
7.
1

C++
"!"
.

" ", -
,
"Hello, World!".
" ": -
.
Borland C++ 3.1 " ",
1.1.

! 1.1. " "


; .. I ; ": " .-.-. '..,.
// " "
ttinclude <iostream.h>
int main ( )
{ cout "2 * 2 =" 2 * 2 endl;
return 0;
1

( <Ctrl>+<F9>).
, , <Alt>+<F5>
, :

. -- -
. // -
. -
, .
, , -
, , .
18 I.

C++ ,
. /* -
*/.
, ,
:

/*

*/

.
, -
, -
.
. "" ,
*/ ,
, .

(~ j
. Borland
, .

/.
C++, , /,
.
.
iostream.h , -
include. C++ -
. C++
, -
/.
# (). ,
. , -
.
, define,
.
( ),
C++, main.
, main
int main ()

.
1. C++ /9

main :
.
int, -
C++ -.
C++ ,
;
main ; .
, main
C++;
, main .
.
, -
. C++ -
.
, -
.
, ,- ( ).

cout "2 * 2 =" 2 * 2 endl;


return 0;

return C++;
, ,
. , ,
,
. .
, -
. ,
1 -1.
,
( ) .
:
cout - ,
, .
C++, , -
, cout -
- ;
:
"2 * 2 =", 2 * 2 endl. -
20 I. C++

. , -
" ".
, ,
.
( , -
\ ), . -
, .
.
: * (-
), . ,
.
endl
.
,
2 -
10 , -
. ,
. -
,
. , 3
2 . -
. (
1.2) -
.

I 1.2.

#includp <iostream.h>
int main()
{ int k; //
cout " 1 9: ";
cin k; //
cout k "* 1 =" k * 1 endl;
cout k "* 2 =" k * 2 endl;
cout "* 3 =" k * 3 endl;
cout k "* 4 =" k * 4 endl;
cout k "* 5 =" k * 5 endl;
cout k "* 6 =" k * 6 endl;
cout k "* "7 =" k * 7 endl;
cout k "* 8 =" k * 8 endl;
cout k "* 9 =" k * 9 endl;
cout k "* 10 =" k * 10 endl;
return 0;
1. C++ 21


. ,
int k;

. -
. -
, -
_. C++ ,
. . -
, .
"" ""
. C++ , -
: ab, , , .
, ,
.
.
int , -
. , int .
, -
. -
, -
. C++ -
, .
, , ,
. -
, .
, C++ -
.
-
.
,
cin k;

k. cin
, .
. ,
, <Enter>.
, cout, cin C++,
- .
, C++
,
.
.
22 ^ ^ C++

.
,
1 9:

, . -
5 <Enter>, :
5 * 1 = 5
5 * 2 = 10
5 * 3 - 15
5 * 4 = 20
5 * 5 = 25

; 6 = 30
5 * 7 = 35
. * 8 = 40
t;
. * 9 = 45
5 * LO = 50

:
k ( 5), -
,
.


(. 1.2) ,
3. ,
, 147,
147. -
. -2. -
. -
, , . -
,
1 9. "" .
"" :
,
. ,
, , . -
-
. :
1 ^ 9 ;
;
1. / C++ 23

, .
C++ :
if () ;

:
if () _1;
else 2;

if else . -
if : ,
. .
,
if .
: , ;
, __2.
, .
, -
. . -
, " "
C++ ,
if . -
. if
:
if ()
{ //
cout k . 1 * 1 <- endl;
=" k

cout k M - * 2 : endl;
2 =" k
cout k * * 3 <: endl;
3=" k
cout k i . 4 <: endl;
4=" k
cout k , 5 =" k
* 5 <: endl;
cout k "* 6 =" k * 6 : endl;
cout k "* 7 =" k * 7 <; endl;
cout k "* 8 =" k * 8 ; endl;
cout k I 9 = k * 9 <: endl;
cout k : 10 =" k * 10 endl;

else cout " !" endl;

, ,
. -
, . ,
else.
24 _ I. C++

, . C++ <
<- ( ). ,
C++ : i <^ k <=9.
. , ,
1.3.
............ : .................................................................................................................... .........................................................................................
| 1.3.

^include <iostream.h>
int main{)
{ int k;
COUT: " I 9: ";
cin k; //
if (I <= k <= 9) cout "" encl;
else cout "" endl;
return 0;

. ,
k
"". , 1 <= <== 9 -
. 1 <- ,
9.



C++: . -
1 <= k ! , -
1; , 0. -
, : 0 < 9, 1 < 9.

-
. -
,
. C++ :
(1 <= k) && (k <= 9)

C++:
( < k) & (k < 10)

,
. . -
, 1.4 -
.
1. C++ 25

1.4.

^include <iostream.h>
int main()
{ int k; //
cout " 1 9: ";
cin k; //
if ( < k) && (k < 1 0 ) ) //
{ cout k "* 1 =" k * 1 endl; //
cout k "* 2 =" k * 2 endl;
cout k "* 3 =" k * 3 endl;
cout k "* 4 =" k * 4 endl;
cout k "* 5 =" k -' 5 endl;
cout k "* 6 =" k - 6 endl;
' cout k "* 7 =" k * 7 endl;
cout k "" 8 =" k * endl;
cout k "* 9 =" k * 9 endl;
cout k "* 10 =" k * 10 endl;
}
else cout " !" endl;
return 0;

( 1.5),
.
, , .
, . ,
,
. ( < ) , ""
: < < .
, , :
1 1
( < ) ( < ) "" " .

,
,
:
( > ) { t = ; = ; = t; }
( > ) { t = b; b = ; = ; }
{ > ) 1 t = ; = b; b = t; }

. , , - 10, =- 8, = . -
== 8, = , = ; = 8, = ,
- . ,
, < . ""
C++.
26 I. C++

1.5. " "

^include <iostream.h>
int main ( }
{ double a, b, ; //
cout " : ";
cin b ;
double x, ; //
cout " : ";
cin ;
if ( > ) { double t = ; = ; = t; I
if (a > b) { double t = ; - b; b = t; }
if (b > ) { double t = b; b = ; = t; )
if (a > b) { double t = a; a = b; b - t; }
if ( (a < x) S& (b < y) ) cout " !" endl;
else cout " !" endl;
return 0;

, ,
. -
. . -
:

cin ;

, :
cin ;
cin ;

, ,
. <Enter> . -
.
, t
"" . , -
.
-
. C++ , ,
, .
-
. , -
, "
" (. . 2).
1. C++ _ 27

-
C++ - .
, .
, -
. +, -, v , /.
- C++, -
. 1.6.

j 1.6. switch
!....>. ................................................................................................................................ ............................ -.. ................ .., .................................................... .............. ;

^include <iostream.h >


int main ( )
{ cout " : ";
double a, b;
cin a b; //
cout " [+ * /] : ";
char op;
cin op; //
switch (op) // -
{ case '+': cout a + b endl; break;
case '-': cout a b endl; break;
case '*': cout a * b endl; break;
case '/': if {b != 0) cout a / b endl;
else cout " !" endl;
break;
default: cout " !" endl;
}
return 0;

, ,
, .
. --
. - :
switch (_)
{ }


char, .
:
: ;
28 /.

, .
C++,
. ,
default. .
C++. - -
:
;
-
;
-
;
,
default.
switch . -
-
. .
- -
break. C++. -

switch. , -
- :

;
, -
, ;
G break,
-.
,
, default, -
. -
break, . .
-.
-
. -
:
if (op == '+') cout + b endl;
else if (op == '-') cout a b endl;
else if (op == '*') cout a * b endl;
else if (op == '/')
1. C++ _ 29

{ if (b != 0) cout a/b endl;


else cout " !" endl;

break .
, -
, .
[3, 32, 35],
switch -
.


" ", -
1.4. ,
. : -
, -
; ,
. .
.
: -
. -
, .
,
, . ,
, 573. ! 573
, :
, 86 400 -
. 2 ! -
? -
.
, , -
. (
)
.
. -
1 . -
i. 1 .
. C++ (
) .
:
30 I. ^

i = 0;
( ) i;

, ,
, ... ?
. -
i, 9. i 9,
. : i < .
C++ , -
. C++ ,
, :
while () ;
while C++.
: ,
; , -
. -
, if. -
-
. . C++
:
1=0; //
while (i < 10) //
{ ++1; // 1
cout k "*" i "=" k * i endl;

.
-
: , ,
.
, 1.7.

: 1.7. while
i- -- - .'.'. ;
#include <iostream.h>
int main()
{ int k; //
cout " 1 9: ";
cin k; //
if ({0 < k) && (k < 10)) //
{ int i = 0 ; //
1. C++ _ 31

while (i < 10) //


{ ++i; // 1
cout k "*" i "=" k * i endl;

else cout " !" endl;


return 0;

,
, .
, .

if. i,
. C++
.
,
( < ). , .
i 1 i -
++1. . -
k, ( ) *, i (
), = () k * i. -
. -
, . (i < ) -
, . -
, i
. ( < ) -
( ) .
if. , -
.
, -
. -
:
do ;
while () ;

,
. -
,
,
,
. . -
do. . .while, 1.8.
32 I. C++

1.8. do.. .while

tinclude <iostream.h>
int main()
{ int k; //
cout " 1 9: ";
cin k;
if ((0 < k) && (k < 10]] //
{ int 1 = 0 ; //
do ( ++i; // 1
cout k "*" i "=" k * i endl;
1
while (i < 10); //
)
else cout " !" endl;
return 0;

, , -
.. ,
C++
.
, ,
(. 1.7, 1.8)
.
1. -,
.
2. , -
1.
3. .
, -
.
, : .
:
for(_; ; __) ;

_ -
, , -
. _ ,
++i, ,
.
1. C++ 33

:
_.
for;
. ,
. ,
;
_;
.
, ""
, ,
. for 1.9.

[ 1.9. for

ttinclude <iostream.h>
int main()
{ int k;
cout " 1 9: ";
cin k;
if ((0 < k) && (k < 10))
for (int i = 0; i < 10; ++i)
cout k "*" i "=" k * i endl;
else cout " !" endl;
return 0;

.
. :
for.

break continue
,
. :
, . -
. , -
. break. ++
, .
continue. ,
:

2 . 1264
34 I. *C++

int k = 0;
for(;;} //
{ cout " 1 9: ";
cin k;
if ((1 > k) || (k > 9)} //
continue; //
else break; //

continue, -
,
. , -
. if -
( ), break, -
.
-:
while(1) { ... )


. .
,
.
.
1. .
2. .
3. .

: . .
, , -
.
, . . C++.
.


.
" -
". :
. ,
1. ^^ C++ 35

, , : 2.0,
-123.4567, ., . .
, , : 1.23445, .7-23.
, "" (
"") "10 ".
, 1.2341045, -
0.62310~23. , -
.
C++ "" : float, double, long double (
). long
float, double, -
.
.
.
, . -
(Help) Borland C++ 3.1, , -
float 3.410~38 3.410+38. ,
double 1.7~308 1.7+308.
, long double 3.410~4932
1.110+4932. -
Intel.


Borland C++ 3.1 -
float.h include. (,
Visual C++ 6) limits.

, .
:
1.451014. -
, "" -
.
"" .
. float 78
, double 1516 , a long double 1920.
double , -
long double.
.
, . Intel
. . -
, ,
1.10.
36 _ I. ^ C++

I 1.10.

^include <iostreara.h>
int main ()
{ ccut sizeof (float) endl;
cout sizeof(long float) endl;
cout sizeof (double) endl;
cout sizeof (long double) endl;
return 0;

sizeof C++
"" (-). C++ sizeof .
siseofjTHn) (
, ).
4, 8, 8 . ,
float 4 , long float double -
8 long double 10 .


Visual C++ 6 long double double (8) .

-
, double.
, .
3.141592653 double, F ( 0 -
3.141592653F float, L ( I) -
3.141592653L long double. -
. .
, , 1.11.

1.11.

^include <iostream.h>
int main ( )
{ cout sizeof 3.141592653F endl;
cout sizeof 3.141592653 endl;
cout sizeof 3.141592653L endl;
return 0;

s i z e o f : sizeof -
, .
1. C++ 37

4, 9, ,
.
C++ -
: , , , . -
, :
+ //
//
* //
/ //
i
. -
++ ,
, . -
1, i.
++ //
//
++ //
//

.
:
//
//
//
//
//
//


. -
,
. , -
, . , -
,
.
,
"" "".

cout ( < ) endl;
1, 0 , -
.
_35 I. C++

: ,
1.
"", - "". 1
while.
. _
J
C++ bool true false.
-
i .

C++ :
;

:
!, 2, ;
.
, :
float x, , ;
double , , ;
long double v, w;

:
float t = 0.456, = 1-23;

:
long double x = 1, z = 0;


:
const double d = 0.123456;
float const exponenta = 2.718281828;

const C++.
, .
-
.
C++ .
:
float t (0.456), p<le-23);
long double x(l), z(0);
const double d (0.123456);


, .
1, C++ 39

.
(). , , ,
,
. Borland C++ 3.1 -
M_PI.


, Visual C++ 6 -
! .

,
/ # include.
-
1.12.

| 1.12.

^include <math.h> //
^include <iostream.h>
int main ()
{ double = 0.0;
label: cout " : ";
cin ; //
if (r < 0}
{ cout " !" endl;
goto label;
}
double L = 2*M_PI*r; // = 2**
cout " = " L endl;
double S = M__PI*r*r; // = 7i*r^
cout " = " S endl;
double V = 4/3*M_PI*r*r*r; // = 4/3*7i*rJ
cout " = " V endl;
return 0;

,
. -
goto. -
. ,
,
. , , ,1
1

40 _ !. C++

,
. , goto
, .


C++:
short;
"" int;
long.

, -
.
Borland C++ 3.1 (
1.13), , 2, 2 4 -
.

1,13.
LI ......,."... ................... ..... .- ............ .."...,.....

^include <iostream.h>
int main ( )
{ cout sizeof (short) endl;
cout sizeof (int) endl;
cout sizeof (long) endl;
return 0;

Visual C-t-+ 6
Borland C++ Builder 6, 2, 4, 4. , -
, , :
Sizeof (short) < sizeof {int) < sizeof (long)

. (37] ,
-
. , short -
, , 2 , long 4 .
, (signed)
(unsigned). , . -
, -
. "-
", 1.14.
1. KaKjianncarb C++ 41

\ 1,14. ""

^include <iostream.h>
int main()
( cout sizeof(short) endl;
cout sizeof(int) endl;
cout sizeof(long) endl;
cout sizeof(signed short) endl;
cout sizeof(signed int) endl;
cout sizeof(signed long) endl;
cout sizeof(unsigned short) endl;
cout sizeof(unsigned int} endl;
cout sizeof(unsigned long) endl;
return 0;
,

, "" ""
, -
.

)
Borland C++ 3.1
limit.h, include.

: -
, .
, . ,
.
i. -
. , -
9
, , , D, , F , , . d, e, f.


, , -
, . ,

, .

, "-
" int, int,
, . -
, , U ( ) L
( 1): 123 , 123U
int, 123L , 123LU
I.

( I23ui) .
.
, -
,
:
int Jc;
short , , z; . // int
long a = , = 1; // int
unsigned int w = 2, v(10); //
signed long , rr(-5); // signed
unsigned long int t = 10; //
unsigned f; // int

int,
. ""
, -
: short, long, signed unsigned, -
short, long, int , int.
signed unsigned,
signed.
,
, :
const unsigned int BakersDozen = 13; //
unsigned int const AnotherBakersDozen (13) ; //

enum, :
enum { one = 1, two = 2, ten = 1 0 };

:
const int one = 1, two = 2, ten = 10;


, , ,
:
enum { one = 1, two, three, four, five, six, seven, eight, nine, ten };

1 , .
, -
, . . ,
:
enum { zero, one, two, three, four, five, six, seven, eight, nine, ten };
"L C++ 43

:
enum { bit = -1, zero };
1
. , zero
.
enum .
() , :
enum Boolean { false, true };
, -
:
Boolean a;
Boolean b(true);
Boolean d = false;

, -
, . -
Boolean , ,
. ,
"" :
= + d;
int f = b + false;

. , ,
.
""
"", typedef.
typedef int Boolean;
const Boolean false = 0;
const Boolean true = 1;
.
true false
.

(
C++ bool. true false
, Borland C++Builder 6, Visual C++6
. Borland C++ 3.1 .

typedef, ,
:
typedef ____ ___;
44 I. C++

Turbo Pascal,
word real, :
typedef unsigned int word;
typedef double real;

Windows ""
DWORD, BOOL BYTE.
__ -
.
-
, . -
. ,
, : 32/4, /4, 34/4, 35/4.
C++ , -
: ( ),
% (). -
, 1.15.

[ 1.15.

tfinclude <iostream,h>
int main()
( cout 5 % 2 endl;
cout - 5 * 2 endl;
cout 5 % -2 endl;
cout -5 % -2 endl
return 0;

: 1, -1, i, -i.
, ,
. -
. k
, , , -
:
if (k % 2 = 0) cout " !" endl;
, .
, , . -
, , 4
( ), , 100.
100, ,
1. C++ 45

400. , 1600,
2000, 2004, 1996, 1900 2003 . -
, , -,
. ,
( 1.16).

1.16.

frinclude <iostream.h>
int main()
{ unsigned int year = 0; //
begin: cout " : ";
cin year;
if (year == 0) goto end; //
if ((year s 100) != 0}
if ((year % 4) == 0) cout " !" endl;
else cout " !" endl;
else
if ((year % 400) == 0) cout " !" endl;
else cQut " !" endl;
goto begin; //
end:
return 0;

, -
. -, ,
. goto
: begin end . -, -
. if :
if () if () ; else ;

, if else,
C++ , :
if () (if () ; else ; |

, else
if.
, -
:
(( 100) ( 4))
( 400} ,
;
46 VacTbJ.

, -
.
:
if (((year % 100) != 0) && ((year % 4) = OJ || ((year % 400) == 0))
cout " !" endl;
else cout " !" endl;

(&&) ,
(| |).
- : 2 -
. 2000,
1996, 1700, 1997, ,
, . -
.


, C++
. , ,
, , . .
.

( j
, -
Borland C++ 3.1. , -
Windows, . .

C++ .
C++. - -
( ): '', ' + ', ''.
. -
- Borland C++
. , C++ -
. -
\ (backslash). C++ -
'\", ' V " , ' \ \ ' .
.
; -
, '\' <Backspace>, ' \ '
. '\',
:
cout ' \ ' endl;
t C++ 47

endl
1 , ,
. , \ -
,
151 = '\' <Enter>.
1 , , sizeof (int) .
f \ t '
,
, 1.17.

I 1.17.
^include <iostream.h>
int main()
{ cout " " endl
"1 \ t2 \ t3 \ t4 \ t5 \ t6 \ t7 \ t8 \ t9" endl

--------------------------------- endl;
for (int 1 = 1 ; i < 10; i++)
{ cout i "| ";
for (int'j = 1; j < 10; j++) cout j * i '\f;
cout endl;

return 0;

:
.

1 2 3 4 6

11 1 2 3 4 5 6 7 8 9
21 2 4 6 8 10 12 14 16 18
31 3 6 9 12 15 18 21 24 27
41 4 8 12 16 20 24 28 32 36
, 10 15 20 25 30 35 40 45
6 6 12 18 24 30 36 42 48 54
71 1 14 21 28 35 42 49 56 63
8| 9 16 24 32 40 48 56 64 12
9 9 18 27 36 45 54 63 72 81

char. char, -
, 1 .
char (signed), (unsigned).
48 _ 4acTbJ. ^ C++

"-",
, (
).
, ,
. " " .
, ,
,
. , -
.
, , -
1.18.

I 1.18.

# include <iostream.h>
int main ()
{ unsigned char nn = 48; //
cout nn ' ' int(nn) encil;
return 0;

,
.
, ' ' . '
"-".
,
int (nn). , -
( ),
. :
cout nn ' ' (int)nn endl;
, , .
. -
, :
cout int('\t') endl; //
cout int('\n') endl; //
cout int('\a') endl; //
cout int('\b'J endl; // backspace

: 9, , 8, 7.
, -
, -
. , 1.19.
1. C++ 49

\ 1.19.

#include <iostream.h>
int main()
{ for (char i = 48; i < 59; ++i) cout i ' ';
return 0;

0 1 2 3 4 5 6 7 8 9 . ,
, -
48 57.
, -
. .
, , ch . -

ch - ' '

.
,
typedef :
typedef signed char small;
typedef unsigned char byte;

small
byte, , , char.
, -
, .
Borland C++ 3.1, ASCII.
, , ,
, .
IBM PC
.
,
.
, -
.


-
. .
.
. :
=
50 I.

= .
,
. -
. , -
, , -
. ,
.
: ++ , ++; -
.
, -
.

:
_1 = __2 = __3 = . . . = __ =

: -
, _,
. . _1. -
, -
, :
= 5 + ( = 6) ;

, -
11 (5 + ). :
a = (b > 5);

i
. , .
-
( ). 5. -
:
d = ++;

d = ++;

1 -
d.
-: 5 d,
1. , -
:
1. C++ 51

, "" : -

d = (--) ;

5 d, -
1. , -
, () -
-.
,
(, ) . -
:
= + ;
, = ()
. , :
i = i + 2;

: i (, )
2 (5) -
.
.
#=-, -
: +, -, *, /, %. -
:
#=

:
= # ()

,
#, . ,
2 C++ -
:

, . ""
, .


, , .
-
.
:
52 I. C++

[[char, short] -> int] -> unsigned int -> long ->
unsigned long -> float -> double -> long double

, "" "".
char short
int, -
.
, :
float -> int


( ), -
.
. -
:
(}
()

.
. , " ...
" [37, . 172].
C++
:
static_cast<TMn> ()
reinterpret_cast<Trai>()


. ,
static_cast. -
.
, ,

. . -
[37, . 172],
reinterpret_cast " ". -
.
dynamic^cast,
If.


?,
, :
_ ? _1: 2
. ^^^ C++ 53

: _, -
, _1 -
; , -
_2.
. , :
max = ( > ] ? : ;

&& ( ) | |
( ).
, ! ( ). -
, , .
.
0 (false ) 1 (true ).
. -
:
! = 1 !false = true
!1 = 0 'true = false

:
|! 0 = 0 false | | false = f a l s e
0 [| 1-1 false I | true = true
1 || 0 = 1 true | | false = true
1 || 1=1 true | I true = true

:
&& 0 = 0 false && false = false
0 && 1 = 0 false && true = false
1 && 0 = 0 true &5 false = false
1 && 1 = 1 true & true = true

:
, -
. ,
(1 I ] )
i, -
.
( & & )
, .
:
_1 && _2 !_1? (__2?1: 0) : 0 ) ;
D _1 | | _2 (_1?1: (_2?1:0} ) ;
[ (?0:1).
54 I. C++

( C++ -
): ( -
) , -
.
, . . . -
, , -
. , C++
:
if () ;
:
if ( != 0) ;

if ( == 0) ;

if (!) ;
, -
=*= () = (). , -
:
if ( = 0) cout " 0" endl;
else cout " 0" endl;

-
, .
: , ,
. ,
else.
, , -
:
if (0 =- a) cout " 0" endl;
else cout " 0" endl;

,
.
. ,
C++ :
whi1 () ;
:
, .
for, :
for (_1; _2; _3) ;
1. C++ 55

_1
; ,
_. _2
,
. -
.
, -
, -
, .
.
, ,
. , -
( 1.20).

1.20. while ""

^include <iostream.h>
int main()
( unsigned int counter = 0;
double summa =0.0, number;
while (cout " : ", cin number) //
//
summa += number, ++counter; //
cout "=" summa / counter endl;
return 0;

.
, .
, ,
. , -
. C++
, ! -
() . , ,
,
.
.
, -
<Ctrl>+<Z> ( <Enter>). -
" ", -
0, .
, , , -
.
, .
56 I. C++

for (
1.21.

1.21. fore ""

#include <iostream.h>
int main()
{ unsigned int counter = 0;
for (double summa =0.0, number; //
(cout " : ", cin number); //
summa += number, -t-+counter) ; //
cout "^" summa / counter endl;
return 0;

for -
.
for.
( )
. ,
, , -
while .
.
_1 _2.
,
. -
<Ctrl>+<Z>,
number , for -
! -
_ , . ,
_3, -
_2, .
""

:
= it = , += 1, t) ;

t , .
C++ (. . 1.1), ( -
) . , (. . 2)
" , () -
1. C++ 7

. (. . 3) .
,
. . 1,1
:
, .

1.1. C++


sizeof new delete
*r >*
*/%












. -
" ",
. , -
, . ,
float, , , ,
1037. , :
cout * 1000 endl;
I. C++

Borland C++ 3.1,


. 1+37.
, -
:
Floating point error: Overflow ( : )
Abnormal program termination ( )

, float.
. , -
. -
-
.
.
Borland C++ 3.1, , int -
32 768 +32 767. , , -
:
cout +32767 + 1 endl;
cout -32768 - 1 endl;
-32 168, -
32 767. , ,
1 () -
. 1
() , , -
.
long short.
unsigned int,
0 65 535.
cout 65535U + 1 endl;

. ,
cout Ou - 1 endl;

65 535. , -
, -
.
char ,
"-". " "
-128 127. Borland,
Visual C++ 6 "", , , -
:
char ch = 255;

ch -1.
, Visual C++ 6 :
1. C++ 59
1
warning 4305: 'initializing : truncation from 'const int' to 'char'
1
'': 'const int' 'char

. -
, d
double d = 15/2;

7, 7,5. , 15/2
. -
( ) double.
,
:
double d - 15.0/2;

double d = 15/2.0;

"" , :
double d;
d = 3,141592653;

d , "", -
. ,
double d = 3,141592653;

" " undefined behavior


C++ [50], -
. , -
. -
. 1.1 . -
:
= b + + d;
, d . -
, . . ,
d. ,
d . , d -
. , , .
. , :
int i = 1, k = 2, j = 3;
i = (i = j - k) + (i = j + k ) ;
cout i endl;
60 _ I. C++

"" , Borland (Bor-


land C++ 3.1, Borland C++ 5, Borland C++ Builder 6)
.
i = (i = j + k) + (i = j - k) ;

. :
j - k i, a j + k
5. . -
, -
. Visual C++ 6 :
i = (i = j - k) + (i = j + k) ;

. ,
2.
undefined behaviour .

. ,
:
fa += b) += a;

: ,
, ? -
,
. ,
i = ++i + 1;

, -
. - , , ,

,
, , -
i 2. ,
1=7, i++, i++;

defined behaviour .
i 9. , ""
" " . -
,
, (| |) (&&).
, -
, .
.
2

, , " ". -
,
"". -
, -
. ,
. , ,
. -
-
. , ,
. -
.
Fortran -
: . -
, Pascal, -
.
C++ , .

. -
, ,
, -
.
C++ ,
, , -

. -
, .
:
, ,
62 /. . C++

,
, .
, , ++
, -
, , ,
, .


C++ -
.
C++,
. , ,
(Standart Template Library, STL).
STL ,
.
, ,
. C++ "-
" :
, "" ;
C++, STL;
G , , -
, conio.h;
- (MFC,
ATL, WTL, OWL, VCL),
Windows;
API , ;
, , boost.
, , -
. , , -
.
, , , , -
1 +1. -
, .
, - ,
"". C++ -
, :
( );

, C++.
, , ,
2. 63

, , .
. ,
void.
. C++ -
, -
, .
:
{ )

, ,
. , , -
, C++ -
. , , -
, -
:
double = sin(x) + 2 * cos(x) + 1.53;
,
void. , , -
,
, :
randomize () ;

- , , -
,
.
C++ .
,
, -
, include .


C++ -
, Borland
math.h. ,
()
(. 1.7). ,
ha, hb, he. , -
, ,

- )( - )
64 I. C++

- (++) /2 .
.
: math.h -
:
double sqrt(double x) ;
long double sqrtldong double x) ;

, , , -
.
-
. ,
.
, -
. , -
. (
) ,
\ . -
. , -
2.1.

j )2.1.

^include <iostream.h>
#include <math,h>
int mainf)
{ double a, b, ; //
double ha, hb, he; //
double p = 0 , S = 0 ; //
cout " : ";
cin a ;
= (++)/2; //
3 = sqrt( * (-) * (-) * (-)); //
//
ha - 2/a*S;
cout " =" ha endl;
hb = 2/b*3;
ccut "Hb =" hb endl;
he = 2/c*S;
cout "He =" he endl;
return 0;
,

, , .
-
2. 65

: , , -
. , 4, 5
, 4, ,
, .
1, 1, 1 : .866025. -
, , 2 -
1.885618, 2.828427.
math.h :
, -
; ;
.
.
C++
stdlib.h. ,
, .
, -
, [44], .
"" 1 1000,
.
. ,
. Bor-
land C++ 3.1
randomize, :
void randomize (void) ;
, ,
.
random, :
int random (int num) ;

(~ j
, C++, -
Borland Pascal, Borland
"" . Visual C++ 6 .

[0, num).
, num : -
num1. ,
1 1000, :
int number = random{1000] + 1;

, ,
random :
int number = random( ) + ;

3 1264
66 I. C++

, 2.2.

! 2.2.

^include <iostream-h>
#include <stdlib.h>
int m a i n ( )
{ cout " 1 1000." endl;
cout " !" endl;
cout " 10 " endl;
const int n = 10; //
randomize () ; //
unsigned int number = random(lOOO) + 1; //
unsigned int getnum; //
int i - 0;
while (i < n)
( cout " ?" endl;
cin getnum;
++i; //
if (number < getnum) cout " ! \";
if (number > getnum) cout " ! \";
if (number === getnum)
{ cout " ! ! ! \ n " ;
cout " " i " . \n";
return 0; //

cout " ! , " endl;


return -1; //

, .
500, :
. , 500. -
250 0500. ,
500, 750, -
5001000. -
"" .


" " , C++
. -
, , -
67

. , -
. , , -
. .
. ,
.
int max3(int a, int b, int c)
{ int t = (a > b) ? a: b;
return (t > c) ? t: c;
1

. -
t, -
. ,
-
.
(
2.3), -
, stdlib.h.
""
| 2.3.

int max3{int a, int b, int c)


{ return max (max (a, b) , c) ; }

,
.
, :
int = (, 5, );


# include <stdlib.h>

. . max . -
-
. :
double LengthCircle (double r) ( return 2*M_PI*r; }

,
:
double SquareCircle (double r} I return M_PI*r*r; }


.
68 _ I. C++

""
. ,
, -
void .
. ,
.
, , -
. -
, -1 ,
. .
2.4.

| 2.4, " "

int TableMult (int k)


( if ( (0 < k) && (k < 10) )
{ for ( int i - 1; i < 10; ++i)
cout k "*" i "=" k * i endl;
return 0; //
}
else return -1; //

:
int main ( }
{ int k, cc;
cout " 1 9: ";
cin k;
if (cc = TableMult fk) )
cout " ! " endl;
return cc;
}

, main . -
: ,
.
. ,
-1, .


. C++

. C++ -
2. 69_

( ) , ().
,
.
, , :
.
:
: extern static. -
extern;
, void. int;
;
;
;
throw ( ).
.
extern , -
. static
. ,
, -
. -
. II.
.
, . .
, . -
, -
. { }:
void f(void) { }

"".

v
J
. -
,
. ,
.

:
float Maxffloat x, float ) //
( return ( < ? : ); }
int main(void)
{ float Cube(float a); //
int cc= 15, k = 2;
JO __ __ I.

//

float Cube (float x) //


{ return (x * x * x) ; }

,
. , -
, ,
.


-
. -
.
, -
:

, C++:
, , -
. , -
, . -
- : -
,

.
C++ -
. ,
.
, C++ .
1 . .
2. .
3. .
, , C++
,
.
: -
? , -
.
2. 71


-
, , -
. , -

(. . " " " "
),
:

_ = . ,
( ), .
,
, ;

,
, -
;
,
.
: sin, cos, ., -
. -
.
cube,
. .
float Cube(float x) //
{ return (x * * ); }
float = 5.5, - 1.23, = Cube{2); //
//
float d = Cube (b); //
float e = Cube(a/b + 2,51); //
float f = Cube(Cube()); //

, . -
f (f ( ) ) . 8, -
: 8 512,
134217728.
, -
,
.
72 /^ C++

, -
1.
cout Cube(b++) endl;
cout b endl;


i
2

, ,
, +-
-
:
= ++;

,
. C++

, .
C++
"" ( ) -
( 2.5)'.
V V * - . :

\ 2.5.
double sqr(double ) //
{ return * ; }
int main(void)
{ double k = 0;
double g - 1, f=2;
g = sqr({f = sqrif), k += f)); // -
//
cout "\n\n f =" f;
cout "\n g =" g;
cout "\n k =" k;
return 0;

g - s q r ( ( f = s q r ( f ) , k += f ) ) ,
. -
C++: ""
. , ,

' , ^include.
! j. 73

, . -
( ) -
. "" , -
: .
. sqr -
f 2, , .
f - s q r ( f ) f 4,
-
k. k += f, ""
sqr, 4. g
16 = 4 x 4 . , :
f = 4
g = 16
k = 4


C++ . -

:
=
, -
. C++ -
:
()
.

( ) .
-- ,
. -
, .
, . -
. -

(. 1.17). , : -
, , -
: ,
60. 2.6.

I 2.6,
void repchfchar ch = '-', int k = 60)
{ for (int i - 0; i < k; ++i) cout ch; }
74 I. _++_

C++ :
repchO; // 60
repch('+'); // 60
repch{'=', 52); // 52

,
. C++ -
.

repch!,54};

. -
:
void repchfchar ch = '-', int k)

, :
repch(54);

, 54- , -
60 .
: 54
' 6 ' . -
, , .
. -
:
void repchfchar ch, int k = 60}

repch(54);

60 .
.
, , ,
2.7.

2.7.

int f(int = 3); //


int main(void)
{ cout f() endl;
return 0;
}
int f(int x) { return x; } //
g. 75

, -
, .
, -
, . ,
, , -
( 2.8).

! 2.8.

double g = I, f;
double sqr (double x) //
{ return x * x; |
double ff (double x = sqr[f}) //
/ /
{ return x; }
int main (void)
{ f = 2;
cout "\n \n f =" f endl;
cout "\n ff(x = sqr(f) =" ff(); //
//
s
cout "\n ff(3) = " ff (3) endl; //
return 0;

,
:
f = 2
ff (x *= s q r ( f ) } = 4
ff(3) = 3

ff f,
4. -
, ff . -
,
, .
(. -
2.5) ( 2.9).

; 2.9.
;,...:. ...... - ..................... , ..................... : ......... ;. .......... :......:. ...- ..... :,...-. ......................... . .............. : .................. .. ....... . ................... .-.-.-.-. .
double g = 1, x;
double" sqr (double x) //
{ return x * x; }
76 I. C++

double ff (double x = (++g, sqr(g))} //


{ return x; }
int main (void)
{ x = 6;
cout "\n ff(x = (++g, sqr(g)))=" fff); //
//
cout "\n ff(x) =" ff(3) endl; //
//
return 0;

,
( = 2) sqr.
, , -
.
(-
2.10).

2.10.

double = 0; //
double ff (double x) ; //
double ff(double x = ff ()) //
{return ;} //
int main(void)
f = ;
cout "\n ff(x - ff(y)) =" ff();
cout "\n ff(3) =" ff(3) endl;
return 0;
}

f f,
. ,
. -
f f ,
, . -
:
ff( - ff()) = 6
ff(3) - 3


. ,
, . -
_2. 77

, -
! .
, -
.


" "
-
. .
. -
. ,
. " "
" ". ,
,
" ".
C++ ,
, ,
, .
,
. . .
" "--
.
. C++
. . " "
. -
, . . -
.
, , , -
, -
. ,
. -
:

, . .
. ,
. -
;
. "-
" "" . ,
" "
78 _ I. C++

. . -
1: -
, -
.
:
1. ,
?
2. ,
( )?
3. ,
" " ?
: -
,
.
: ""
. :
int x = 5; //
void f(void)
( int x = 3; //
cout "x = " " endl;

cout "x =" x endl;


f0 ;
cout "x =" x endl;

:
= 5
= 3

, .
,
f , -
. -
.

1
, Last In First Out
(UFO) " ".
_ . 79

- ,
, ?

::. , .
int x = 5; //
void f(void)
{ int x = 3; //
cout "x = x " " endl;
cout "x = :: x " " endl;
}
int main(void)
. . .
cout "x =" x endi;
f();
cout "x =" x endl;
. . .
1

:
= 5
= 3
= 5
= 5

,
: , , -
. , ,
, -
, , . , -
? C++ -
, .
. -
:
( ) . -
. (
2.11).

! 2.11. :

int f l i n t x = 3);
// int f(int x = 7); // ,
int main(void)
{ cout f() endl; // 3
80 _ 1^^

int f ( i n t x = 9} ; //
cout f f j endl; // 9
return 0;

int f(int x) //
( return x; }

: -
, , -
, .
: -
, " " -
. -
. :
static

, , -
. -
,
. -
.
.
-

, , . -
:
unsigned int f ()
( static unsigned int count = 0;
// . . .
-t-4-count ;
return count;

:
insigned int n = f () ;

.

, .
(. 1.11). 2.12

.
!_ 81

! 2.12.

double Average(double data)


) static double sumna = 0; //
static int count = 0 ; //
count++;
surrma += data;
return summa / count; //
1

;
int main()
i double data = 1, middle;
while (data) //
{ cout " : ";
cin data;
middle = Average(data]; //
cout " : " middle endl;
)
return 0;
J
summa count -
. summa
data, count 1. -
( ).
( ) -
, , -
. , -
.
C++ , , . -
if, :
if (int = 1) cout endl;

:
if (int { 1 ) ) cout endl;

if, ""
true, "" false.

" ". ,
, .
.
I. C++


Borland C++ Builder 6, Visual C++ 6,
.


while. ,
. for -
_1. -
for,
"" , .
,
.


Borland C++ Builder 6, Visual C++ 6, -
.


, ,
,
. . -
, ,
.
. , , -
: , .
void Swapiint a, int b)
{ int t = a; a = b; b = t; }

, ,
. -
, .
int = 5, = 3;
Swap(x, ) ;
cout " =" ";
= " endl;

cout = 5 = . ,
Swap . -
:
void Swap(int a, int b)
{ int t = a; a = b; b = t;
2. 83

cout "a-= " ";


= " b endl;

, :
=* 3; b = 5
=. 5; = 3

, . -
. -
: -
.
, , -
. (-
) , -
.
. swap (
2.13).
; ...."-v ; ;, ; yi-r.pl - ', ;; :

\ 2.13.

void Swap(int &a, int &b} //


{ int t = ; = b; b = t; }

-
:
Swap(x, ) ;
, , -
.
C++ -
.

:. J
-
. Borland
Borland, .
,
. . .
,
.

""
(-
2.14).
84 _ I. C++

! 2.14. -
!.;:, ......................... :ILJ..D. .............................................. . .............. '.:..>,., .......................... ~~ ................................ *....;..: ............................................................ :-....-
double (double &) // -
{ return {--} ; }
int main (void)
( double g - 1, f = 2;
g = s({f = s(f), f ) ) ; //
cout "\n \n f = " f;
cout "\n g = " g;
return 0;

! -
- s .
, , :
f= 3
g = 2

, s , f -
^ . -
++, .
.


, .
f.
"int f(int x); //
int f(int& x) ; //
int f (const int x) ; //
int f (const int& x) ; //


, .
-
. ,
. ,
,
. ,
.
, -
.
-
2. 85

.
, .
, -
:
.
.
, : -
, (
Visual C++ 6
),

, ,
( const). ,
,
:
;
.
-, ,
- - -
.


-
, ,
, . , -
.
.
. , -
-
. -
. , ,
,
.
. , -
.
, .
1 ( -
2.15).

1
Pascal : . . -
Turbo Pascal 6.0, 7.0. .: , , 1993.
86 _ ___ I. C++

\ 2.15.

int a, z; //
int (int )
( z -= ; //
return ( * ) ;
}
void main (void)
{ z = 10;
a = C(l); // ""
cout z ' ' a endl; // 9 1
z = 10;
a = C(z); //
cout z ' ' a endl; // 100
z = 10;
a = ) * C(z); //
cout z ' ' a endl; //
z = 10;
a - C(z) * C(10); //
cout z ' ' a endl; // -10 10000

z,
, ,
. ,
. -
.
, -
. z
, z -
, z .
-
. -
, .
, C++ -
: . -
-
.
,
Visual C++ 6 ( -
2.16) (9 1, , 0 , -10 0 ).
, z
.
2. 87

: 2.16.

^include <iostream,h>
int a, z; //
int CC (int &)
{ z ;
return * ;

int main (void)


{ int d;
a = 1;
z = 10;
d = CC(a) ;
cout z d endl;
z = 10;
d = CC(z) ;
cout z d endl;
a = 1;
z = 10;
d = CC(a) * CC(z);
cout z d endl;
a - 1;
z = 10;
d = CC(z} * CC(a);
COUt 2 d ' endl;


. - -
-
. ,
2.17.

| 2.17.

int i; //
double f () // ,
{ double sum = 0;
for (i = 0; i < 3; i-H-J sum += i;
return sum; // 3 = 0 -f 1 + 2

int main (void)


{ double sum = 0;
88 _ I. C++

for (i = 0; i < 3; i++) //


sum += f ( ) ;
cout sum endl; // 3

! ,
, : -
C++. ,
9, ,
. ! -
i = . f o , -
, . -
: i,
. -
, f ,
, sum ,
9.
,
. , ,
,
.
-
, ,
. , -
. -
. -
:
int f ( i n t &a, int Sb)
{ return (a++ + b++) ; }

. -

int i = 1, j = 1;
j = f ( j , j);
i = f (i, i);
cout i ' ; ' j ' ; ' endl;

Borland C++ Builder 6 , , a Visual C++ 6 -


4, 4. , -
.
. ,
2. 89

,
return:
return {a++, b++, +b);


i = f(i, i);
6.


, .
,
, -
.
(overloading).
, - .
Help Borland
, .
, -
- :
int abs(int x);
long labs(long x);
double fabs(double x);
long double fabsl(long double x);

, -
, ,
, -
. , ,

.
, -
. -
,
. , -
, ,
void print(char t) { . . . }
int print(char t) ( . . . )

Visual C++ 6 ""


,
90 I. ++

" " print (see declaration of


'print'). Borland C++ .
, -
, ,
.
, -
,
. ,
""
, . . -
[37]
.
,
:
int abs(int x);
long abs(long x) { return labs(x); )
double abs(double x) { return fabs(x); }
long double abs(long double x) { return fabsl(x); }
, , -
, , -
.
"" , -
. . -
, .
:
int f(int a) { ... }
int f ( i n t &a) { ... }

, :
f(b);

? Visual C++ 6
"", 29 (" ").
const
. -
.
, -
. , .
.
void gfint a) {cout "g(int a)" endl;}
void gfconst int a) { cout "gtconst int a)" endl; }
2, 91

, Visual C++ 6 ,
- "", -
. :
error 2084: function 'void cdecl gfint)' already has a body
'void cdecl g(int)'

,
void gfint &) { cout "gfint Sa)" endl; }
void g(const int sa) { cout "g(const int &a)" endl; }

, , -
: ,
const; ,
.
"" :
void g(int Sa) { cout "g(int Sa)" endl; }
void gfconst int a) { cout "g(const int a ) " endl; f

void g(int a) { cout "g(int a ) " endl; }


void g{const int Sa) { cout "g (const int s a ) " endl; }

, .

!2);
g(v);

2668 .

:
void f ( i n t a) //
{ cout " f ( a ) " endl; }
void f ( i n t a, int b = 0} //
( cout " f ( a , b - 0 ) " endl; }


.
2668. ,
, . , -
.' Borland
.
92 I.


:
void f(double a) I/
{ cout "f{double a ) " endl; }

, -
.
(, ir.t, long short) -
- . -
,
: -
, -
.
.
-
.


-
.
. , ,
"" .
2 -
( signed char unsigned char), 6
3 . 11 -
. ,
. -
, . , -
.
-
, . , .
, C++
, "" ,
. -
:
template < >

(
2.18), .
2. 5

| 2.18.

template <class T>


void swapfTs , 14 )
{ 2 = ; = ; = z; )

, , ,
. -
-
. , ,
, .
, " " ,
. -
, :
long k = 7, d = 12;
swap(k, d);
float p = 62.44f q = 68.3;
swap(p, q) ;

:
void swapflongS , longs ) { long z = ; = ; = z; }
void swap(floats x, floats ) { float z = ; = ; = z; }

,
, . -
. -

swap<long>(k, d);
swap<float>(p, q);

, -
.
.
,
. -
, , -
class, ().

J
typename,
. -
Borland C++3.1, -
Borland C++ 5, Borland C++ Builder Q .
94 !. C++

C++ -
, .

)
Borland C++ 3-1 , . . .
, -
(. 2.6) ,
2.19.
.

i 2.19.

template <int n>


void repch(char ch = '-')
{ for (int i = 0; i < n; ++i) cout ch; }

-
. -
:
repch<10>('+');

- . ,
.
template <int n = '10>
void repchfchar ch = '-')
{ for (int i = 0; i < n; ++i) cout ch; }
C++ . , -
, ( 2.19), -
(. 2.6). -
:
repch<10>('=');
repch('=');
repch(' + ', 30) ;
repch<5>('+');

( ^)
Visual C++
. ,
. Borland C++ Builder 6 .
,
. , -
STL.
2. 95

C++ -
. . -
C++ ctype.h,
. ,
. . 2.1.

2.1. ctype.h

int isdigitfint ) 0-9


int isxdigit (int ) 0-9,
a-f, A-F
int isalphafint ) a-z, A-s
int isalnumtint )
int islowerfint ) a-z
int isupper(int ! A-z
int isspacefint )
int isgraph(int )
int tolowerfint ch} A-z, a-z
int toupperjint ch) a-z, A-z

, is, -,
- ,
"" "". -
, true
false, . "", , , -
"" i. Borland C++ 3.1 -
, . , -

cout i s a l p h a ( ' a ' ) endl;


cout i s d i g i t ( ' O ' ) endl;

2 . Visual C++ 6 -
2 4. ,
1, . tolower toupper.
-
, .
96 I. C++

,
. , ,
.
. ,
-.
Borland conio.h, -
. -
Visual C++ 6.
, :
int getch(void);

, -
. ,
. -
, <Esc>,
27. , -
unsigned char.
2.20.

[ 2.20.

tinclude <iostream.h>
^include <conio.h>
int main f )
{ const int Esc = 27; // ASCII- ESC
unsigned char ch = 0; // char
while (ch != Esc}
{ ch = getchO; // char
cout ch '='
int(ch) endl; //
}
return 0;

,
128 159. "",
240. :
"" "" 160 175 (
32), "" "" 224 239.
"" "" 48 : 176223 -
.
, , "",
241.
2. 97


Visual C++ 6 Borland C++ Builder 6
.

, , -
, ctype.h.
isalpha, isalnum, isgraph, islower, isupper, tolower, toupper. -
isupper 2.21.

1 2.21. isupper
;.'. ; '........ , , .'.;

int isrupperfint ch)


{ if ((127 < ch) & (ch < 160) || (ch = 240)) return 1;
else return isupper{ch);

l, . -
, -
.
,
if ( { 6 4 < ch) && (ch < 9 1 ) ) return 1;

.
, -
.
""
. , "" -
. islower -
2.22.

) 2.22, islower

int isrlowerfint ch)


( if ( ( 1 5 9 < ch) SS (ch <-176) I I
6 (ch < 240) ||
return 1;
else return islower(ch);

-
.
int isralphafint ch)
( return (isrlower(ch) I I isrupper(ch)); )

. 12fi4
98 _ ^ C++

, isainuni isgraph .
- . -
toiower 2.23.
; ............................. ............... ;" ...... ' ...... :".' ...... ' .............................................. ' ..................... *- ...... .' ............................ '"""' ..... " ................................................................ ]
! 23. toiower :
! ....................... ;. ............ ___ .......... , ................................. , ........................... .... ................................... ; ...... ....;..: ...... ::. ................ :. ................ ;: ..... .: ....... ......

unsigned char rtolower(int ch)


{ if ((127 < ch) && (ch < 144)) return (ch += 32);
else if ((143 < ch) &fi (ch < 176)) return (ch += 80);
else if (ch == 240) return ++ch;
else toiower (ch);

,
. , , -
,
. toupper -
2.24.
; .......................... ; ...... ; ............................. ' ....... ". .............. ; .............. : .................................. " ..... ........... " .......................................... ; .............................. -{
I 2.24. toupper

unsigned char rtoupper (irit ch)


{ if ((159 < ch) && (ch < 176)) return (ch -= 32);
else if ((223 < ch) && (ch < 240)) return (ch -= 80);
else if (ch == 241) return ch;
else toupper(ch);

, Borland C++ 3.1 -


. , -
:
ch = getch();
cout rtolower(ch) endl;
cout rtolower('A') endl;

, :
. ,
Windows, , -
. , -
-
Windows, DOS! -
.
2. 99_

inline-
. -
.
"" C++, ""
. C++ -
. -
, . -
.

( J
, , Bin
Borland C++ Builder 6 cpp32.exe.

.
#inciude (. . 1).
, ,
ttdefine. :
ftdefine

:
^define and &6
#def ine or 1 |
ttdefine not !

.
float. h limit.h, , .
, float double
Visual C++ 6 float. h :
ttdefine DBL_MAX 1 . 7976931348623158e+3Q8 /* max value */
ttdefine DBL_MIN 2 . 2250738585072Q14e-308 /* min positive value */
#define FLT_MAX 3 . 402S23466e+38F /* max value */
ttdefine FLT MIN 1 . 175494351e-38F /* rain positive value */

limit. h , -
:
tdefine INT_MIN (-214748364T - 1) /* minimum (signed) int value */
ttdefine INT_MAX 2147483647 /* maximum (signed) int value */
#define UINT_MAX Oxffffffff /* maximum unsigned int value */


Windows. : -
^include
100 I. C++


, #define, -
.

, ,
-. ,
C++ const, -
. . -
const
^define.
, #define, , , -
, . -
.
(. 2.3). , -
, , -
-
. , , -
. ,
.
, .
, ,
#define max3(a, b, ) max(max(a, b ) , c )

int x = max3(c + d, 5, C u b e ( n ) ) ;

, :
int = max (max( + d, 5 ) , C u b e ( n ) ) ;

, include -
, .
,
randomizef) random . stdlib.li, , -
, :
#define RAND_MAX Ox7FFFU //
tdefine random(num) {int) ( ( (long) rand () * (num)) / (RAND_MAX -f 1))
ftdefine randomize()srandf(unsigned) time (NULL))

To max mi
#define max(a, b) ( ( ( a ) > (b)) ? (a) :
^define min(a, b) ( ( ( a ) < ( b ) ) ? (a) :
2^ 101

"
". ,
.
. , , -
.
tfdefine max(, ) > ? :

int d = max (a += 1, b += 2) ;
:
int d - += 1 > += 2 ? += 1 : += 2;

, . . >
+=. i > , -
+=, ,
" ".
, -
, C++ -. -

inline. max :
inline int max(int a, int b) { return (a > b ? a: b); )
,
.
inline ,
-
, . ,
, -
,
.
, :
;
"". , ;
switch goto;
,
, .
"" inline,
,


.
, -
102 I C++

. -
, # (). -
:
tfdefine DIAG(v) cout #v '=' v endl

.
tt 55.4.
DIAG(tt);

:
tt = 55.4;

, , -
-
.
,
. ,
LINE , FILE , DATE .
2.25.

2.25. -

ttdefine Print(arg) \
cout #arg ';' endl; \
cout __DATE '; ' \
cout << FILE__ ';' endl \ __LINE__ endl; \
arg //

"" -
. , . .
( ).
, -
.

. :
DATE___ , __FILE -
, LIME - print ( -
2.26).

2.26. Print

#include <iostream.h>
//
//
2. _ 103

ttdefine Print (arg) \


cout #arg ';' endl; \
cout __DATE _ ';' endl; \
cout _ FILE _ ' ; ' endl \
_ LINE_ endl; \
arg
int main ( )
{ Print (int a = 3) ; // 11-
cout a endl;
return 0;

, , -
, -
;
int = 3; // arg
May 25 2003; // _ DATE__
PREPROC.CPP; // _ FILE _
11 // __LINE _
3 // main

.
(. . 11).

assert, -
. assert. h. . 1

(. 1.3). if
assert ( 2.27).
:,! ............... ......... "-;" ......... : ................................... " ........................................................... ' ........................................................... ' ..................................................... ;
I 2.27. assert

^include <assert.h>
^include <iostream.h>
int main()
{ int k; //
cout " 1 9: ";
cin k; //
assert ( (0 < k) && (k < 10)); //
cout k "* 1 =" k * 1 endl;
cout k "* 2 -" k * 2 endl;
cout k "* 3 =" k * 3 endl;
cout k "* 4 =" k * 4 endl;
104 I. C++

cout k "* 5 =" k * 5 endl;


cout k "* 6 =" k * 6 << endl;
cout k "* 7 =" k * 1 endl;
cout k "* 8 =" k * 8 endl;
cout k "* 9 =" k * 9 endl;
cout k "* 10 =" k * 10 endl;
return 0;

k ,
.
: Assertion failed. -
:
,
. ( < k} && (k < );
d , "".
cassert, file cassert.cpp;
, .
line 6.


Borland C++ 5, Borland C++ Builder 6, Visual C++ 6, Win-
dows, .

, ,
print. , , , . . -
:
.

. , -

^include <assert.h>
.

^define KDEBUG
ftinclude <assert.h>

, -
assert .
3

, ',
"", , , -
. , -
, , . -
. -,
: , , -
, , , , . .
(
). -, -
.
.

. -
.
C++ -
. -
, C++. -
.
-
-. , Pascal -
. C++ ( )
.

, , -
, ,
(2003). , -
706 I. C++

, . : -
1, 2 . . ,
. , 12 ,
: 31 , -
28 . . . :
,
7. , ,
( )
(-
). .
. ,
2003 . -

12 . ,
.
. -
switch:
switch () {
case 1: = 0; break;
case 2: = 31; break;
case 3: = 31 + 28; break/-

case 12: = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 -f 30 + 31 + 30;


break;
}
+= ;

: -
, 12.
-
. ,
, . , -
, . ,
. , -
, :
[];

,
.
12 , :
int month[12];

-
. , C++ -
3. 107

, ,
.
, , . -
:
int month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

-
:
int monthf] - { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

.
, ( -
),
. , -
:
int v[10] = (0);

, . .
, . ,

int v[] = { 0 } ;

, .
, -
, , -
sizeof . -
24 12
:
cout sizeof(month) sizeof (month) / sizeof(int) endl;

, -
. C++ (
) .
,
-- . ,
,
. , month[0] ,
month[1] . . (month[11]).
,
. -
for. ,
month:
for (int i = 0; i < 12; --i) cout month[i] endl;
108 _ I. C++

, -
:
for (int i = 0; i < 12; ++i) cin month [i];

,
:
for (int i - ; i < 12; ++i) month [i] = random(lOO);

:
int v[10] ;
v = { 1, 2, 3, 4, 5 );

, ""
. -
, for.
20 . -
:
for (int i = 0; i < 20; ++i) a [ i ] - b [ i ] ; .

, -
3.1.

| 3.1.

#include <iostream.h>
int main ()
{ unsigned int day, m;
int month [12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
cout " : ";
cin m; //
cout " : ";
cin day; //
for (int i = 0, s - 0; i < m - 1; ++i)
s += month [i]; //
+= day; //
int weekDay = ( (s + 2) % 7) ? (s + 2} % 1 : 7; //
//
cout weekDay endl;
return 0;

,
. -
. _ 109

weekDay? , 1 .
7 , 7.
:
. , -
.


, -
:
( . . );
, ;
;
.
, .
,
. , , , -
.
:
double Surrma (double array [], int n)

C++ ( ), -
, , " ", -
.
. , ,
, .sizeof, ,
month. -
:
double Summatdouble a r r a y [ ] )
( unsigned long n = sizeof (array) / sizeof (double) ;
double s = Q ;
for (int i = 0; i < n; ++i) s += array [ i] ;
return s;

main :
int main ( )
{ double v[10] { X, 2, 3, 4, 5, 6 };

1
, .
110 I. C++

cout Suirana (v) endl;


return 0;
}

!
:
, , -
, . -
, ,
.
cout, -
.
double Sumna(double arrayM)
{ cout ": " sizeof(array) endl;
unsigned long n = sizeof (array) / sizeof(double);
double s = 0;
for (int i = 0; i < n; ++i) += array[i];
return s;

int main()
{ double v[10] = { 1, 2, 3, 4, 5, 6 };
cout ": " sizeof(v) endl;
cout Summa(v) endl;
return 0;
}

, , 80 (sizeof (double) * ).
4. , -
: 4 / sizeof (double) ,
, .
:' -
. , .
, ,
. . 2
. ,
, .
3.2.

; 3.2.

double Surrana (double array[], int n)


{ double s = 0;
for (int i = 0; i < n; ++i) s += array[i];
return s;
I
"

, ,
.
, -
: "" , -
. -
, .
, , v[5]
, "" , 5- -
. C++ , -
.
int main ()
{ double v[10] = { 1, 2, 3, 4, 5, 6 };
cout Summa (v, 10) endl; //
cout Suirimafv + 2, 8) endl; // 8
cout Summa (v + 5, 5) endl; // 5
return 0;
J

-
, :
int ma in ()
{ int i = 3;
const int ten = 10;
double v[ten] = { 1, 2, 3, 4, 5, 6 } ;
cout Summa (v + i, ten i) endl; // v + i
/ / -
return 0;
1

, ,
.
C++ ,
. ,
, , -
:
cout Surma (v, 10000) endl; //
, , -
" ".
.
, -
. ( 3.3) -
112 _ \. _ ^

. , -
-1.

I: ...........
. 3.3.
.................................................... _ .......... . .......................... , ......... .............................. ; ..

int FindNumber (double Number, double array [], int n)


{ for {int i = 0; i < n; ++i) if [arrayti] == Number) return i;
return -1;

, -
. (. 3.2),
"" -
, :
double pi = 3.141592653;
int k = FindNurnber (pi, v + 3, n 3) ;

- ,
:
. , -
, 3,4.

| 3.4. -

const int n = 10;


double Summa (double (&array)[nj)
( double s = 0 ;
for (int i - 0; i < n; ++i) s += arrayti];
return s;

- ,
. , -

. ,
.
.
- .
"" ,
. .
.
typedef .
:
typedef [] ;
3. 113

. -
. -
. ,
:
typedef double DoubleArray[10];
DoubleArray a;

Suim-a :
double Surnma (DoubleArray &a) // 3.4

-
3.4. -
-
.

DoubleArray Summa(DoubleArray Sa)

"".
:
double Summa(DoubleArray a)

"" C++. -
. , -
" ". -
, .
, ,
.
, ,
, . . Surama:
double Summa(DoubleArray &a) (//, . .};
double Summa(DoubleArray a) (//. . .);

. -
" " " -
", . . .
cout Summa(v) endl; //
cout Summa(v + 2 ) endl; //
//
cout Summa(v + 5) endl; // 5
//

: ,
. ,
/. ^ C++

. . . -
, , -
, .
,
. , -
, 3.5,
.

; 3.5.

template < typename T >


void Copy (const T a [ ] , b [ ] , int n)
{ for (int i = 0; i < n; --i) b[i] = a[i]; }


Borland C++ 3.1,
typename class.


, . ,

, .
. -
, . const
, . -
: int double.
int main ()
( int i;
int v[10] = {0};
int w[10] - { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (i = 0; i < 10; ++i) cout v[i] ' '; //
cout endl;
Copyfw, v, 10); // w -> v
for (i = 0; i < 10; ++i) cout v[i] ' ';
cout endl;
double vl[10] -= {0};
double wl[10] = { 1.1, 2.1, 3.1, 4, 5, 6, 7, 8, 9, 10.1 };
for (i = 0; i < 10; --i) cout vl[i] ' ';
cout endl;
Copytwl, vl, 10); // wl -> vl
for (i = 0; i < 10; ++i) cout vl[i] ' ';
cout endl;
return 0;
3. 115

:
0 0 0 0 0 0 0 0 0 0 // v int
1 2 3 4 5 6 8 9 1 0 // v < - w
0000000000 // vl double
1.1 2.1 3.1 4 5 7 8 9 10.1 // vl <- wl

, const,
.
, -
, & -
- .
, -
, ,
2 ( 3.6).

j 3.6. 2

template < typename T >


void Mult 2 ( array [], int n)
{ for (int i = 0; i < n; -t-+i) array[i] *= 2; }

, -
.
"
".
, , .
, , , -
: " ".
void Divide (double array [], int n, double m)
{ for {int i = 0; i < n; ++i) array[i] /= m; }

"" . , -

double w[10] = { 1.1, 2.1, 3.1, 4, 5, 6, 7, 8, 9, 10.1 };
Divide (w, 10, 2};
Divide (w, 10, 3) ;
Divide (w, 10, 7.543);

, -
. ? :
Divide (w, 10, w[5] ) ;

, , . -
, -
116 _[. C++

; ,
. 1.
w Divide, ,
,
1. -
. " " . -
, i,
!

, . -
, .
, ,

, .


-

.
(. . 7),
. -
: -
, . -
,
. -
,
/ ,
[9, 14, 39, 41, 45],
, .
. -
, . ,
, , .
-
.
. ,
:
int BinarySearch(int a [ ] , int begin, int end, int v)

begin end -
, a v . (
3.7) , ,
-1, .
3. 117

. 3.7.

int BinarySearch(const int a[], int begin, int end, int v)


[ while (begin <= end)
{ int m = (begin + end) / 2; //
if (v == a[m]) return m;
if (v < a[m]) end = m 1; // " "
else begin = m + 1; // " "
}
return -1; / /

, -
, const. -
-
. , :
begin = 0;
end = begin + (n 1);

,
, -
. -
.
, -
, . .
-
-.
, , -
. -
, " ",
-
.
,
. .
- "". -
3.8.

[ 3,8.

template < typename T>


void SortArray(T a[], int n)
i for (int i = 0; i < n - 1; ++i)
{ min = a [ i} ;
118 I. C++

for (int j = i + 1, k = i; j < n;


if (a[j] < min) { min = a [ j ] ; k - j; }
a[k] = a [ i ) ; //
a[i] = min; //

min, k- -
, .

double wl[10] = { 9, 8, 7, , 5, 4, 3, 2, 1, 10.1 };
for (i = 0; i < 10; ++i) cout wl[i] ' ';
cout endl;
SortArray(wl, 10);
for (i = 0; i < 10; ++i) cout wl[i] ' ';
cout endl;

:
9 8 7 6 5 4 3 2 1 10.1 / /
1 2 3 4 5 6 7 8 9 10.1 / /

. ,
, "" .
, -
.
1 -
. , -
: -
, , I,
. ,
"" .
-
. , , -1,
, .
- 3.9.

! 3.9, -

template < typename T>


int SortArrayfT a[], int first, int last)
{ if ((last <= first) I I (first < 0} I I (last <= 0)) return -1
for (int i = first; i < last - 1; --i)
( min = a[i];
3.

for (int j = i -i- I, k = i; j < last; ++j )


if ( a [ j ] < min) { min = a [ j ] ; k = j ; }
a[k] = a[i];
a [ i ] = min;

return 0;

, . -

int w[10] - ( 10, 2, 3, S, 8, 6, 5, 7, 4, 1};
for (i = 0; i < 10; ++i) cout w[ij ' ';
-
cout endl;
SortArray(w, 0, 10);
for (i = 0; i < 10; ++i) cout w[i] ' ';
cout endl;
:
10 2 3 9 8 6 5 7 4 1 //
1 2 3 4 5 6 7 8 9 10 //

.
.


C++ ,
. -
. -
, -
. typedef:
typedef float array [10]; // array - 10 float
array m [ 1 0 ] ; // 10 float [10]

-
:
int a[10] [10];
, :
int [3] [3] = { { 1, 2, 3 }, { 2, 3 } };

3- ,
3 . [0] { 1 , 2 , 3 ), A[i]
{ 2, , }, .
120 I. C++

:
[ 0 ] , [1], [ 2 ] . ,
. . 3.1 , .

] AM] A [2]
0^ 0 |
|0][0] [0][1] [0][2] |1][0] [1][1] [1][2] [2][0] A[2]|1J A [2] [2]

. 3.1. [ 3 ] [3]


A[i] [j], , .
"
:
int [ 3 ] [ 3 ] - { 1, 2, 3, 2, 3 };

. , -
, , :
int [ ] [ 3 ] = { { 1, 2, 3 }, { 2, 3 } };

,
(. 3.2), 3 .

API A[1]
1 ~2 ~]2 [0
[0][0] [0][1] [0][2]

. 3.2. [ ] [3]

[ 0 ] , [ 1 ]
, .
- ,
. 4.

, (. 3.1). -

. . -
weekoay , -
:
cout [>] encil;
3. _ 121


, C++ .
,
.
[17], -
. C++ string
C++.


. string
,
Borland C++ 3.1.

, char. -
, 7 ,
char.
:
char s [ ] = "";

,
, :
char s [ ] = { ' 1 , ' ' , ' ' , ' ' , ' ' , ' ' , ' 1 , ' ' , ' ' , ' ' , ' ' };
, , "" ,
char ( ) ""
.
7 , -
.
,
:
char Days [] [11] = { "", "", "", "",
"", "", ""
1;
, -
.
3.10.

! 3.10.

tinclude <iostream.h>
int main ( )
{ unsigned int day, rn;
int month [12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
122 I. C++

char Days [] [11] = { "", "", "", "",


"", "", ""
};
begin:
cout " (1-12): ";
cin m;
if ((1 > m) || (m > 12)) goto begin; //
ram: cout " : ";
cin day;
if ({1 > day) I I (day > month [m])) goto mm; //
for (int i = 0, s = 0; i < m - 1; ++i)
s += month [i]; //
+= day; //
int weekDay = (s + 2) % 7; //
cout Days [weekDay] endl;
return 0;

Borland C++ 3.1 -


:

, -
-, -
. - ,
. ,
:
^include <iostream.h>
int main ( )
( cout sizeof ("") endl;
cout sizeof ("") endl;
return 0;

12 8 ,
- 1 , -
. , ""
"" 12 , ,
. - ""
"" , -
, "".
3. 123


Visual C++ 6 ,
Borland "" , .
, ,
, 12.
char Days[][12] = {"", "", "", "",
"", "", ""

. , -
. -
, -

.


( ) , -
.
( 3.11),
.

\ 3.11.

unsigned int Length (const char s[]) //


// '/
{ int L = 0;
while (s[L++]); //
return (L - 1) ;
1

, . -
, s -
. "" , -
, . L ,
1 -
L - 1.
.
( ) -
( ). ( -
3.12), ,
.
, .
124 !.

3.12, atoi

int atoi (const char number[])


( int n = 0, //
sign = 1; //
int i - 0;
if (number[i] == ' + ' ) ++i;
if (number[i] == ' - ' ) { sign = -1; ++i; }
while (i < Length(number))
{ n = n * 10 + (number[i] - ' 0 ' ) ; ++i; }
n *= sign; // ""
return n;
j

.
n = n * 10 4- (number[i] - ' ' } ;

. (number - ' 1 ) , ,
. . , -
. , , number 5472.
n , n
5. :
n = 5 * 10 + 4 = 54;

:
n = 54 * 10 + 7 = 547;

, , -
:
n = 547 * 10 + 24 = 5472;

sign . -
:
char s [ ] = "-5427";
int a -= a t o i ( s ) ;
int b = a t o i { " 3 4 5 6 " ) ;

, .
,
atoi, atoi atof, stdlib.h.

.
, - ,
- . -
. -
3. 125

. -
, (. -
3.4).
, -
. Length
(. 3.11). ( -
3.13) , , -1,
.
, , -
.

I 3.13.

int CopyStr(const char source[], char dest[]J


( if (Length(source) < Length(dest)) return -1;
for (int i = 0; i < Length(source); ++i) destfi] = source[i];
return 0;
1

:
char s[80] = (0}; // ""
int = CopyStr(" ", );

:
char ss[80] = ""; // ""
char dd[] = " ";
int = CopyStr(dd, ss) ;

""
. ,
, :
for (i = 0; i < 80; ++i) cout (int) s [ i ] ' ';
cout endl;
for (i - 0; i < 80; ++i) cout (int} ss[i] ' ';
ccut endl;

, . . , ,
.


, / . -
(. ) ,
:
cout Days[weekDay] endl;
126 I. C++

C++ , -
-.
, :
char s[] = "";
COut s endl;

,
char s [ ] = { ' 1 , ' ' , ' 1 , ' ' , '', ' 1 , ' 1 , ' ' , ' ' , ' ' , '' };
. ,
sizeof. -
-:
cout s i z e o f ( s ) endl;

,
.
^include <iostream.h>
int main()
{ char b = 'a';
char s[] = { 'a', 'b1, 'c'f 'd', 'e1, 'f };
int d = 10101;
cout sizeof (s), endl;
cout s endl;
1
Borland C++ 3.1 :
6

abcdefwa

6 s. ,
"" wa. Visual C++ 6, -
"":
6
abcdef![!!|8_

, -
.
, -
, . -
""
Length (. 3.11).
sizeof, -
:
3. 127

char s[10] = "!";


cout sizeof(s) endl; // 10
cout Length(s) endl; // 7

, cout , -
-.
- "-
" . , , ,
:
cin s;

, , -
:
char s [80];
. :
^include <iostreara.h>
int main()
{ char sd[80];
cin sd;
cout sd endl;
return 0;
}

" " -
<Enter>. . -
"".
.

for (int i = 0; i < 80; ++i) cout sd[i];
, "".
,
. , . 6, -
/ C++.


( ) , . -
, : , -
. C++ :
struct Date { unsigned int year, month, day; };
128 I. C++

, 3.14.

j 3.14.

enum Month
{ jan = 1, feb, mar, apr, may, jun, j u l , aug, sep, oct, nov, dec };
struct Date
.
unsigned int year;
Month month; // .
unsigned int day;


Month Visual C++ 6
"" ,
: fatal error ciooi: INTERNAL COMPILER ERROR, -
. , Month
,
oct dec. Month,
(Get oct_),
"" . -
, .

. , " ",
Date, , .

Date , :
Date today; //
Date dl = { 2003, feb, 22 }; //
Date d = { 2000, jan }; //
Date d2 = dl; //
Date d3(d2); //

. , -
, .
C++ -
, .
:
Date z = { z.year = 2004, z.month - feb, z.day - 28 };

,

.
3. 129

,
struct Old { int a; float b; char ch; };
Old z = { z.b = 2.5, z.ch-'O 1 };

2 ( 2.5 0.5), -
48 ( '0'), ch .
, -
, :
struct ( unsigned int year, month, day; ) dl = { 2C03, feb, 22 );

-
Date, , -
.
"-
" . ,
di, di.year, di.month,
di.day. , ,
:
dl - today;

, :
cout " ( ):";
cin di.year dl.month di.day;

:
cout di.year '-' dl.month '-' di.day;


C++ , -
,
. -
, . -
. -
3.15.

struct Old { int a; float b; };


Old g(01d p) //

5 . ! 264
130 t. C++

{ p.a = 6;
p.b - 7; //
return p; //
}
int main(void) //
{ Old = { 1, 2 }, x; //
x = g(y); //
= g fy) ; //

old ,
old
. x = g(y) . =
. = 7; , -
.
,
.
-
, . .
.
3.16.

! 3,16.

struct Old { int a; float b;};


Old g(01d 6p) //
{ p.a = p.b + 6;
p.b = p. a + 7 ;
return p;

int main(void)
{ Old = { 1, 2 }, x;
x = g(yX; //
}

- g f y ) ,
.
const
, ( )
. ,
3.17,
.
3. 131

3117.

struct Old { int a; float b;};


Old g(const Old &p) //
( Old r; //
. = . + 6;
. *= . + 7;
return r;
)
int main(void)
{ Old = { 1, 2 ), x;
x = g(y); //
- giy); //

, -
. -
:
Old g(01d p ( 1, 2 })

- .
-
, :
Old g(Old p = t)


Old g(01d p ( t ) )

, .

. ,
. ,
.
,
, (
3.18).

3.18. -

struct Old //
{ int a; float b; };
Old = { 1, 2 }, x; //
Old q(01d p); //
Old g(01d p = g(y)} //
132 I.

( - - 6;
. = 7; //
return ; //

void main(void)
{ = g(); //
cout " =" . ";" y.b endl;
cout "x =" x.a ";" x.b endl;
= g(y); //
cout " =" . ";" y.b endl;
}

-
.
( ).
:


, ,
. , -

struct Complex { double re, im; };

struct Rational { unsigned long num/ denum; ];

:
struct Point { double x, ; };

" ", -
. .
"" :
template <class T>
struct Pair { first; T second; };

-, -
3.19.
3. 133

; 3.19. -

template <class Tl, class T2>


struct Pair
( Tl first; T2 second; };

,
.
,
, . . main
. -
, . -
, , ,
.
:
Pair <double, double> a; //
Pair <double, double> t = { 1, 2 }; //
Pair <double, double> r = t; //
Pair <double, double> d{t); //
Pair <int, double> p = ( 1.1, 2 };

Visual C++ 6 -
.
,
:
Pair <double, double> t = { 1, 2 };
Pair <int, double> k = t;

, , .
,
, , Date.
:
Pair <Date, double> rr;

typedef.
typedef Pair <double r double> ddPair;
typedef Pair <int, double> idPair;

-
. '
Windows, -
.
134 I. C++


:
, -
-
. , 10 -
. :
struct Number {double number[10]; Date date; );

,

:
Number N = { { 1.1, 2 . 2 , 3.3, 4 . 4 , 5.5, 6 . 6 , 7.7, 8.8, 9.9, 0},
{ 2003, feb, 2 } };

100 ,
:
Number NN[100];

, , .
-
. , :
NN[0].numberfO] - 1.2;
NN[0].numberfl] = 3.4;
// . . .
NN[0].number[9] = 5.7;
NN[0].date.year = 2003;
NN[0].date.month = feb;
NN[0].date.day = 22;

,
.
:
-
struct Array { double a[10]; };
Array a = { (1, 2, 3, 4, 5, 6, 7, 8, 9,0 } }; //
//
Array b = ;
Array ;
= ;

-
:
Array = ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; //
3. 135

-
, .
, , -
,
. ,
-
. :
struct Person { char fio[30]; Date BirthDay; };
:
Person * { "", ( 1955, 12, 21 } };
:
Person w [ 2 ] = { { "", {1955, 12, 21 } },
( "", { 1975, 10, 12 } }
};

, ,
.
person :
Person mans[100];

BithDay mans
:
raans[5].BirthDay.year = 2003;
mans[5].BirthDay.month = feb;
mans[5].BirthDay.day = 22;

,
-:
mans[5].fio = ""; //
.
, . .
.
:
string.h, string,
,
Person .

,
.
. -
.
136 /. C++

. -
(. 3.2) (
3.20).
'!" "!">!!' ,-.... ............... .-.-, ,; |

; 3.20.
; ,_.. , , ..*.., ." -* "...."* ,...,.,.. ,._*^; . ,.,:,.,.,,, .,";.......^~.~, ...L,*.-!

double Average(double array[], int n)


{ return Summa(array, n) / n; }

N Number
:
double a = Average(N.number, 10);

,
, " "
. , ,
. ,
. -,
- -
.
- , . . Java.
,
, .
3.21.

| 3.21.

struct Number
{ double number [10];
Date date;
double Average (void) { return Summa (number, 10) / 10; }

Number, -
. ,
:
cout N. Average () endl;

,
. .
, -
, .
inline-,
3. 137

, .
- , ,
, .
::.
double Number :: Average(void) 1 return Summa(number, 10) / 10; }

: -
, . , -

, . ,
,
.
, , ,
. -
, -
, ' .
enum, , , -
. -
3.22.

3.22.

struct Number
{ enum { k = 10 };
double number[k];
Date date;
1;
double Number :: Average(void) { return Summa (number, k) / k; }

( ) -
,
static int k;


int Number :: k = 10;

.
:
static const int k 10;

- :
const int Number :: k;
138 I. C++


, Visual C++ 6, Borland C++ 5
.
Borland C++ Builder 6.

, .
: Number
10 . , -
, ,
10 , 12,
7. -
. .
-
, 3.23.

template <int n>


struct Number
{ double number [n] ;
Date date;
double Average (void) { return Summa (number, n) / n; }

.
"" , 3.24,
:

3.24. ""
.'. : ', :.. .-. ;,...
template <int n>
struct Number
{ double number[n];
Date date;
double Average(void);
);
template < int n >
double Number <n> :: Average(void) ( return Summa (number, n) / n; }

, template ,
.
, , .
:
3. 139

Number<12>N = { { 1.1, 2,2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 0 },
{ 2003, feb_, 2 } };
cout N.Average() endl; // 12
Number<10>M = { { 1.1, -2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 0 },
{ 2003, _, 2 } };
cout M.Average endl; // 10

12 (-
2 ) 4.125.
10 4.5. , -
,
, " -
".
,
. "-
" , 3.25.

: 3.25.
!;..,..; : .-. ; .-... .'. :....,.,'. ;.-. .:,.;

template <int n = 10>


struct Number
{ double number[n];
Date date;
double Average(void) { return Summa(number, n) / n; )
1;

,
:
Nuniber<12>N = { { 1.1, 2.2, 3.3, 4 . 4 , 5.5, 6.6, 7 . 7 , 8.8, 9.9, 0 },
( 2003, feb_, 2 } );
cout N.Average() endl; // 12
NumberOM = { { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 0 },
{ 2003, feb_, 2 } };
cout M.Averaged endl; // 10

, . . .
, ,
.
, ,
.
. , Date
.
140 I. C++


- -
.
Visual C++6. ^
3.26.
: '. ' ' """"
I 3.26.

^include <iostream.h>
int main ()
( struct AAA { char ch; double b; };
struct BBB { double b; char ch; };
struct CCC { int b; char ch; };
struct DDD { double b; char ch;
void f(void) { cout "Hello!" endl; }

cout "AAA sizeof(AAA) endl;


cout "BBB siseof(BBB) endl;
cout "CCC sizeof(CCC) endl;
cout "DDD sizeof(DDD) endl;
return 0;

:
16
16

DDD 16

, , -
:
-
, .
16, . . double
8. 8 , . . int
4 ;

.
.
Release
Minimize Size. Borland C++ Builder , Release
3. 14_1_

. Borland C++ 3.1


:
10
10
4
DDD 10


, , -
:
Bor-
land C++ 3.1 , char,
, ;
char 2 ;

.
.
-
, sizeof (), -
ttpragma.
,
(linker). ,
p a c k ( i ) .

( J
Borland C++ 3.1 " ". -
: ttpragma option --.
.

,
^pragma p a c k ( l )

^include, :
. 9
9
5
DDD 9
i
, , .
4

C++ -
. ,
. ,
. -
, , C++.
. [48],
" C++" - . -
, -
. -
. , -
.
,
-
. " -
", .
" ":
"" -- "". -
Java .

-
. 3 ,
(. 3.2). , -
, , -
:
Surama(A, A +' 10)
SuircnalA + 3, + 3)
144 _ I, C++

10- . ,
, 5- , [ 3 ] [ 7 ] . , -

. ,
: \1 .
, -
. - -
:

, , -.
,
.
. -
,
4.1.
: ............... "- .......... ""-* .................... ....... " .............................. > ......... -.' ................................... *...'. ......... ,...,. ......... , ...... ._, ........ ..-~. ----- -..* ......... * ....... . . ............ ..^

\ 4.1.

template <class T>


SummafT *begin, T *end)
{ sum = 0;
if (begin < end) //
for {/begin < end; ++begin) sum += *begin;
return sum;

, . -
.
:
sum += *begin; //

, *begin ,
sum. , .
, -
. , -
.


, .
Intel . .
, ( 1). -
4. 145

. -
, -
. ,
, -
. , -
.
, -
, . ,
C++ , -
.
, .
, . -
, ,
. :
int *p; // int

. -

int ; // int
,
.
,
, & ():
int a;
= 3; //
int *pa;
pa = &; //

- -
. . 4.1.

&

, *
. 4.1.

C++ (and),
,
. , -
, :
int sra = ;
146 ^.^^ C++

- , , ,
, .
.
" " ,
. -
, .
- -
:
int *p;

-
, * . -
, -
. -
- begin (. 4.1).
- , -
, , -
. , & ( ) * (-
) . -
,
4.2.

| 4.2.

ttinclude <iostream.h>
int main()
{ int a = 5, b = 0; / / 1
int *pa; pa = &a; // 2
cout "a =" a endl; // 3
cout "*pa =" *pa ; endl; // 4
cout "pa =" pa endl; // 4 . 1
b - *pa - 3; // 5
cout "b =" b endl; //
if fa == *pa) *pa = b; / / 7
cout "a =" a endl; // 8
cout "*pa =" *pa endl; // 9
b = 6; pa = fib; // 10
cout "b = " b endl; // 11
cout "*pa =" *pa endl; // 12
cout "a =" a endl; // 13
return 0;
4. 147

:
= 5 // 3
* = 5 // 4
pa = Cx0066FDE8 // 4.1
b = 2 //
= 2 // 8
* =2 // 9
b = 6 // 11
* == // 12
= 2 // 13
1 2 .
. 4 -
, * . -
, * ,' 4.1, -
. 5
.
* - 3
: ( )
, . -
if 8 9
*: * .


. 11-13
.


, " ",
. , C++
(
):
*.
, ;
void*;
.
. -
. -
148 I. ^ C++

, -
.
.

,
,
.
-
.


:
*

, -
, -
(. 4.1). , -
, -
. , C++, -
. , ,
:
int* pa, pb;

, .
, " " - - -
, .

:
int = 3; //
int * = &; //
int *h{&a); // h
int *r = h; //

.
h, . *h, *pa * -
: , -
.
, "" -
, .
, "". -
, , -
. -
4. 149

, -
. , , -
:
double * = 0;

:
if ( ! p > . . .

"".
NULL, -
.
, -,
:
'
int 1 ;
.
const int ci = 5;
const int *pi = &ci;

pi
. - -
:
int *const pi = Sci; // ci -
!
, .
. -
, ,
:
D *
, ;
const * const *
, ;
* const -
, ;
const const *
const * const , .
, :
inn **p;

. "- -..."
.
150 I. C++


;
void *;

C++ void,

. (. . 1), C++
: -
:
()
()

,
:
int a = ;
int *pa = 5;
void *vpa = (void *)5a; // int* void*
void *vp = {void *)pa; // int* void*

:
-
int a = 6;
int *pa;
void *vpa = (void *)&a; // int* void*
pa - (int *)vpa; // void* int*

-
:
int a = 6;
double *dp - (double *)&;
int *ip - (int *)dp;

, (. . 1), C++ -
:
static_cast< i ran>()
reinterpret__cast<TMn> ()
, void * :
int = 6;
int *pa = &;
void *pointer = static_cast<void *>(sa);
pointer = reinterpret cast<void *>(pa);
4.^ 151

-
static_cast :
int = 6;
int *pa = &;
double *ddp = static_cast<double *>(); //
int *ip = static_cast<int *>(dp); //
double *dp = reinterpret_cast<double * > ( & a ) ; //

Visual C++ 6 2440 -


.
, , .
, , , -
4.3.

4.3.

int main()
{ cout sizeof(void *) endl;
cout sizeof(char *) endl;
cout sizeof(unsigned char *) endi;
cout sizeof(sort *) endl;
cout sizeof(unsigned short *) endl;
cout sizeof(int*) endi;
cout sizeof(unsigned int *) endl;
cout sizeof(long *) endl;
cout sizeof(unsigned long *) endl;
cout << sizeofffloat *) endl;
cout sizeof(double *) endl;
cout sizeof(long double *} endl;
return 0;
}

Visual C++ 6 Borland C++ Buil-


der 6, 4 .
-
(. 3.2). Borland C++3.1
. -
, . . MS DOS.


C++ -
. ,
, -. -
152 I, C++

: -
. :
int a[10];
int *p;
.
- -
:

, , -
+ i
1- . , i --
, . -
, i sizeof (). 1- -
, :
*( + i)
, *
+.
p[i] a [ i ] . * + i
: [0] + i. , a[i], * ( + i) -
, .
, -
, *( + i), a [ i j .
, - , -
(
). C++ -
. , -
*{ + i) *{i + ) -
, , 1[]. -
i 2.
coat 3[] endl;
cout (i + 1)[] endl;
ccut i[ + 1] endl;
cout * (i + + 1) endl;
[3].
, ++
++ "" :
sizeof () .
4. __ __ _ 153

, , 5- . -

// 2
// 3

, , :
i 5-
"" ;
2 5- -
;
"" -
6- .
,
.
. . -
. -
:
int a [10] ;
int *pl = &[1] ;
int *p2 = &[8] ;

cout 2 - pi endl;

7 [1]
[8].


, ,
.
C++ , -

. swap, -
. - -
:
.
void Swap(int *, int *b) //
{ int t = *; * = *b; *b = t; } //

, -
- . -
.
154 I. _ C++

-
(
), :
Swap(&x, fiy) ;

, , -
,
, &
( -
).
, -
. :
G - ;
- .
Swap -
4.4.

-:
-
4.4.
'.

-

','
f

void Swap(int *&vl, int *&v2)


I int *t - v2; v2 = vl; vl = t; }

int *&vi : vl
int. -
. swap :
int i - 10;
int j = 20;
int *pi = si;
.
int *pj = Sj; //
Swap (pi, pj>;

swap -
4.5.
:
;'"'"'""'"""""-'-' ""'" :"':"" ; '- -""'"'.' ' '"'-' '.'""' ."' "" ' " I

: 4.5.

void S w a p f i n t **vl, int **v2)


{ int *tmp - *v2; *v2 - *vl; *vl - tmp; }

!
"" :
Swap(&pi, s p j ) ;
4. (^ 155


-
.


void , -
.
void * :
void f(void *p)

, :
void *f(void *p)

,
.
.
-
.
. : -
, . -
-
void *. , ,
. . .
switch,
.
4.6.

| 4.6.

enum Type { Int, Long, UnsignedLong, Float, Double };


int NuirberToASCII (Type t, void *n, char buf fer [ ] )
f switch(t)
{ case Int: { int k ^ *(int *) n; // ""
buffer = _itoa(k, buffer,10); break; }
case Long: { long k = *{long *) n; // ""
buffer = _ltoa(k, buffer,10); break; }
case UnsignedLong:
{ unsigned long k = *(unsigned long *} n;
buffer = _ultoa(k, buffer,10); break; }
case Float: ( double k = *(float *) n; // ""
//
buffer = gcvt(k, 4, buffer); break;
156 _ I. C++

case Double: { double k = * (double *} n;

buffer = _gcvt(k, 16, b u f f e r ) ; break; }


default: cout "Wrong types" endl;
return -1;
}
return 0;

"" ,
-
switch. . -
, -, -
. , -
. case
-
"" .
, ,
stdlib.h.
, 4.7.

4.7.

int main(void)
{ int m = -123;
long ram = -321;
unsigned long mu = 1234567890;
float d - -123.4567e-9;
double dd = -123.45678901234;
char ch[50];
NumberToASCII(Int, &m, ch);
cout ch endl;
NumberToASCII(Long, &mm, ch);
cout ch endl;
NuriiberToASCII (UnsignedLong, smu, ch) ;
cout ch endl;
NumberToASCII(Float, &d, ch) ;
cout ch endl;
NumberToASCII{Double, &dd, ch);
cout ch endl;
return 0;
4 157

.
(- ):
int NumberToASCIKint n r char buffer[]);
int NumberToASCII(long n, char buffer[]);
int NumberToASCII(unsigned long n, char buffer[]);
int NumberToASCII(float n, char buffer []);
int NumberToASCII(double n, char buffer[j);

:
int NumberToASCII(int n, char buffer[])
{ buffer = _itoa(k, buffer, 10); }

, ,
.
, -
,
-
.


, -
, . ,
:
unsigned int Length(const char *s)
{ int L = 0; while (s[L++]); return (L -1); }

-
-. -
,
, :
void Print(char *s = " ")
{ cout endl; }

Visual C++ 6 ""


:
void Print(char s[] = " ")


-. . [37] -
, :
const char* error_message(int i)
{ return "String of return"; }
158 t. C++

const, . i
, -
-.
, ,
-.


. 2 , , ,
. -
Borland C++3.1. -
. , -
Windows,
, Windows.
, Windows ASCII, -
. , -
Windows- ASCII. -
WinAPI charToOem.
4.8.

i 4.8,
i : ,. .-.,.....,. '.., .,.,!
BOOL CharToOem(
LPCTSTR IpszSrc //
LPSTR IpszDst //

:
IpszSrc ;
IpszDst , .
,
, Windows, -
-
. , , -
, .
. -
, windows.h. -
, , -
4.9.
: v;--V- " ">" " -" '" !'"' '. ' ' f";.""v :

! 4.9.

char * Rus(char i n [ ] , char o u t [ ] )


{ if (CharToOem(in, out}} return out; else return 0; }
4. 159

,
:
char * Rusfchar *in, char *out)

-
:
char s[100] = " !";
cout Rus(s, s) endl;

-:
char s[100];
cout ' ! ! Kellc!", s) endl;

string.h
:
, , -
-
.
string.h. C++.
BOB-
(char *). , , -
, :
size_t strlen(const char *s);
sizej: typedef unsigned
int.
. 4.1.
, -
, ASCII-. -
.
strlwr strupr, -,
. -
, ,
. strlwr
4.10.

I 4.10. strlwr

char *strRlwr(char *s}


{ const int nA - 33; //
static const char RusB[nA] = "";
static const char RusM[nA] = "";
160 I. C++

int L = strlen ( s ) ;
for (int i = 0; i < L; ++i)
for (int j = 0; j < nA; ++j}
if (s[i]=RusB[j]J { s[i] = RusM[j]; break; }
strlwr(s);
return s;

strRupr strRlwr :
if (s[i] == RusM[j]} ( s[i] = R u s B t j l ; break; }
.
: RUSB RUSM -
.

4.1.


char *strcat{char *dst, src dst.
const char *src)
strlen(src + dst)
char*strncat (char*dst, const char*src, -
size_t n) src dst.
strlen {dst! + .
char *strchr (const char *s, int ) s

const char *strrchr (const char *s,


int )
const char*strstr (const char*sl, s2 si
const char*s2)
int strcinp (const char *sl.
const char *s2) ( ).
si < s2, < 0.
si - 2, == 0.
si > s2, >
int stricmpfconst char *sl,
const char *s2)
int strncitp(const char*sl, ,
const char*s2, size__t n}
char *strcpy(char *dst, src dst
const char *)
4. 161

4.1 ()

char *strncpy(char *dst, src


const char *src, size t n) dfet

char *strlwr(char *s)

char *strupr(char *s\


s

Person (. . 3).

strcpy:
strcpy(mans[5].fio, "");



Borland . -
Visual C++ 6 .,
, . ,
cout Rus(strRlwr("!"), ss) endl;

,
char ss[100] = "!";
cout Rus(strRlur(ss), ss) endl;

. , - -
-, , -
(char *). ,
:
char *sl = " ";
char s2[] = " ";

, ,
:
si , 4 ; 2
, 16 (15 -
);

6 1264
162 I, C++

si
; 2 ,
, 2[1] = ' ( ;
si = s2; s2 = si.
, -, -
. " ".
Borland .
, C++ -
,
cout ("abcdef" + 2 ) [ 0 ] endl;
cout 2["abcdef"] endl;


cout "abcdef" + 2 endl;

"". , -
-, .
. 3
.
: .
char *Days[] = {"", "", "", "",
"", "", ""
};
, (-
).
, -
.
:
char s[3][10] = {"", "", ""};
char * s [ l G ] = {"", "", ""};


. 4.2 4.3.
, , -
, , -
.
, 4.11, -
, -
.
. -
, .
4. 163

char s[3][10]
s[0]

9(2]

10
. 4.2.

char 413]


. 4.3.

4.11.

void PrintArrayString(char * m [ ] , int n)


( for (i - 0; i < n; +4-1) cout Days[i] endl; J

- ,
, :
.
.
, 4.12.

I 4.12.

void PrintArrayString (char *m[]} //


{ for (i = 0; Daysfi]; --i) // ?
cout Days[i] endl;

int main (void)


{ char *s[] = //
{ "First", "Second", "Three", 0 } ; //
PrintArrayString (s) ; //
return 0;

,
( ) .
164 - I. C++


, ,
C++ , -
. :
,
, ,
4 . : -
.

.
. , " "
, -
, . -
new, -
delete.
int *p;


= new int;
* = 10;


delete ;
= 0;

, , , -
, .
:
int * = new i n t ( 1 0 2 4 ) ;

, . .
. , Date
(. 3.14) :
Date *p = new Date;


(. . 8).
"" .
:
(*)-year - 2004;
4. _ 165

, . . "" -
.
->:
-> month = 12;

, ,
"" .
new , , -
, . ,
.
delete ( !).
, ,
.
.
new[], , , -
deietef].
.

(~ j

, -
. , Borland "" ,
char *s = new char [sin (x) ].

, .
, -
4.13. string. h, -
, .
,
. , -
.

| 4.13.

char *Duplicate (char *s)


{ char *ss = new char [strlen(s) + 1]; //
strcpy(ss, s) ; //
return ss; //

, ,
.
166 I. C++

, . . ,
.
, -

new. , ,
, . -
-
4.14.

4.14.

int f f f i n t m) ( return m; } //
int* f (int n, int *a)
( // -
return a;

int main (void)


{ int k - 3;
int *p = f(k, new int[ff(k)]); //
//
delete []p; //
return 0;

,
, .
, ,
, , .


, -
, , C++
, . -
, ,
. -
, .
, R. , -
r [ i ] [ j ] :

, R
:
4. ^}

i < n; i++)
j < n; j + + )
for (int k - 0; k < n; k-t-+). s += a[i] [k] * fa[k] [jj;

: , 5, V, 17
. . , -
, . . -
, -
.
n x :
float *r = new float [n] [n] ;

, . .
. -
:
, -
. , "" -
. C++ :
int n; //
cout : ;
cin ;
float **m = new float * [n] ; //
for (int i = 0; i < n; ++i) m[i] = new float [nj; //

.
, -
. -
, :
,
for (i = 0 ; i < n; i++) delete a[i];
delete []a;

, .
4.15.

; 4.1 5.

int main(void) //
{ int i, j , n;
cout " , , R ";
cin n;
//
float **a = new float*[n]; //
168 I. C++

for {j = 0; j < n; j++) a[j] = new float[n];


float **b = new float*[n]; //
for (j = 0; j < n; j++) [ = new float [n] ;
float **r = new float*[n]; // R
for (j = 0; j < n; j++) r[j] = new float[n];
//
for (i - 0; i < n; i++)
( for (j = 0; j < n; j-t-+') cin a[i][j]; cout endl; }
cout endl;
for (i = 0; i < n; i++)
{ for (j = 0; j < n; j++) cin b[i][j]; cout endl; )
cout endl;
//
for (i
4 = 0 ; i < n; i++)
for (j =0; j < n; j++)
{ float s = 0; for (int k = 0; k < n; k++)
s += a[i] [k] * b[k] [j]; r[i] [j] = s; }
II
cout "" endl;
for (i =0; i < n; i++)
{ for (j = 0; j < n; j++) cout setw(6) r[i][j]; cout endl; }
for (i - 0; i < n; i++) delete a[i]; delete []a;
for (i = 0; i < n; i++) delete b[i]; delete []b;
for (i = 0; i < n; i++) delete r[il; delete []r;
return 0;

, -
, i (). -
.


-
. .
. -
, . . .
, -
, :
void PrintMatrix(int m[] [], int n)


void PrintMatrix(int *m[], int n)
4. -/69


void PrintMatrix(int **m, int n)
:
for (int i = 0; i < n - 1; i++)
{ for (int j = i + 1; j < n; j+-t-) cout m [ i ] [ j ]; cout endl; }

.
. -
, STL
.


. ,
4.16,
.
.
.
4.16.

struct Old { int a; float b; };


Old g(01d *p) // -
( p -> a = p -> b + 6; // "->"
(*). = -> + 7; // "."
return *p; // ,
}
void main(void)
{ Old - { . - 1, y.b = 2 ), ;
x = g(fiy); //
I

, -
.
= g(fiy) ;
, .


,
. , -
, -
I. C++

. ,
. , -
, .
.

, . , -
. -
, . Node -
:
struct Node { Car car__; Node *next; };
, , Node
Mode *next .
, , "-
".
Node,
Node *. -
. , -
, Mode, -
:
Node *Head, *Tail; //
Node *Cars; //

,
.
: ,
, . . , -
, . .
Head Tail. -
. 4.4.

Head

. 4.4.

:
Cars = new Node; //
Cars -> next = 0; // 2
Head = Tail = Cars; //

2 -> next.
4. 171

:
Cars - new Node; //
*
Cars -> next = 0; //
Tail -> next = Cars; // 3,
Tail = Cars; // 4,

4 , -
"" .
:
Cars = Head; //
Head = Head -> next; //
delete Cars; //


, .
.
.
.
. -
. ,
:
Cars = new Node; //
Cars -> next = Head; //
Head = Cars; //

, -
, .
, , -
. ,
. (node)
, :
.
, . , -
. .
. (. 4.5) ,
.
,
, .
. 4.6. list -
STL.
. ,
, . 4.7.
172 I. C++

. -
, -
. () set () -
STL
.

Head

ik

. 4.5.

. 4.6.

Root

. 4.7.

, -
, ,
(. 4.8).
.
.
:
4. 173
template <class T>

struct Node
{ info; //
first *Node;
second *Node;
};
(
), , .

.
Left () Right ().
. , ,
"" ( ). -
" ", -- " ". "" ,
( ).

. 4.8.

\{
:
void *info;


. [5, 9, 14, 16, 34, 39, 40, 41, 45, 47]
,
, , -
.


, -
. , -
" " , -
, . :
, ;
"", .
174 I. C++

" ", " -


". ( 4.17)
" ".
;;;; ; ' V

4.17, : " "


int *f(void)
{ int Lokal = 1; //
return SLokal; //

void main (void)


{ int a - 2;
int *p = sa/
cout "*p =" *p endl; // * = 2
p = f(};
cout "*p =" *p endl; // *p' = 1
*p = 3;
cout "*p =" *p endl; // *p = 3
a = *p;
cout "*p =" *p endl; // *p = 6684152
cout "a =" a endl; // a = 6684152


"".
, -
f "",
: * = 1. -
* = ,
. ,
, * !
, " " , , -
. ,
:
, , "".
" " .
,
4.18.

) 4,18. : " "


void XI(void)
( int *p - new int[1000]; } //
void main (void)
{ XI (); } //
4. /75


xi, -
. , -
, . ,
. ,
, .
, - -
,
:
void X l ( i n t *p) // -
{ = new int[1000]; } //
void_mainfvoid)
{ int *pp = 0; X I ( p p ) ; } //

, -
4.18.
XI. , .
-, -
int **,
int *&p.
,
.


2 (. 3.6).
, -
,
. ,
.
""
.
C++ .
"" ( -
, ) - . -
, - ,
, . , -

, ,
Summa (. 4.1). , , ForEach.
, .
"" 4.19.
-

176 I. C++

4.19. ForEach
!. ; : -.. - - -I.'. :;...-:.1.*.-, : '

template <class T>


void ForEach( *begin, *end, f(T elem))
{ for (; begin < end; ++begin] *begin = f(*begin); }

, ,
. .
: , -
.
:
int f2(int a) //
{ return (* = 2); }
double f3(double a} // +2
{ return ( += 2 } ; }
int main(void)
{ int a[10] = { 1, 2, 3, 4, 5, 16, 7, 8, 9, 10 );
ForEach(a, a + 10, f 2 ) ;
for (int i - 0; i < 10; ++i) cout a [ i ] ' ';
cout endl;
double b[10] = f 10, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
ForEachfb, b + 10, f3) ;
for (int i = 0; i < 10; ++i) cout b [ i ] ' ';
cout endl;

.
:
2 4 6 8 10 32 14 16 18 20
12 14 15 16 17 18 19 20 21 22

, -
.
STL .
- , , -
-.
. -
. : .
, -, -
. , , -
, .
4. 177


, , , -
. -
, , -
.

:
;
;
,
, .
-
. , , -
, (,
Excel). C++ -
,
. union - .

union:
union DD { int a; double b; };
, union ,
-. . 3 , -
(. 3.26). ,
4.20, struct
union.

! 4.20.

ftinclu.de <iostream.h>
int main()
{ union { char ch; double b; };
union BBB { double b; char ch; };
union CCC { int b; char ch; };
union ODD { short b; char ch;
void f(void) { cout "Hello!" endl; }
} /"
cout "AAA" sizeof(AAA) endl;
cout "BBB" sizeof(BBB) endl;
cout "CCC" sizeof(CCC) endl;
cout "ODD" sizeof(ODD) endl;
return 0;
178 I. C++

:
. 8
8
4
DDD 2

, , ,
. , ,
, -
.
DD
. 4.9.
union DD

int a;

double b;
. 4.9. DD

( 4.21)
.

! 4.21.

struct CELL
{ char attrib; //
union //
{ char text[MAX + 1]; //
double value; //
struct //
f double fvalue; //
char formula[MAX + 1]; // -
} f;
} v;

, - . -, -
, -
, , ,
,
. ,
:
4. 779

CELL A;
Strcpy(A.v.text, " ");

, - -
, -
,
,
"" . ,
:
Strcpy(A.v.f.formula, "sin(al) * 0.12");
A.v.f.fvalue = Calculated;

main
Windows -
.
. , -
. C++ -
. -
, C++.
, C++,
main. -
, .
main
, argc
argv :
int main(int argc, char * a r g v [ ] }
-
:
int main(int argc, char **argv)
, ,
argc (argument count ) argv
(argument vector ) C++
. argc -
. 1, . . -
(, ) .
argv ,
.

> echo
.
argv , . 4.10.
180 I. C++

argv *argv[]
h 0
|
0

. 4.10.
>

C++ , argv[argc] .
, -
. Windows (
MSDOS), ( -
). , -
, "". -
,

, atof .
( 4.22),
.
.

1 4.22. main
itjtf,,:....(Ub ........... .'. ........ > ........ ---- .......... - ...................... ( .............. -'- ............ ii.i ....................... <'; ............ .- ............. -. ............ -

^include <iostream.h>
int main(int argc, char *argv[])
{ if (argc ! = 2) //
{ cout " !" endl
return 1;
}
cout "!" argv[l] endl;
// if (argv [argc] 0) cout " !" endl;
return 0;

VW.EXE,
,, ,
> VW " , ?"
:
! , ?
'
4. 181


> VW

:
!

argc argv
(99), C++,
. -
,
. Borland C++ Builder 6 ,
:
int main()
int mainfint argc)
int mainfint argc, char * argv[])
int mainfint argc, char * argv[], char * env[])

Visual C++ 6. -
. , ,
path. , -
env, environment.
main
int. , ,
, return. -
. -
( ).
.
main -
, :
void main(void) { / / . . . }
, , -
. , [45], "",
,
int.
5

199S C++ .
" " -
. -
"" ,
C++ . "" -
,
C++, -
. -

. 1994 () -
( Meng Lee)
, STL
(Standart Template Library ). -
C++.
, -
- , , -
, .
, -
, , -
, . -
, . -
""
. ""
, (,
Visual C++ 6). , , , -
, "" STL, -
( ) .
184 I.


,
bool, true false C++
. -
, . .
(&&), (| |) (<).
,
, , , ,
,
. sizeof(bool) 1. ,
(. . " -
" . 1) 1 -
. :
false, true. , -
-
.
booiaipha, :
cout booiaipha (a > 1) endl;
true false -
.

(" j
Borland C++ 3.1 Borland C++ 5 " " ,
.


, -
C++
string. " string "
[37, . 645], -
: -
; , .
, , ,
, . -
string,
, .
[]. ,
.
string :
;
J. 185

. , . . string ,
, .
-
, 5.1.

5.1.

^include <iostream>
^include <string>
using namespace std;
int main(void)
{ string sO; // 00
string si = "11111111111111111111" // 01
string s3 ("98765432109876543210"); // 02
char cs4[] = "01234567890123456789"; // 03
string s2 (si);
cout "s2 =" s2 endl; // 04
string s4(cs4);
cout "s4 =" s4 endl; // 05
string s5(cs4, 15);
cout "s5 =" s5 endl; // 06
string s6(cs4, 15, 5);
cout "36 =" s6 endl; // 07
string s61(cs4, C54 + 4);
cout "s61 -" s61 endl; // 08
//string s7(s3, 15);
cout s7 endl; // 09
string s8 (s3, 15, 5};
cout "58 =" s8 endl; // 10
string s7(s3.begin(), s3.end());
cout "s7 =" s7 endl; // 11
string s71 (s3.begin(), s3.begin() + 6);
cout "s71 =" s71 endl; // 12
string s72(s3.begin(), 15);
cout "s72 =" s72 endl; // 13
string s73is3.end() 7, s3.end());
cout "s73 =" s73 endl;
string s74(s3.begin() + 6, s3.end() 7);
cout "s74=" s74 endl;
string s9(20, '-');
cout "s9 =" s9 endl; // 14
si = s3;
cout "si =" si endl; // 15
s2 += si;
186 _ ^ C++

cout "s2 ~" s2 endl; // 16


s3 = s3 + s9;
cout "s3 =" s3 endl; // 17
s4 += "abcdef";
cout "s4 =" s4 endl; // 18
1
54 += ' I ;
cout <<"s4 =" 54 endl; // 19
s4 = $4 + "abcdef";
cout "s4 =" s4 endl; // 20
S
4 = 54 + 2 1 ;
cout "s4 =" s4 endl; // 21
s5 = si [0] + s9;
cout "s5 =" 35 endl; // 22
cout "sT. begin () =" s7.begin() endl; // 23
cout "s7.begin() [2] =" s'7 .begin () [2] endl; // 24
cout "s7. begin () + 2 =" s 7. begin 0 + 2 endl; // 25
cout "si-length =" si. length (} endl; // 26
cout "si. size =" si. size () endl; // 27
int n;
cin n;
string aa(n, ' - ' ) ; // 28
return 0;

,
"" . so, si, s2, "-
" . 34, s5, , ! -
, -
. s4 ,
15 , 5 , 15-. -
-
. , -
-.
09 Visual C++ 6,
Borland C++ 5 Borland C++ Builder 6
. [37, . 652] .


Visual C++ 6 (-
, STLport 4.5.3} .

s8 ( )
( 07): 5 , 15-. , 9
.
5. ^ 187

,
sv, s7i, s72, s73, s?4
. -
, .
( -
""),
. : begin(} end{).
, 7
s3, s7i 6
, si2 -
15- s3.
09, .
73 7 , s74
7 , 7-, . . ?4 "3210987".
, 23, 24, 25 ,
-
(. . "- "
. 4). ,
. -
, -
C++, ( -
).
15-22
. ,
. -
string assign, -
, . -
,
:
A.assign(B);
A.assign (, 15, 5);
A.assign(.begin(), .end());
A.assign(C);
A.assign(C + 10);
A.assign(100, '+');

assign, .
, 26 , , 27
. .
28 ,
, "" .
.
188 \._ ^ C++

"" , .
-, ^include
h. C++. -
" " , -
. string.h,
.
, " " - C++. -
, C++
"". , C++ string.h
cstring. ctype.h cctype, a
math.h cmath, . .
-
. "" string -
h. " "
"", .

-, :
using namespace std;

Visual C++ 6 ,
.
. ,
- . -
" std". -
-
. , -
,
.
,
-
std: :
std::cout

std: -.string

.

, - , using
, :
using std::cout;
using std::string;

.
5. 189



, , -

:
string , -
. string .
string 5.2.

5.2. string

tinclude <string> // string.


^include <iostream>
using namespace std; //
string ss(string s) //
{ g[0] = 'V; //
return s; //

string sss (string &s) //


{ s[0] ^ 'v'; return s;
int main(void)
( string si =" , s2, s3;
s2 = ss (si); // si
cout si ' s2 endl; // 11111 vllll
s2 = sss fsl) ; // si
cout si ' s2 endl; // vllll vllll
return 0;
}

,
. , -
, const -
. -
: string
string, .
- .
5.3.

[ 5.3. -
1.1.,'. ,..; j _.., :... , '. _;.;;..';

string hh{string s = " ")


{ cout s endl; return s; )
190 I. C++

-
(. . " " , 4). -
,

(. . " " . 2).
- -
string, (. -
4.9) string. " -
" charTooem string. -
, - ""
. , -
, string c_str(),
. -
5.4.

\ 5.4. string
i: ;. .-....".... .?: : :..:::.:: i J ;.,: i
char * Rus(const string sin, char out[])
{ if (CharToOem(in.c_str(), out)) return out; else return 0; }

,
:
string ' ! ! Hello!");
cout Rusfss, s) endl;

. ,

. , -
. -
Rus.h -
ttinciude .
include. Rus.h
5.5.

: 5.5. Rus.h

^include <windows.h>
ttinclude <string>
using namespace std;
char as[100]; //
char * Rus(const char in[], char out[]J
{ if (CharToOemi.in, out)) return out; else return 0; 1
char * Rus(const string Sin, char out )
{ if (CharToOsmfin.c str(), out)) return out; else return 0; }
5. 191

windows.h
string. ,
. , -
,
:
^include <Rus.h>
,
, . -
.


, :
. -
, . . string -
. -
1 999 999 999 -
int.

(_
, Visual C++ 6 -
int64.
long.

999 999 999 -


1 999 , -
. :
, , .
, 1
999 string. -
.
, . ""
"" "" "". -
. bool, -
false, true. -
5.6.

: 5.6. toNumeral

//
string toNumeral(unsigned long Number, bool Thousands)
{ string units[] =
{ "", "", "", "", "",
"", "", " ", ""
192 I. C++

string tens[] =
{ "", "", "", "", "",
"", "", "", ""
1;
string hundreds[] =
{ "", "", "", "", "",
"", "", "", ""
,.
string secondten[] =
{ "", "", "", "", ",
"", "", "", ""
I:
string Women[2] = "", "" };
string result(""); -
typedef unsigned char byte
byte digits[3] = { 0 }; //
unsigned long n = Number;
digits[0] = n % 10; //
digits[1] = (n / 10} % 10; //
digits [2] = n / 100; //
if (digits[2] > 0) result += (hundreds[digits[2] - 1] + " ");
if (digits[1] > 0}
{ if ((digits [1] === 1) && (digits [0] != 0))
{ result += (secondten[digits [0] 1] -f " "); return result,
else result += (tens[digits[1] - 1] + " ");

if (digits[0] > 0)
{ if (((digits[0] > 2} && (digits[l] != 1) f [Thousands) }
result += (units [digits [0] - 1] " ") ;
else result += (Women[digits[0] - 1] + "
}
return result;

: -
9 .
: digits[2], -
digits[1] digits[0]. ,
-. --
. , -
.
(): 1, .
, secondten
. 1, - -
5. ^^ 793

tens.
1 2 women -
.
, -
. (
5.7).

5.7. toNumerai

int main ( }
{ unsigned long Number [] =
{ 1, 2, 9, 10, 11, 19, 20, 21, 22, 45, 100,
101, 102, 109, 110, 118, 150, 870, 912, 999

int k = sizeof (Number) / sizeof (unsigned long) ;


char s[200] = { 0 };
for (int i - 0; i < k; ++i}
( CharToOemt toNumerai (Number [i] , true) . c__str ( } , s) ;
cout s endl;
}
return 0;

Number
.
. -
RUS (. 4.9, 5.4). -
. ,
. toNumerai string,
( ) c_str(). -
,
.
( 5.8),
. ,
toNumerai, "" -
.

I 5.8.

string Numeral(unsigned int Number)


{ string what_[3][3] = { { "", "", -"" },
( "", "", "" },
( "", "", "" } };

7 . 1264
194 I. C++

unsigned int t = Number;


unsigned int treads[3]; //
treads[0] - t % 1000;
treads[1] - t / 1000 % 1000;
treads [2] -= t / 1000000;
string result = "";
if (treads[2] > 0) { result += toNumeral(treads[2], false);
if (treads[2] / 10 % 10 =- 1) result += what_[0][2]; // 1
else
switch (treads[2] % 10)
{ case 0: case 5: case 6: case 1: case 8: case 9:
result +- what_[0][2]; break;
case 2: case 3: case 4: result += what_[0][1]; break;
case 1: result += what_[0][0]; break;
}
result +="';
)
if (treads[1] > 0) {result += toNumeral(treads[1], true);
if (treads[1] / 10 % 10 = 1) result += what_[l][2]; // 2
else switch (treads[1] % 10)
{ case 0: case 5: case 6:
case "7: case 8: case 9: result += what_Jl] [2]; break;
case 2: case 3: case 4: result += what_[l][1]; break;
case 1; result += what_Jl][0];break;
}
result += ' ';
}
result += toNumeral(treads[0], false);
if (treads[0] / 10 % 10 =* 1) result += what_[21[2]; // 3
else switch (treads[0] % 10)
{ case 0: case 5: case 6:
case 7: case 8: case 9; result += what_[2][2]; break;
case 2: case 3: case 4: result += what_[2][1]; break;
case 1: result += what_[2][0]; break;
1
result += ' ';
return result;
-

, . ,
, :
1, .
.
, -
5. 195

switch,
.
: 5.7 -
, , ,
charToOem:
CharToOem(Numeral(Number[i]) ,c str ( ) , sj;

,
, -
. . ,
,
"". , , ""
. , (. . 3)
. -
60 .
,
,
.
, -
-
, . string -
, .
. -
, , -
, .
: .
,
. , -
. , -
(vector) (list), (queue)
(priority^queue), (deque) (stack).

.
(. 5.1). ,
,
. ,
.
(set) -
(multiset ),
() (multimap ).
196 I.


Borland C++ Builder 6 STL (STLport 4.5.3) -
, slist hashjnap.

5,1.

vector deque list stack queue

push front + +
pop front + + +
push back + + . + + +
pop back + + + +
insert + + +
erase
[ ], at

. -
, -
, . -
, -
.
. w,
,
.
, :
cout [""] endl;

"" , w . C++ -
,
.


(. 1.20
1.21). . 2 (. 2.13)
, -
, .
, . .
. "" , -
.
5.

, , -
.
-
. ( )
. -
, :
^include <_>
using namespace std;

vector. , -
( -
5.9).

| 5.9.

vector <double> u; // 1
vector <int> v(10); // 2
vector <int> x(15, -1); // 3
vector <int> t(v.begin(), v.end()); // 4
int a[10] - { 1, 2, 3, 4, 5 };
vector <int> w(a, a + 10); // 5
int n;
cout "Input n: ";
cin n;
vector <int> w(n, 1); // 6
tt = w; // 1 tt = w
for( i - 0; i < tt.sized; ++i) cout tt[i] ' '; // 8
cout endl;
t.assign{20, -1);

, vector ,
(. . " ". 3),
.
: , .
1 "" ,
double.
push_back. , "" -
empty , true,
.
2 10- int,
.
15- ,
-1.
198 I. C++

4 , -
,
.
, string.
5 -
. ,
.
,
, , -
. , -
.
, -
. ,
. ,
-
. -
.
1. .
2. -, , .
3. .
4. .
assign .
.,
,
. ( 7) tt -
w. ,
.
, ,
.
, "
". .
: at, -
. , v[ij
v . a t ( i ) . s i z e ( ) ,
.
5.9
assign,
. ,
.
5. 799

. :
,
.
. 5.10.

j 5.10. , 1

^include <vector>
using namespace std;
double Average(const vector <double> &v)
{ double s = 0.0;
for (int i = 0, count = 0; i < v.sizeO; ++i)
s += v[i], ++count; /7
return s I count;

Average . ,
,
, -
.
, : -,
, ;
-, , . . -
. -
. -
, ,
.
:
^include <iostream>
tfinclude <Rua.h> , //
int main )
f vector <double> u; double el;
while (cout Rus(" : ", ss), cin el)
u.push_back(el); // ""
cout Average(u) endl;
return 0;
i

.
- -
. , -
"", -
:
200 _ I, C++

list <double> u; // deque


while (cout Rus (" : ", out), cin el)
u.push__front (el) ; // ""

,
, -
accumulate. :
double Average (const vector <double> &v)
( if (Iv.emptyO) return
(accumulate (v.beginO , v.end(), 0.0) / v.siaeO);
return 0;

,
, . -
:
((include <numeric>
: vector, list deque,
( [25]) ,
, vector list
deque, ^include.
5.11.
:'- ; ;
I 5.11. deque

^include <iostream>
tinclude <deque>
^include <numeric>
using namespace std;
tinclude <Rus.h> //
double Average(const deque <double> sv)
{ if (!v.empty())
return (accumulate (v.begin (), v,end(), 0.0) / v.sizeO);
else return 0;
]
int main()
{ deque <double> u; double el;
char out[100]; //
while (cout Rus(" : ", out), cin el)
u.push_back(el); // ""
cout Average!u) endl;
return 0,-
5. ^ 20?

, -
. , ,
, -
, -
. , ,
,

. , string.
, typedef, -
:
typedef vector <string> Question;
Question GetTest(const Question &Test, int n)

:
;
;
i -= 0;
(i < +1)
;
,
[];
++i;

m -
. ,
. , set , -
.
. -
. ,
, -
-. -
. 5.12.

\ 5.12.

typedef vector <string> Question;


Question GetTest (const Question STes.t, int n)
{ time_t t; //
srand((unsigned) t i m e l & t ) ) ; //
Question ConcreteTest; // -
Set <int> s; //
202 _ I. C++

int i = 0;
while (i < n)
{ int k = randQ % Test.size(); //
set <int> :: iterator ik = s.find(k); //
if (ik == s.end()) //
{ s. insert (k) ; //
ConcreteTest.pu5h_back(Test [k] ) ; //
++i; .
i
i
return ConcreteTest ; // -

, :
^include <set>

-
find, , , -.
, end . -
, .
, -
, :
Question w;
w.push_back( "") ;
\.__]("") ;
w . push_back ( "" ) ;
w.push_back ("") ,-
w.push_back("Maji") ;
w.push_back("raoHb") ;
w . push_back ( " " ) ;
w.push__back("aBrycT") ;
w.push_back("ceHTH6pb"} ;
w.push_back( "") ;
w,push_back{ "") ;
w.pu5h__back(" ") ;

:
Question tt = GetTest(w, 5) ;
for (int mm = 0; mm < t t . s i z e O ; +4) cout Rus {tt [ton] , as) endl;

12 , :

5. 203

, , , . .
. ,
,
.



. , ,
, :
vector <void *> v;
,
-, ;
vector <int> *m = new vector <int>;
, ,
, , :
typedef vector <double> row;
row *m = new row(10, 2.1);
10
2.1. :
cout (*m)[i] endl;

:
cout m -> a t ( i ) endl;

.
, , ->:
row *mm = new row{10, 2); //
mm -> push_back(l); // 1
cout mm -> size () endl; // 1
cout mm -> capacity(} endl; // 2
vector <double> :: iterator k;
for (k = mm -> begin(); k < mm -> end(); ++k) cout setw{4) *k;
cout endl;

i (
11), 2
204 _ /. ++_

( Visual C++ 6 20). k, -


.


(. . 3) , -
-
, ,
.
STL . -
( ) -
, .
, , ,
(
, - . -
4.1), .
-,
( ) ,
, .

( ^
-
.
. "" "-
".

, -
. ,
, -
, . . .
, , -
- list
double,
. (
5.13), -
,

| 5.13.

^include <iostream>
Itinclude <algorithm>
^include <list>
^include <cstdlib>
^include <ctirae>
5. 205

using namespace std;


ttinclude <Rus.h>
int main ( )
{ double muflQ];
time_t t; // 1
srand((unsigned) time(St)); // 2
for (i =0; i < 10; ++i) mu[i] = (rand() % 100); // 3
list <double> L(mu, mu + 10); // 4
list <double> :: iterator il; // 5
list <double> :: reverse_iterator ril; // 6
for (il = L.beginf); il != L.endO; +-H.1) cout *il ' ';
ccut endl;
for (ril = L.rbegin(); ril != L.rend{); -f+ril) cout *ril ' ';
cout endl;
return 0;

:
77 59 24 23 3 6 97 45 10 96
96 10 45 97 3 23 24 59 77

L. -
, , -
. mu, -
. ,
5.12, srand,
time ( ctime).
1 1970 ,
IBM PC. rand.
srand rand CStdlib.
4 L,
: ( 5) ( ).
"" -
, .
.

begin ( } . -
rbegin().
rend{).
-
. : -
, -
(. 5.1).
206 I. C++

()
begin() > > end()

i
i i
i i

rend() < < rbegin()



. 5.1.

.
[first, last)

, last -
-
last - 1. [first, last) first
last, -
. : -
.
, ,
:
(
cout *il '; cout *ril ' ';

. 4 ,
. ,
, , : ,
, . -
-. , -
.
. -,
.
, const (,
). -, STL 5
: (input), (output), (forward), -
(bidirectional) (random access). -
, -
.
,
. -
(i j ):
5. 207

1 =
i = j
i != j

, , -
. 5.2.

5.2.

= *i

*i -

= *i, *i =

= *i, *i = x, i, i

x = *i, *i = x, i, i , , list
i + n, i n, i += n, i -= n,
i < j, I > j, i <= j, i > j

, ,
: distance, ,
advance, i += n.
distance :

template <class Forwardlterator, class Distance>


void distance(Forwardlterator first, Forwardlterator last, Distance & n ) ;

n difference_type, -
last - first. , ,
unsigned int.

(reverse) . ,
, , .
:
back_insert_iterator back_inserter
( );
front_insert_iterator front_
inserter ( );
insert_iterator inserter (
).
208 ____^ I. C++

:
template <class Container>
back__insert_iterator<Container> back_inserter(Containers x)

template <class Container>


front_insert_iterator<Container> front_inserter(Containers x)

.
:
template <class Container, class Iterator>
insert_iterator<Container> inserter(Containers x, Iterator i);


, , .
. ,
, .
,
. -
:
vector<int> v(a, +10);
list<int> L;
copy (v. begin () , v.endO, inserter{L, L.begin()));

L , -
:
(V.begin(), V . e n d ( ) , front_inserter(L));

, -
/, . -
Borland C++ Builder 6 -
:
ostream_iterator<int, char> out(cout, " "};
copy(vl.begin(),vl.end{},out);

out,
.
-
, .
cout , .
, -
:
fill n(ostream iterator<int,char>(cout," "), 3, 5);
5. 209

.
fill_n(ostream_iterator<char,char>(cout, ' - ' ) , 30, ' - ' ) ;

"" 60 .
Borland C++ Builder 6 ( -
5.14), . -
, -
.

| 5.14.

^include <iterator> //
^include <numeric>
ttinclude <deque>
^include <iostream>
using namespace std;
int main()
( int arr[4] = { 3, 4, 1, 3 };
int total = 0;
deque <int> d(arr + 0, arr + 4);
//
copy (d.beginO, d.end(} 1,
ostream__iterator <int, char> (cout, " + "));
cout *(d.end() - 1) " = "
accumulate (d.beginO , d.end(}, total) endl;
return 0;
}

(
) ( -
).
:
;
end();
, ,
.
:
#include <iterator>


:
G ,
;
210 I. C++

, , -
, ;
;
, , ,
"" .
,
. , - sort,
, sort. -
: -, -
, -
, ; -,
.
, -
(. 5.3).

5.3.

find

find_first_of -

find end

find if , -

search

search n n-

count -

count if -
,
equal
lower__bound -

upper_bound -

5. 211

5.3 ()

binary_search -

equal range -
,

sort
partial_sort
merge

.
. -
, 5.15.

I 5.15.

^include <iostream>
#include <algorithm>
#include <list>
^include <cstdlib>
^include <ctime>
using namespace std;
^include <Rus.h>
int main()
( double elem; int i; const int nn = 20; double mu[nn];
//
time_t t; srand ((unsigned) time(St));
for (i - 0; i < nn; ++i) mu[i] = (rand() % 1000);
list <double> u(mu, mu + nn); //
list <ciouble> : : iterator iu; //
//
for (iu = u.beginO; iu != u.end{); ++iu) cout *iu ' ';
cout endl;
elem = *min_element(u.begin(), u.end(j); // 1
cout elem endl;
cout *max_element(u.begin(), u.end()) endl; // 2
cout *min__elernent (mu, mu + nn) endl; // 3
return 0;
i
212 /. C++


mu, , -
. 1 2 -
. -
, -
, 3.
, . . .
, .
-
, :
. 5.15 :
*min__element (u.begin ( ) , u . e n d ( ) } = 400;
*max_element (mu, mu + rm) = 200;

400,
200.
, . ,
C++ -- -
. , . 7.
,
. , -
,
. ,
, ,
( 5.16). ,
.

5.16.

bool i s P r i m e f i n t number) //
{ number = abs(number); //
// 0 1
if (number == 0 | | number == I) return false;
int divisor; // ,
for (divisor = number / 2; number%divisor != 0; divisor);
return divisor == 1; // 1,
}
int main()
{ list<int> L; //
list<int> :: iterator pL; //
// 20 40
for (int i = 20; i <= 4 0 ; +4-i) L.push b a c k ( i ) ;
5. _ 273

for (pL = L. begin (); pL != L.endO; -f+pL) cout setw(3) *pL;


cout endl;
pL find(L. begin () , L.endO, 31); /* 31 */
/* <4 31 */
reverse (pL, L.end() ) ;
for (pL = L. begin (); pL != L.endO; ++pL) cout setw(3) *pL;
cout endl; .
list<int> :: iterator pos;
pos = find_if (L. begin (} , L.endO isPrime) ; //
if (pos != L.endO) cout *pos " " endl;
else cout " " endl; //
//
cout count_if {L. begin 0 , L.endO, isPrime) endl;
L.sortO; //
for (pL = L. begin 0; pL != L,end(); ++pL) cout setw(3) *pL;
cout endl;
return 0;

, ,
20 40. -
:
cout setw(3) *pL;

,
L 3 .
, :
#include <iomanip>

,
.
31 ""
, 31. :
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

:
20 21 22 23 24 25 26 27 28 29 30 40 39 38 37 36 35 34 33 32 31

, 5.13,
. -
reverse,
.
214 I. C++

- isPrime, .

= find_if(L.begin() , L . e n d { ) , isPrime);

. -
23. , -
L.endo . -
find_if .
find_if -
, , -
, ,
() . , . . -
, :
* f i n d _ i f ( L . b e g i n ( ) , L . e n d ( ) , isPrime} = 0;

.

count_if.
cout count_if (L.begin ( ) , L . e n d O , isPrime) endl;

,
.
STL.

, . -
, -
.
, sort -
, .
.

. ,
( -
5.17).

1 5.17,

double mu[10], nu[10];


time_t t; srand((unsigned) time(&t));
for (i = 0; i < 10; ++i) mu[i] = (rand() % 100);
for (i = 0; i < 10; ++i) cout mu[i] ' '; cout endl;
for (i - 0; i < 10; ++i) nu[i] - (rand() % 100);
for (i = 0; i < 10; ++i) cout setw(3) nu[i]; cout endl;
5. 215

sort(rnu, mu + 10); //
sort(nu, nu + 10); //
double ru[20];
merge(mu, mu + 10, nu, nu + 10, ru); // 4
for (i = 0; i < 20; -h+i) cout setw(3) ru[i]; cout endl;

, , .
( 2 )
( 4). -
:
56 5 83 12 33 39 63 37 91 83 //
27 0 0 83 97 0 16 0 37 67 // nu
5 12 16 27 33 37 37 39 56 67 68 83 83 83 91 97 // ru

sort , , -
(), -
. -
. ,
, , " ",
, .


! 5,17 ru. -
mu nu.
.
, unique:
unique(ru, ru-+ 20);
for (i =0; i < 20; ++i) cout setw(3) ru[i]; cout endl;

,
:
6 23 34 36 43 58 59 73 73 78 80 83 89 90 91
23 34 36 43 56 59 73 78 80 83 89 90 91 80 83 89 90 91

.
ru , -
unique. -
:
vector<double> V(ru, ru + 20);
unique(V.begin{), V.end());
for (i = 0; i < V.sized; ++i) cout setw(3) V[i]; cout endl;
216 I. C++

, ,
, .
remove -
: "" remove, remove__copy, -
remove_if remove_copy__if.
, unique, -
. -
erase, . -
v:
vector<double> V(ru, ru + 20};
remove(ru, ru + 20, 0) ;
for (i = 0; i < 20; ++i) cout setw(3) ru[i]; cout endl;
vector<double> :: iterator pos = remove_if{V.begin(), V.endl), isPrime);
for (i = 0; i < V.size{); --i) cout setw(3) V[i]; cout endl;
V.erase (pos, V.endO ) ;
for (i = 0; i < V.size!); ++i) cout setw(3) V[i]; cout endl;

ru :
1 14 15 26 33 37 38 47 61 62 65 68 69 85

remove :
7 14 15 26 33 37 38 47 61 62 65 68 69 85 61 62 65 68 69 85
, , ,
"" .
remove . v remove_if
:
14 15 26 33 38 62 65 68 69 85 65 68 69 85

. erase -
, :
14 15 26 33 38 62 65 68 69 85

, unique, remove, copy, reverse, -


, .
STL .
. 5.4.

5.4. ,


,
copy_backward ,
5. 217

5.4 ()


fill
f ill_n
generate
generate_n
remove ""
remove_if "" ,
replace
replace_if ,
transform -

unique ""
unique_copy ""

reverse
reverse_copy

,
, -
( ) . ,
. -
:
STL *, -
. -
"" . -
. 5.5
, .

5.5.

plus<T> +

minus<T> -

, .
218 I. C++

5.5 ()

multiplies<T> x *
divide s<T> x /
modulus <T> x %
negate<T> - X
equal__to<T> ==
not_equal_to<T> !=
greater<T> >
less<T> <
great er_equal<T> >=
less equal <T> <=
logical and &&
logical or X | I
logical not !

. -
:
. -
:
bool compare(int x, int ) { return x > ; }
v :
. sort (V.begin () , V . e n d f ) , compare) ;
, . .
greater<T>.
:
sort(V.begin f ) , V . e n d ( ) , greater<int>());

, -
:
^include <functional>
. -
accumulate -
(. 5.11). -
rnasa5L 219

, , , ,
:
vector<double> v; // -
double = accumulate (v.begin () , v.endO, 1.0, multiplies<double> {) ) ;

,
. , -
, STL ,
generate. ,
:
double f ( ) { return ( r a n d f ) % 100); }

:
double mu[10], nu[10]; time_j: t; srand((unsigned) time(St));
generate(mu, mu + 10, f) ;

transform,
, -
. ,
: ,
, ,
. -
:
trans-form(mu, mu + 10, mu, negate<double>());
fill(, + 10, 1); //
transform(mu, mu + 10, nu, nu, plus<double>());

mu (
transform), ,
( transform), -
. transform . -
, nu 2:
double f2(double a) { return *2; }
transform(nu, nu + 10, nu, 2);

, -
(- !) .
, -
:
double ff(double a, double b) { return a*2-b;}
fill(nu, nu + 10, 1); //
transformfmu, mu + 10, nu, nu, ff);
220 /. C++

mu :
30 91 94 92 66 54 56 56 31 95

:
59 181 187 163 131 107 111 111 61 189

: -
, . -
- !
6

C++


,
. , ,
. -
.
, .
/,
. / ,
. , /
,
. ,
, -
.
/ -
- -. ,
, ,
, UNIX. -
, / -
. stdio ( -
/) C++. -,
/-
. , -
. -
^ . ,
.
C++ -
/ iostream. ,
/ C++. stdio
222 I. C++

, iostream
.
,
, .
" " .
. ,
[29] / Visual C++ 6,
. .
[37, . 705]: " , , -
". -
[37] Borland C++ Builder 6
Visual C++ 6 STLport 4.5.3.

C++
/ C++ , / .
,
: , .
.
, , . -
string. -
. -
. <iostream> / -
:
cin ; ;
cout ; ;
clog ; ;
-=- ; .
cout "" ,
clog .
, cout:
" !" endl;
cleg " I" endl;

"" "-
" . - ,
cout , clog, - -
"" cout,
clog.
-
, . . [37,
6. C++ 223

. 671] ; ", -
, ...
". , , -
. -
cin -
(, int double ,
). -

cout.
(
, 3.12).


. ,
31 12.78.
:
int ;
double b;
cin b;
:
31<enter>
12.78<enter>

:
31<npo6en>12.?8<enter>
, <Enter>.
:
cn a;
cin b;

,
, <Enter> ,
, .
-
, , :
31rl2.78<enter>

31 , . -
, -
, . ,
. -
224 /^ C++

- , ,
. -
, , . [37, . 681],
( 6.1).

6.1.

int read__double (vector <double> sv)


( int i = 0; while (i < v. sized && cin v[i] ) i++; return i; }

,
( 6.2).

I 6.2.

int read_double (vector <double> &v)


{ istream__iterator<double>is (cin) ; //
istreara_iterator<double>eof ; //
copyfis, eof, back^inserter (v) ) ;
return v.sizeO ;

copy : -
,
. eof ""
"end of file" . , -
, dd. ,
<Ctrl>+<Z>, <Enter>. -
, 5 ,
:
1<>2<>3<>4<>5<><^1>+<2><^>

, <Enter>.
,
- ,
. :
<><><>
:
int a;
char ch;
double b; -1
6. C++ 225


3112.78

:
cin ch ;

( . . ), -
ch.
<Enter>. -
:
char ch;
for (int i = 0; i < 10; ++i) cin ch;

<Enter> , ,
. 10- -
-, <Enter>
10 . ,
:
for (int i = 0; i < 10; +->-i) { cin >> ch; cout ch; }

: -
-, -
, ( <>)
, .
(
, )
, . , ,
, -
, , ,
( )
.
, .
/ . 6.1.
/ :
-
/;
_
3 .
.
,
.


. 3 , -
.

8 . 1204
I, C++

. 6.1. /

string. -
, -
.
.

/ -: read, get
getiine. read . -
, , , -
. - get. -
. 1 ,

.
char s[100] - { 0 } ;

, .
:
cin.get(s, 1001;

, , <Enter>,
' \ ' , -
6. C++ 227

. -
( ), '\'
. ignore. ,
s :
cin.get(s, 100);
cin.ignore{);

' \ ,
, * ; ' .
cin.get(s, 100, ' ; ' ) ;
cin.ignore();

,
<Enter>. , get . -
getiine get,
- ,
ignore . -
getiine :
cin.getiine(sr 100);
cin.getiine(s, 100, ';');

,
. ,
.
string getiine, -
:
string s;
getline(cin, s); //

, , . . -
cin, , , ,
. , ,
.


,
, -
. "" . -
( )
: ,
, .
228 I. C++


. ,
. -
.
! ,
.
, .
, -
,
. -
:
;
;
(/);
;
.
.
,
" " . -

- , .
,
.


""
.
,
, . , .

- . -
, -
.

Windows, ,

C++. , -
.
C++ -.
,
6. C++ 229

- . string .
:
>, <, :, ", |;
,
;
, .
file, -
, FILE, File.
, , -
. , -
"". ( ) .
. , -
. Windows
MAX PATH.


Borland C++ 3.1 ( )
. 8 . MSDOS
Windows 3.1. Windows (95, 98, 2000, .)
8 .

A Z ( a z) .
.
-. Windows
\ ( backslash). C++
\\. , -
/.
. "." () ".." ()
.

.
C++ ,
API
Windows. API Windows,
Microsoft ,
API.
Windows XP, Windows NT, -
API Windows NT. -
, GetcurrentDirectory (
6.3).
230 I. C++

( 6.3.

DWORD GetCurrentDirectory (
DWORD nBufferLength //
LPTSTR IpBuffer //

:
nBufferLength , -
;
IpBuffer , .
,
-, IpBuffer. -
. , .
, (
6.4). -
,
Rus.h (. 5.4).
; >'- ; ;

6.4.

^include <iostream>
^include <Rus.h>
int main ( )
( char pp[MAX_PATH] ; long cc - MAX_PATH;
DWORD t = GetCurrentDirectory (cc, pp) ;
cout Rus (pp, pp} endl;
return 0 ;

, windows. h
C++ (_)
(DWORD), Windows.
,
SetcurrentDirectory, -
6.5.

| 6.5. SetCurrentDirectory

BOOL SetcurrentDirectory(
LPCTSTR IpPathName //
6. C++ 231

ippathName
.
( 6.6),
,
, .

) 6.6.

tinclude <io5tream>
tinclude <Rus.h>
int mainO
{ char pp[MAX_PATH], tt [MAX_PATH] ;
long cc - MAX_PATH;
DWORD t = GetCurrentDirectory (cc, pp) ; //
cout Rusfpp, tt) endl; //
SetCurrentDirectory ("a: ") ; //
t = GetCurrentDirectory {cc, tt) ; //
cout Rusftt, tt) endl;
SetCurrentDirectory (pp) ; //
t = GetCurrentDirectory (cc, pp) ; //
cout Rus(pp, pp) endl;

" ",
.
,
tt. ,
SetCurrentDirectory .
, .

,
: ,
C++. .
, "" ,
, 10 :
" C++?"
" C++?"
" "
" "
232 I. C++

" "
" "
" "
" "
" C++?"
" "

question.txt, -
tests :.
. , -
. -
, , ( 6.7).

6.7.

^include <fstream> // 1
ffinclude <iostream>
^include <string>
using namespace std;
int main()
( ofstream tof'c:\\tests\\question.txt"); //
if (!to.is_open()) // 2
{ cout "Error! Not openfile. "endl; return 1; }
string s;
while(getline(cin, s) ) // <Ctrl>+<Z>
{ to 5 endl; } / / 1
to.closed ; // 3
return 0;

, 1
fstream.
, . :
ofstream to(":\\tests\\question.txt");

to, -
. ,
. 2
is_open , .
, .
, . . .
, .
, . -
6. C++ 233

. ,
:
:/tests/question.txt

( ),
. .
(, , ),
, "". ,
:
ofstream to; .
to.open(":\\tests\\question.txt");

, -
-
. ,
, , -
. -
.
: -
(, ), (-
, ). -
. , ,
.
. -
, get line". ,
, <Ctrl>+<Z>.
, -
cout:

to s endl;

endl ,

.
. , -
, 6.8.

6.8.

^include <fstream>
#include <iostream>
^include <string>
using namespace std;
int m a i n ( )
234 _ ___ _ I. C++

{ ifstream from("c:/tests/question. txt") ; // 1


if ( ! from.is_ppen() ) { cout "Error! Not openfile." endl; return 1;
}
while (getline (from, s) ) cout s endl; // 2
return 0;

1 from,
, (. 6.7)
. , . .
.
: ,
, :
SetCurrentDirectory (": /tests/"} ;
ifstream from ("question. txt") ;

2 . -
-
(. 6.7), ,
, . -
, <Ctrt>+<Z>.
, - .


-
. / C++ ,
[37, . 683]:
bool good () const; //
bool eof () const; //
bool fail () const; //
bool bad{) const; //

ios_base :
typedef int iostate;
enum io_state ( goodbit = 0x00,
badbit = 0x01,
eofbit = 0x02,
failbit = 0x04
J;

, ,
, . . -
10.
6. C++ 235


good, : -
-
/.
. eof
.
<Ctrl>+<Z>. question.txt -
. getiine, ,
. , -
:
while { ! f r o m . e o f ( ) ) //
{ getiine (from, s) ; cout s encil; }
fail bad .
, .
bad fail: -
bad, fail; .
bad
fail, , .
bad, .
, , (). -
fail
clear ( ) :
if (stream, f a i l ( ) ) stream, clear ( ) ;


.
ios_base
iostate _M_iostate;

-
. :
iostate rdstate();

:
void setstate(iostate flag);


. -
(. . "" ) -
5 , .
:
236 /. C++


"1", "2", "5", "8 , "10"

"1", "2", "5", "3 , "7"
"al", "_bbb", "_5_", "8_d", "abcdefghlO"
"1.0", "lei", "1000000L", "10e-5f","-le-1"
"for", "const", "if", "switch", "cout"
"for", "main", "cout", "function", "procedure"
"cin", "cstream", "cout", "clog", "cerr"
"A", "L", "u", "F", "f"
"1", "3", "2", "4", "5"
"return", "goto", "continue", "break", "case"

: 4, 4, 4, 3, 5, 2, 2, 1, 2, 4.
.
,
.
. , , ,
.
. -
, . -
, $ @,
C++. -
getiine ( , string) -
. -
. , -
, , :
C++?
1@2@5@8@10@4


string. ,
:
, -
1 5. , . . -
. ,
( 6.9).

6.9.

^include <fstream>
ttinclude <iostream>
using namespace std;
^include <Rus.h>
hool isRight(char digit)
g. C++ 237

{ return (('I' <= digit) && (digit <= '5')); }


int main ()
{ char ssflOOj; string s;
ofstream to ("c:/tests/question.txt");
if (!to.is_open())
{ cout Rus(" !", ss) endl; return 1; }
while {cout Rus(" !", ss)
endl, getline(cin, s))
{ to s endl;
variants: // ,
cout Rus(" (1@2@@4@5@):", ss);
getline(cin, s);
if (count (s .begin () , s.end() , ' @ '} != 5)
{ cout Rus(" 5 !",ss) endl;
goto variants;

if ( !isRight(s[s.size(} -1]))
{ cout Rus (" !", ss) ;
goto variants;
};
to s endl;
1
to. close () ;
return 0;

,
question.txt.
( ) -
. , -
variants, .
tests question.txt -
:
C++?

C++?
1@2@5@3@7@4

al@_bbb@_5_@ 8_d@ abcde f ghl 0 @ 4

l.O@leieiOOOOQOL@10e-5f@-le-l@3

for@const@if @switch@cout@5
235 _ I. C++


for@main@cout@function@procedure@2

cin@c5tream@cout@clogScerr@2

C++?
1@3@2@4@5@2

return@goto@continue@break@case@4


,
, -
.
: I .
,
: 5, 2;
5 , 3; 7
8 , 5 ,
. ,
:


; ;
=
< 5 ""
= 5 = 6 ""
= 7 = 8 ""
= 9 = 10 ""
,
. -
,
:
struct Questions
{ string question; //
string answer [5]; //
char right; //

STL, :
vector <Questions> Q;
6. C++ 239


, . . .
,
. answer
.
( 6.10), -
. ,
,
.

6.10.
void ReadQuestion(vector <Questicns> &Q)
{ Questions trnp;
char s [ 5 ] [ 3 0 ] ; //
ifstream f r o m ( " c : / t e s t s / q u e s t i o n . t x t " ] ;
while ( ! f r o m . e o f { ) )
{ getline(from, tmp.question); //
for (int i - 0; i < 5; ++i)
{ from.getline(s[i], 30, ' @ ' ) ; //
tmp.answer[i].assignfs[i]); //
}
from.getline(tmp.right, 2, r\n'); // A
Q.push_back(tmp); //
)
I

,
/, "" .
:
. . from ,
;
G
' \ ' (" "), getline.
right
Questions char right [2], . . ;
-
, . . -
.

)
-*
, / Visual C++ 6.
, . .
/ , -
"".
240 . I. C++

( -
6.11).

! .11, !
;.;, ,

int main ()
{ char ss[100}; //
vector <Questions> v;
ReadQuestion(v); //
unsigned int i = 0, summa = 0;
int j; char a[2];
while (i < v.size ()}
{ cout i + 1 ". " v[i].question endl;
for (j = 0; j < 5; ++j)
cout ' ' j+1 "}" v[i].answer[j] endl;
cout Rus (" :__", ss) ; cin. get line (a, 2, ' \n ') ;
if {a[0] == v[ij.right[0]) ++summa;

cout Rus (" = ", ss) summa;


cout Rus(" 10.", ss) endl;
if {surnma < 5) cout Rus [ "", ss) endl;
if (summa^S I I summa) cout Rus (", ss) endl
if (5==7 ! | surnma==8) cout Rus ("! ", ss) endl;
if (surraria==9 I I surrnia==10) cout Rus (" ! !", ss) endl;
return 0;

,
. ,
.
,
cout, -
.


.
,
. . -
, ,
, .
,
.
6. C++ 241

1. ?
2. ?
3. ?
.
.
entii, 5.23
setw.
, ,
boolalpha. .
/
ios_base :
typedef int fratflags;
enum{
left = 0x0001, //
right - 0x0002, //
internal = 0x0004, // ,
dec = 0x0008, //
hex = 0x0010, //
oct = 0x0020, //
fixed = 0x0040, // dddd.dd
scientific = 0x0080, //
boolalpha = 0x0100, // true false
showbase = 0x0200, // oct hex
showpoint = 0x0400, //
showpos = 0x0800, // +
skipws - OxlOOO, // -
unitbuf = 0x2000, //
uppercase = 0x4000 // X

, :
adjustfield - left I right | internal //
basefield = dec [ hex | oct //
f l o a t f i e l d = scientific I fixed //

/.
ios_base :
fmtflags M fratflags; //

-
.
-:
242 10 C++

fmtflags flagst) const;


fmtflags flags{fmtflags flags);
fmt flags setf(fmt flags flag);
fmtflags setf{fmtflags flag, fmtflags mask);
void unsetf(fmtflags mask);

, :
streamsize precision{) const;
streamsize precision(streamsize newprecision);

:
streamsize width(} const;
streamsize width(streamsize newwidth);

, iomanip :
resetiosflags (ios_base: : fmtf lags mask) ;
setiosflags(ics_base::fmtflags flag);
setprecision(int n ) ;
setwtint n ) ;

basic_ios :
char_type f i l l { ) const;
char_type fill(char_type f i l l ) ;

- ( ).



. , -
. , -
-
, , 10 15. (. -
5.12). ,
.
, test.ini. -
: ,
.
() .
,
/ -
.
, , . , , -
! C++ _ 243

, "". ()
, -
,
. -
,
.



, .

-
. -
. -
. -
,
, . -
.
""
-. , ( -
)
:
1. -.
2. .
3. . .
4. -.
5. . -
. -
.
6. . .
7. . .
8. . .
,
:
-
, , -
, -
. . -. , ,
, -
:
244 _ /. C++

const int n = 5;
string mm[n]= { "0. ",
"1. ",
"2 . ",
"3. ",
"4. ",

, "", -
. ""
. C++
,
cout.
. ,
. :
25 \:
void ClearScreen (void)
{ cout "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\" flush; }

flush -
. (
6.12). - ,
.
. 6.2.

! 6.1 2, -

int Menu(vector<string>mm)
( ClearScreen () ; //
int k = -1; char ss[100]; string blank{29,' ');
for {int i = 0; i < ran. size () ; ++i)
cout Rus (blank + mm[i] , ss) endl;
cout endl endl flush;
cout Rus (blank + " :_", ss) ;
cin k;
return k;
1

-
29 . ,
6.13. -
.
6. C++ 245

. 6.2.

6.13. -

int main()
{ const int n = 5;
string mm[n]= { "0. ",
"i. ",
"2. ",
"3. ",
"4. ",

vector<string>vMenu(ram, mm + n); int k


while(k)
( k = Menu(vMenu);
switch(k)
{ case 0: break; /I
case 1: testing!); break; //
case 2: setOptions(); break; // -
case 3: writeQuestions(); break; //
case 1: lookQuestions(); break; //
246 _ _ __ I. C++

default: cout " !"

return 0;
J

, . , -
.
. -
, , :
void setOptions { cout "setOptions ( ) ; " endl; }
.

()
(. 6.9) "-
" .
question.txt, , . -
, , -
. . - -
: , -
, /
.
, C++
, . -
.
, ,
. ,
question.txt -
(. 6.9) -
:
of stream to (": /tests/question. txt", ios_base: :app) ;

.
ios__base /,
. -
, . .
[37, . 705] "" -
. ,
C++ Builder 6 ios_base -
( 6.14).
6. C++ 247

| 6.14.

typedef int openmode;


enum openjnode ( app = 0x01,
binary = 0x02,
in = 0x04,
out OxOS,
trunc = 0x10,
ate - 0x20

"", , -
, :
in .
if stream;
out . -
ofstream;
. -
ofstream, fstream;
trunc . , -
; -
ofstream;
ate .
, , -
;
binary .
. , -
. , ios.h Visual C++ -
iostream.h Borland C++ 5 :
CJ nocreate -- , ,
;
ncrepiace , ; -
, .
. -
binary.
.


, , -
:
248 I. C++

D , -
"new line" ( ), /
C++ * \ ' ;

( atoi . 3.12);
.
, , .
, , /
.
,
. , , ,
sizeof(int) .
, -
. , 1 1 ,
55 555 5 . -
( ). ,
, 1000
. , -
.
: fstream, ifstream, ofstream -
/ . -
, -
. read/write char *. -
,
. , long:
from , to .
6.15.

' 6.15. /

long a;
from.read((char *)&a, sizeof(long)>;
to.write((char *)&a, sizeof(long));

C++:
to.write(reinterpret_cast<char *> (&), sizeof(long));
from.read(reinterpret_cast<char *>(&a), sizeof(long));

. -
, Date :
to.write(reinterpret_cast<char *> (&), sizeof(Date));
from.read(reinterpret cast<char *>{&a), sizeof(Date));
6. C++ 249

1000
, - ,
.
, .
6.16.

! 6.16. 1000
U...;.; :' ;<.-. ; .- ,
int main ()
( of stream toBinf "c: /tests/number. bin", ios :: binary) ;
time_t t; srandf (unsigned) time(St) ) ;
for (int i = 0; i < 1000; ++i)
{ int t = rand f ) ;
toBin, write (reinterpret_cast<char *> (&t) , sizeof (int) ) ;
I
toBin. closet) ;
of stream toTxt {"c: /tests /number. txt") ;
srand{ (unsigned) time (&t) ) ;
for (i - 0; i < 1000; ++i) tcTxt randf) endl;
toTxt. close () ;
return 0;

"" ,
, , number.bin
4000 ( * 4). number.txt
.
, , Notepad. -

.


/ . , -
,
. number.bin, , , -
, .
k- k * sizeof ( ) .
/
, . ()
. / -
, -
.
istream -:
pos type t e l l g f ) ;
250 I. _++

ostream, :
pos_type t e l l p f ) ;

,
seekg{pos_type)H seekp(pos_type) ,
. ( ):
s e e k ( o f f s , pos)

pos ,
ios_base :
static const seekdir beg, //
cur, //
end; //

, ' offs.
,
, , . -
, : -
, sizeof ( ) -
.
-


. , -
, ,
.
[41, 45].
,
, . -
:

1
1

, '
:
A
ch - const;

-
. , , 0x55, -
010101, ,
10101010.
6. C++ 251

,
6.17. -

.

6.17.

char Code(char ch) { ch A = OxAA; return ch; }


char Decode(char ch) { ch = OxAA; return ch; }

-
:
char Code(char ch)
{ char L = ch & OxfO; char R = ch & OxOf; return ( (L 4}|(R 4)); }
char Decode{char ch)
{ char L = ch & OxfO; char R = ch & OxOf; return ((L 4) j (R 4)); )

,
. .
.
. ,
, . . .
, -
. /
read/write get/put. -
. . ""
. ,
, . .
.
#. -
. , .
, -
, -
(
), . ,
6.18.

6.18.

void writeString(ofstream to, const string &s)


{ fortunsigned int i = 0; i < s.length(); ++i) to.put(Code(s[i])); }
void WriteQuestions(void)
{ char ss [100],- string s;
252 /^

ofstream to(":/tests/question.bin", ios_base::app|ios_base::binary);


if (!to) { cout Rus(" !", ss) endi; return; }
while(cout Rus(" !", ss) endl, getlinefcin, s))
{ writeString{to, s);
variants:
cout Rus (" (12@@4@5@: ", ss) ;
getline(cinr s);
if (ccunr (s.begin(),s.end(), '@'} 1=5)
( cout Rus{" 5 !", ss) endl;
goto variants;
};
if (lisRight(s[s.size() -1]))
{ cout <:< RusC'Bbi !", ss)
endl;
goto variants;
};
writeStringfto, s) ;
)
to. close ();

, -
.
: ? -

(. 6.8). -
-:
char Code(char ch) { return ch; }

, .
"" . ,
,
. ,
. .
.
6.19.

16.19.

void ReadQuestion(vector <Questions> &Q)


{ Questions tmp; char ch; string s;
ifstream from("c:/tests/question.bin");
if ( ! f r o m ) cout "Error! Not openfile." endl;
6. C++ 253
from. get (ch) ; //
while ( ! frcm.eof 1) }
{ f rom.putback (ch) ; //
5 = "";
while (ch = from. get f)) //
{ ch = Decode (ch) ; //
if (ch == '#') break; // ?
s += ch; //
};
trap. quest ion=s ; //
for (int i - 0; i < 5; ++i] //
{ s = "";
while (ch = from, get ())
{ ch = Decode (ch) ; //
if (ch == '(?') break; // ?
s += ch; //

tmp. answer [i] = s; //

ch = from. get () ; //
ch = Decode (ch) ; //
tmp. right = ch; //
Q.push^back (trap) ; //
from. get (ch) ; //

: -
, ?
eofbit:
, -
. , eofbit -
- .
,
, .
, -
, . . .
,
, .
, get,
. ,
:
254 I. C++

while (from.get(ch)) //
{ s = ""; s += Decode(ch); //
while (from.get(ch)) //
( ch = Decode(ch); if (ch == '#') break; s += ch; };
tmp.question = s;
for tint i = 0; i < 5; ++i)
{ 3 - "";
while (from, get(ch)) //
{ ch = Decode[ch); if {ch == '@') break; s += ch; )
tmp.answer[i] = s;.
}
from, get (ch) ; // ^
tmp.right = Decode(ch);
Q.push_back(tmp);
}

, .
.


, -
6.20. :
. -
,
. ,
1015 . -
, , 10 .

; 6.20.

void writelnteger (of stream^ to, const, int &n)


{ to.write(reinterpret_cast<char*>(&n) , sizeof ( i n t ) ) ; }
int readlnteger(ofstreams from)
{ int n; from, read (reinterpret__cast<char*> ( & n ) , sizeof {int) ) ; return n; }
void setOptionsO
{ ofstream f ( " c : / t e s t s / t e s t . i n i " , i o s _ b a s e : i b i n a r y ) ;
cout " :_";
int n;
cin n;
if (n < 10) { cout " < 10." endl;
cout " 10." endl;
n = 10;
6. C++ _ _ 255

writelnteger (f , n) ;
}
int getOptions (void)
j ifstream f ("c: /tests/test . ini", ios_base: :binary) ;
int n = readlnteger (f } ; return n;

setoptions -
-, getOptions .
. -
10 , , . -
-
.
.
-
( 6.21).

6.21.

void What(int n, int summa)


{ char ss[100]f
cout Rus(" = ", ss) summa;
cout Rus(" ", ss) n endl;
if (summa <= n*0.5) cout Rus(",ss};
if ((n*0.5 < surma) SS (summa <= n*0.7))
cout Rus("", ss);
if ((r.*0.7 < summa) SS (summa < n=*0.9})
cout Rus("!", ss);
if (n*0.9 < summa) cout Rus("!!!", ss) endl endl;
}

( 6.22).

6.22.

void testing!)
{ char 55 [100]; vector <Questions> v;
ReadQuestion(v); //
int k = getOptions () ; /'/
vector <Questions> w; //
w = GetTest(v, k); //
unsigned int i = 0, summa = 0; int j; char a;
while(i < v.sizef))
{ cout i + 1 "." v[i].question endl;
256 I. C++

for (j = 0; j < 5,
cout ' ' j + 1 ")" v[i] .answer [j] endl;
cout Rus ( " :_", ) ;
cin a;
if (a == v [ i ] . right) ++suirms; ++i;
}
W h a t f k , surratia) ; //
getch(); // ,


, (. 5.12).
. typedef -
, :
typedef vector<Qaestions> Question;
, . -
: ,
, -
. . ,
,
"" (# @).
6.23.

6.23,

void lookQuestions ( )
{ vector <Questions> v; ReadQuestion (v) ;
ClearScreen ( ) ; //
vector<Questions>: : iterator i;
for(i = v. begin {}; i < v.endO; ++i)
{ cout i -> question endl endl endl; // 2
cout Rus (" <0-, 1->? ", as);
int a = 0; cin a; if (a) v.erase(i);
}
ofstream to("c: /tests/question. bin", ios_base: : binary) ;
for (int m = 0; m < v.size(); ++m)
f spring s = v [m] .question+ ' # ' ; writeString (to, s) ;
for (int k = 0; k < 5; f+k)
{ s = v[m] .answer [k] + '@'; writeString (to, s) ; }
s = v[m] . right; writeString (to, s) ;
I
to. close ( )

.
6. C++ 257


[371, , -
. , -
/ . ,
, . -
/ :
istringstream; //
ostringstream; //
stringstream; //

sstream.
, -
. , -
6.24, .

j 6.24.

struct Complex { double re, im; };


string toString (const Complex &c)
( ostringstream os;
os '<' setprecision(3) re(c) ' + '
setprecision(3! im(c) ' i> ' ;
return os.str (} ;

/
, " C++".

93. 1264
7

C++ ,
. :
, , , .
C++
: "" , -
, . ,
(, Basic), C++ -
.

""
, , -
. C++ .

(. . 8).
, C++ , -
. -
. , .
, , :
, , ? , -
. -
:
int& fffint k)
{ int d; . . . return d; }

,
. Visual C++ 6 "" -
, :
260 I. C++

warning C41"?2: returning address of local variable or temporary


, -
, .
( 7.1), , -
, .

[ 7.1. ,

int n = 2; //
int& f f ( i n t k} // ,
( return k; } // ,
int f ( i n t Si) // -
{ return i; }
int main(void)
{ ccut f ( f f ( n ) ) endl; // -
int g = 5;
cout f ( g ) endl;
return 0;

"" 6684064, , , -
. , , 5.
? , , :
inns ff (int& k ) ;
G .
,
, -
. -
:
ints f f t i n t k )
{ static int a; . . . return d; }

"" Visual C++ 6.


, -
"" f.
, -
"" . ,
7.2.
7. 261

\ 7.2. ""

doubles max(double &, double y )


| return (x>y ? x : y ) ; }

, :
double k = max(a r b) ;
,
, :
double = 5, b = 6, = 7;
max(, b} = 10;

, .
, :
if ( > ) = 10; else b - 10.
, :
max(max(a, b ) , ) = 10;
max (max (a, b) = 10, ) = 0;


( ) . -
,
, 7, ( ) .
. ,
.
, -
. "" ,
, 7.3.

7.3. ""

int& nin(int ri, int d [ ] )


( int im = 0;
for (int i = 1; i < n; i++) im = d[im] < d [ i ] ? im : i;
return d[im] ;
1

, -
.
:
int [] = { 10, 20, 40, 5, , 1, 8, 9, 50, 12 };
int m = Mmin(10, x ) ;
262 I. C++

m 5
Mntin. , -
. :
Mmin(10, ) = 0;

, 5, .

""
. 5 , , ,
.
. . ,
, -
7.4.

I 7.4. ""

double* max(double *x, doable *y)


{ return (*x > *? : ); }

:
double = 5, b = 6;
*(&, sb} = 10;


. -
, . Mmin
:
int* Mrain(int n, int d[])
{ int im = 0;
f o r t i n t i = 1; i < n; i++) im = d[im] < d [ i j ? im : i;
return ud[im];
I
:
int [] = { 10, 20, 40, 5, 6 1, 8, 9, 50, 60 };
int rn = *Mmin(10, x j ;
*iin(10, x) = 0;

, ,
: -
, , ?
Visual C++ 6
7. 263

, (
7.5).

7.5. ""
; ,'....". .' : .'..;,.-.....' '. ;

double* max(double fix, double &y)


{ return (x > y? 5x: &y); }
doubles max(double *x, double *y)
( return (* > *y? *x: *y); )

, -. -

& &. :
cour *max(a, b) endl;
*max(a, b) - 20;

max (, -
, -
.
, -,
Visual C++ 6 :
error C2440: 'return' : cannot convert from 'double *' to 'double &'
'return': 'double *' 'double &'

:
cout max(sa, Sb) endl;
max(Sa, &b) = 10;

, C++ "-
". , , const
. 7.6.

7.6. ""

const doubled max(double &x, double &y)


{ return (H > y? x: y ) ; }


:
error C2166: 1-value specifies const object
1-value
264 I. C++

, , const, -
, ,
, .
double * const rr.ax (double *, double * y j
( return (*x > *y? x: y ) ; )

. -
- double.
*max{&a, &b) = 10;

, ,
.


C++ -
. , -
. , -
, -
.
-
:
int f ( . -
. -
,
, , , , .
-
.
-
. . -
, . -
, . - -
, , -
, , , "" .
, ,
, -
. :
int f |int k . . . )

, ,,
.
7. 265

, -
: ,
, -
.
-- C++
. , -
, , ,
, -
- .
,
. :
;
"" ;
.
, -
.
, -
. ,
,
. , -
-
, .
, -
: , , -
.
.
:
;
,
.
-
"" , -
: .
. 7.7.

1 7.7. ( )

double f(double n, . . . ) //
{ double *p = &; //
double sum = 0, count = 0;
while <*p) //
266 /. C++

{ sum += (*); //
++; //
count++; //

return ((sum) ?sum/count:0}; //

:
double = f (1.0, 2.0, 3.0, 4 . 0 , 0 . 0 ) ;

2.5. -
,
f d , 2, , 0) (
), ( -
) .
, -
, , . -
, .
. :
double ffint n, ...) //
{ int *p = &; // - ""
double sum = 0, count = n;
for (;; (double*)--) // int* -> double*
sum += (*p);
return ((sum)?sum/count:0);

:
double f(int n, ...) //
{ double *p = (double *)&n; //
double sum = 0, count = n;
for (;n;p++) //
sum += (*p) ;
return ((sura) ?sum/count:0) /
}
,
, . -
double-, sizeofunt;
"" .
8 (sizeof (double)),
. , ""
, double *.
?. _ 267

,
. -
7.8.

| 7,8. ()

double f ( i n t n, . . . ) //
{ int *p = &;
++; / / ( "" double)
double *pp = (double *)p; //
double sum = 0, count = n;
for (;n ;pp++} // 8
sum += (*pp) ;
return ( ( sum) ?sum/count : 0) ;

++
double. 8 -
:
double * = (double *}p;
++
sizeof (double) 8 , .
.
-
. -
,
. , -
. -
( ):
double f (double- &n, . . . )
:
double m[] = { 1.0, 2 . 0 , 3 . 0 , 4 . 0 , 0 . 0 f;
cout f ( m [ 0 ] , m [ l ] , m [ 2 ] , m [ 3 ] , m [ 4 ] ) endl;

2 . 5.
-+
. -
,
- "". , -
, (. 7.8) ( -
-) * double, -
. "" ,
268 I. C++

"" .
, ,
, -
Intel 4 . ""
union. , ,
7.9.

I 7.9. - ()

double f(int n, ...)


( int *p = &n; // ""
P++; // "" -
union Pointer
{ double **; double *kp; }; // ""
Pointer A;
A.kp = (double *); // ""
double sum = 0, count = n;
for (;n;A.pp++) //
sum += (**A.pp); //
return f(sum]?sum/count:0);

union undefined behaviour


( ), , (,
Intel) . -
.
, "" -
- "" ,
sizeof (double), 8 , sizeof (double*),
4 . -
. :
cout f ( 2 , & a , fib) endl;

(
- )
. -
"" ,
7.10, "" -
.

7,10. - ( )

double f(double "a, . . . )


{ double **p = &; // -
?. 269

double sum = 0, count = 0;


while (*p != 0) // NULL
{ sum += (**) ; /V
count++; ++; // ""
}
return ( ( s u m ) ? s u m / c o u n t : 0 } ;

:
f ( & a , ub, 0 )

: -*- -
, . -
, .
, -
char.
:
(char) , (char *) -
;
Q char * , -
.
,
,
. :
char *t(char *sl, ...)

,
, . -
, -
0. 7.11.

I 7.11. { )

char *f(char *sl, . . . )


( char **cp = &sl; //
int len = 0;
//
while (*) { len += strlen(*cp); -+; }
char *s = new char[len + 1]; //
s[0] = 0 // ""
//
= &sl; // 1-
270 I. ^++

while (*cp)
{ strcatfs, *cp); // ( )
++; //

return s;

:
char * = f ( s l , s2, s3, 0);
si, s2, s3 ,
char *. -
:
char *sd = f("First", "Two", "Three", 0 ) ;
, - () -
, (). , -
char , -
.


-
, stdarg.h. -
,
, -
. NULL. ,
, -
:
void va_start (va_list prra, );
va arg(va_list prm, );
void va_end(va_list prm);
typedef va_iist.
va_start va^iist ,
va_arg ,
va_end .
.
1. -
,
va_iist, va_list Last?;
.
2. (
)
7. 271

va__start, va_start (LastP, ) ; -


.
3. -
va_arg. -
, . . -
. , va_arg
: va_arg(LastP, int).
4. va_end, va_end(LastP).

( )
. 7.12.

7.12.
()

double f(int n, double a, ...)


{ va list p; //
double sum = 0, count = 0;
va^start (p, n) ; //
while (n )
( sum +- va_arg(p, double); //
count ++;
)
va_end(p); // ""
return ( (sum) ?sum/count: 0) ;

,
7.13.

7.13. ( )

double f(double a, ...)


{ va list p; //
double sum = 0, count = 0;
va_start(p, a); //
double k = a; //
do { sum += k; count++; }
while (k = va__arg(p, double)); // ,
va_end(p); // ""
return ((sum)? sum/count: 0);
272 I. C++

do.. .while, . .
.
, .


C++ ,
-
. , ,
.
!.
,
:
! = 1 * 2 * 3 * ... *

-
, , for.
7.14.

! 7. 14.

long Fact (int k)


{ long f; int i;
for (f = 1, i = 1; i < k; i++) f *= i;
return f;

() ,
:
0! = !
V >0 ! = ( -1)!
()
, :
/ -1
2) = i
V > 2/() = F(n - 1) + f ( - 2)
, .
,
. , , -
[9, 30, 39],
. -
7, 273

( ) -
. , -
! ( 7.15).

7.15.

long Fact f i n t k)
{ if (k = 0! return 1;
return ( k * Fact(k 1 ) ) ; //

,
, 7.16.

7.16.

long Fibo(int k)
{ if ({k = 2) I | (k = 1)} return 1;
return (Fibo(k - 1) + Fibo(k 2}); //

,
. , ""
, . ,
"" .
.
, -
, .
, .

.
J L W


. ,
,
. [9] [30, 39]. :
void Quicksort(A, I, n)
{ // 1 < k <
// k-ro
Quicksort(, 1, k-1); //
Quicksort , k+1, n) ; //'
1
"" (
, . .),
274 I. C++

[9. 34| . -
,
. ,
. ,
7.17, -
(. 3.11).

I 7.17.

unsigned int Length (char *s)


{ if (*s == 0) return 0; // !\*s}
else return 1 + Len(s + 1);
1

-
, :
cout Len("1234567390");

.
"" ,
,
:
G .
, ;
, , -
, ;
,
if -
.
:
unsigned int Length (char *s)
if (*s) return 1 + Len(s + 1) ;
else return 0;
1

:
;
;
.
, -
.
^. __ 275

, , -
. -
:
while ( )
i //
/ /

.
f.
,
, .
7.18.
.......................................................................................;;
! 7.18.

void ReadFile(ifstream & f )


( char s[100]; //
getline(f, s) ; II
cout s; //
if ( ! f . e o f ( ) ) //
ReadFile(f); //

int main (void)


( ifstream f ("recurs. cpp") ;
ReadFiletf) ;
f .close () ;
return 0;

, ReadFiie
. -
, f .
, ,
.


Rec
s
Rec. ,
: , -
. , -
276 I. C++

. -
" ..." -- -
.
, :
void PriestAndDog(void;
( cout " , ,";
cout " ', ,";
cout " , :";
PriestAndDog();
I

.
( -
). ,
, -
. if
, . -
if , .


-
. ,
Fact(k ),
.
k . -
, -
.

(-
).
1. ( -
): void Reclvoid) { S; if () Rec ( ) ; }.
2. ( -
): void Rec [void) ( if () R e c ( ) ; S; }.
3. ( ),
( ): void Rec (void)
{ 31; if () Rec ( ) ; S2; }.

void Rec (void)

, .
" " " "
7. 277

"-
".
.
, . ,
,
( 7.19).

7.19.
. .......... ..:. ..:!>.....-.;'..;;;:: Z... ..*. . ,. ;
long Fact(int k)
{ if (k > 0) return (k * Fact(k - 1)); //
else return 1;

. -
"" :
void ReadFile(ifstream &f)
{ char s[100]; //
if (If.eofO) //
ReadFile(f); //
getline{f, s); //
cout s; //

. ,
.

,
. , :
void ReadFile(ifstream &f)
{ char s[100]; //
getlinetf, s); //
if (If.eofO) //
ReadFile(f); //
coat s; //
}

, , . -
. , -
.
,
.
278 I. C++


, ,
"" . , -
-
. ( , ) -
,
, . -

. -

, , .
. -
. -
. ,
ReadFile
. , -
.

.
. -
, -
: ,
, .
,
, . -
, , (
).
:
bool Palindrom{string s)
{ int i - 0, j = s.length(I - 1;
for (;i < j ; i++, j ) if ( s [ i ] != s [ j ] } return false;
return true;

.
.
, . -
, ,
:
j s ;
s .
7. __ _________ __ __ 279

.
:
bool Palindromfstring s)
{ if (s - = 1) return true
else if ( == )
return Palindromfs )
else return false;

, -
7.20.

I 7.20. PaJ-indrom

bool Palindrom (string s)


{ int end = s. length 0-1;
if ( s . length ()=0 | | s .length 0-1} return true;
else if ( s [ 0 ] == s[end]) return Palindromfs .substr (1, end-1) ) ;
else return false;

, , :
cout PalindromfnoTon") endl;
:
string s = "";
;
cout Palindrom(s) endl;
.
, , : ,
, ,
,, . -
. . -,
,
. . -,
. -
. -
.
"".
, s [ 0 ] - s[end] ( ""). -
true, return
:
Palindrora(s.substr(I, end-1))
280 I. C++


end. s[0] == s[end] ( ""), -
-
"". true if.
,
.
(true,
l).

. -
:
1. .
2. "", .
3. "",
.
4. "",
.
3 4 . -
"" C++
( 7.21).

i 7.21.
;....'.'. ; -...' -.. !

int BinarySearch(const int a[], int b, int e, int k)


{ if (b = e) return -1; //
int = ( + e) / 2; //
if ( [] == k) return c;
else if (a[c] > k) = BinarySearch(a, b, c, k); //
else if (a[c] < k} - BinarySearch(a, c+1, e, k); //
}

, 1. -
.
, .

. :
int d[] = { 1, 2, 3, 4, 5, , 7, 8, 9, 10, 12, 13 };


int k = BinarySearch(d, 0, 12, 7);

k i, . -
,
7.

1 () ,
4 .
, -
- " ":
,
.
. , -
. -
.
-
:
for (int m = [ 0 ] , i = 1; i < n ; i++) m = ( ( a u ] < m) ? a [i] :m) ;

,
. 1
.

, , -
. , -
, , -
. ,
( -
7.22).

| 7.22.

int minfconst int a [ ] , int left, int right)


{ if (left == right) return a [ l e f t ] ; // a [ r i g h t ]
int m = (left + right) / 2; //
int x = min(a, l e f t , m ) ; //
int = miri(a, m+1, right); //
if {x < ) return x; else return y;
1

, -
( ), (
). min -
, , ,
, .
. -
, -
. ,
-
282 _ I _++_

. ,
, :
void ppfints tt, int 1, int )
{ cout endl;
for (int i = 0; i < tt; i++) cout ".";
cout "min(" 1 ',' r ')';
}

"" min :
int min (int [], int left, int right)
{ static int tt = 0; //
pp(++tt, left, right) ; //
if (left == right) return a [left]; // a[right]
int m - (left + right) /2; //
int x = min {a, left, m) ; //
pp( tt, left, right); //
int = min (a, m+1, right); //
pp (--tt, left, right) ; //
if (x < y) return x; else return y;
}

"" , 8
in (, , 7) :
. min {0, 7)
. . min(0, 3]
. . . min (, 1)
. . . . min (, )
. . . min (, 1)
. . . . rrtinfl, 1 )
. . . min (0, 1)
. . min (0, 3)
. . . min (2, 3)
. . . . m i n (2, 2 )
. . . min (2, 3)
. . . . min(3, 3 )
. . . min (2, 3)
. . rtiinlO, 3)
. m i n ( 0 , 7)
. . min ( 4 , 7 )
. . . m i n ( 4 , 5)
. . . . min ( 4 , 4 )
. . . min ( 4 , 5)
. . . . mints, 5 )
. . . m i n { 4 , 5)
\7^ 283

. . min(4, 7)
, . . m i n t , 1)
. . . . min(6, 6 )
. . . m i n t , 7)
. . . . min(7, 7 )
. . . m i n t 6, 7)
. . min(4, 7)
. m i n ( 0 , 7) //

& -
, -
.
. -
. "
" "" -
: ( -
) , 1 . -
7.23.

....... rirwi
17.23. .......
" .........

int mini tint a[], int begin, int end)


I if {begin == end) return a [begin];
int x = mini (a, begin+1, end);
if (x < a [begin]) return x; else return a [begin];

, (. 7.22), -
, . -
, ,
. , -
"" :
. min(0, 9)
,1 9}
. . mind, ,
. . . min(2, 9)
. . . . min(3, 9 )
..... min(4, 9)
...... min(5, 9}
....... m i n t 6, 9}
........ min(7, 9)
......... min(8, 9)
.......... min(9, 9)
......... min(8f 9}
. minC7, 9)
284 I. C++

min(6, 9)
min(5, 9)
m i n ( 4 , 9)
. . . . min(3, 9 )
. . . m i n ( 2 , 9)
. . m i n f l , 9)
. m i n ( 0 r 9)

, -
. -
.
.
.
! -
:
else return a [ b e g i n ] ;


. , -
, -
( ).



, ,
: , .
( ),
.
, -
.


, . 4.
:
struct Item { int info; Item* next; };

:
typedef Item* link;

, :
link Head; //
7. 285

, -
( 7.24). -
- .

7.24.

long count (link p)


{ if (p == 0) return 0;
return 1 + count (p -> n e x t ) ;

, ""
.
.
-
. ,
, , . -
7.25.

7.25.

void visitor (link p, void fflink p) )


{ if (p = 0) return 0;
f(p); //
visitor (p -> next, f ) ; //

, ,
. , , -
.
-
!
ReadFile,
,
. -
7.26.
\:
I 7.26.

void visitor(link p , void f ( l i n k p ) )


{ if (p == NULL) return 0;
v i s i t o r ( p -> next, f ) ; //
f(p); //
. '

286 _ I. C++

, -
. , " " - -
. -
-
,
.
. ""
.
7.27.
;:; ..... ' ............... ' ........................... ............................. - ......................................................................................................................................................... '"" ----- :
; 7. 27,

void visitor (link p, void p r e d f l i n k p) , void post (link p) )


{ if (p = NULL) return 0;
pred(p) ;
visitor (p -> next, pred, post); //
post (p) ;


, , -
. -
" ". -
:
struct Node {
string 'name; //
Node *Left; // " "
Node *Right; // " "

, , ,
. ,
, .
"" [9, 14, 16, 39, 41, 45]
"" . -
: Root, " " Left, " " Right,
.
1. Root, Left, Right. [9] preOrder.
2. Left, Root, Right. inOrder.

3. Left, Right, Root. postOrder.


7. 287

-
. . -

. -
7.28.

! 7.28.
*". /..-
- -
typedef Node* link;
void preOrder (link p, void visit (link p) )
{ if (p ! = NULL} //
{ visit (p) ; //
preOrder (p -> Left); //
preOrder (p -> Right); //

void iriOrder (link p, void visitflink p) }


{ if (p J= NULL) //
{ inOrder(p -> Left}; //
visit (p) ; //
inOrder (p -> Right); //

void postOrder (link p, void visit (link p) )


{ if (p != NULL) //
{ postOrder(p -> Left); //
postOrderfp -> Right); //
visit (p); //

visit , -
inOrcier -
.
,
,
.
. -
,
.
,
,
, -
288 I.

, -
[16]. :
. ,
. , -
NULL. -
: ( -
), .
7.29.

[, 7.29.

link Search(link p, char *name)


t int cmp;
if (p = NULL) return NULL; //
onp - strcmp(name, p -> name!
if (cmp == 0) return p; //
else if (cmp < 0)
-> left, name); //
else
Search(p -> right, name); //

,
. , -
, 7.30.

7.30.

unsigned long count(link p)


{ if (p = NULL) return 0;
return-count(p -> Left) + 1 + count(p -> Right);

unsigned long -
.
( 7.31).

I 7.31.

typedef unsigned long ul;


ul h(link p)
{ if (p == 0) return 0; //
ul x - h(p -> Left); //
ul = h(p -> Right); //
7. __ 289

if ( > ) return +1; // +1


else return y+1; // +1

-
.



. .
, -
. :

?
-
?
-
. , -
, 10!.
:
long Fact(int k = 10)
{ if {k > 0) return k * Fact(k-l); else return 1; }

:
cout Fact(6) endl;
cout FactO endl;

720, 10!,
3628800. ,
.
. ,
, , :
double Fact(double k);
double Fact(double k = Fact(4))
{ if (k > 0) return k * Fact(k-l); else return 1; }

:
,
, .
F(F(x)).
(4!)! = 241;

. 1264
290 I. C++

double, 24!
, unsigned long.
, ,
Pentium 32? 1754!. 104930,
long double;
double, -
. -
.
, -
"" (. 2-5)
.
, , .
,
- ( 7.32).

| 7.32. -

double Fact(unsigned int& k)


( if (k == 0) return 1;
return (k * Fact (k-1));


int n = 5;

Factfn} 120, n 5.
? :
double Fact(unsigned ints k)
( if (k = 0) return 1;
return (k * Fact (k)); //
}

Fact(k-i) Fact(--k). -
.
n 1. -
, .
Fact (n) n .

.
II

-

8.
9.
10.
11.
12.
'
8

-
. - , -
, .
,
, .
"",
,
.
C++ ,
. ,
C++ -
, , " ". -
, , -.
, . -,
,
. -
. ,
. -, -
,
, .


C++,
: enum struct. , ,
,
.
, .
294 //. -

, , , -
:
1
= b + ;

: -

= add(b, ) ;

. C++
. ,
/ , " "
. , , ,
iostream .
C++ .
, -
. .
, (, ** )
, ,
(, "). , -
:
. () ;
. * ;
?: ;
:: ;
sizeof ;
# () ;
G ## .
, -
. ,

double, . -
:
double operator A (double a, int b)
Visual C++ 6
2803:

'operator ' must have at least one formal parameter of class type
'operator A l , ,

, ,
, -
erium.
8. 295

=, [ ] , -
-> . -

.
, .
"" .
- :
operators( );
.
. ,
. -
, -
, -
, .
, , :
operators( 1, _2);

, . -
-
:
_1@_2;
D operator@ (_1, _2).
:
operater@();

:
;
-
operators ().

enum struct. .


Borland C++3.1
.
Visual C++ 6, Borland C++ Builder 6 Borland C++ 5.

enum
:
enum Decade -
{ zero, one, two, three, four, five, six, seven, eight, nine };
296 . -

.
,
10.
. , -
. 8.1, -
10.
8.1. 10

+ zero one two three four five six seven eight nine

zero zero one two three four five six seven eight nine
one one two three four five six seven eight nine zero
two two three four five six seven eight nine zero one
three three four five six seven eight nine zero one two
(our four five six seven eight nine zero one two three
five five six seven eight nine zero one two three four
six six seven eight nine zero one two three four five
seven seven eight nine zero one two three four five six
eight eight nine zero one two three four five six seven
nine nine zero one two three four five six seven eight

, - -
:
Decade operator*{const Decade &a, const Decade &b)

, ,
.
" "
Decade operator-*- (const Decade &a, const Decade &b)
{ return Decade((a + b} % 10); }
Visual C++ 6 -
. , +
,
-. ""
( 8.1).

I 8.1. , 1
Decade operator+(const Decade sa, const Decade &b)
{ Decade t = Decade{(int)a+(int)b); return Decade(c % 10); }
8. 297

t -
. -
+ , -
int , -
Decade. return ,
Visual C++ 6 :
error C2440:'return': cannot convert from 'int1 to 'enum Decade1
'return1: 'int' 'enum Decade1


, . ,
C++ .

Decade , ;
= seven;
= six;
cout A + endl;
(7 + 6) % 10, . ,
, ,
. , -
:
Decade A = seven, = six;
.
cout operator+(A, ) endl;
-
.
.
:
+=, "" -
. . ,
: -
, , ,
. ,
( -). , -
, Decade.
8.2.
\ ....... ' ........... "": ......... " ................... : ................... '"-"''"" ....................... - ..................... - .................. 7~ ....... ............................. " ..... " .............................. \
I 8.2. ()

Decade opera tor+= (Decade &a, const Decade &b)


{ return Decade (a = Decade ( ( a + b) % 1 0 ) ) ; }
298 II. -

"" : +
.
:
cout ( += ) endl;
, +=
,
.
+= , -
. , :
decade A = one, = one, D = one;
cout ( += += D) endl; // - two, A = three
cout (( += } += D) endl; //

+= -
, !
. :
cout operator-*-=(A, operator+=(B, D) ) endl;

"" operator+={B, D),


two W "" , .
:
cout operator4=(operator+=(A, ), D) endl;
, ,
, .
8.3,

8.3.

Decades operator+=(Decade Sa, const Decade Sb)


{ return Decade(a = Decade((a + b) % 1 0 ) ) ; }

+= -
8.4.

| 8.4. , 2

Decade operator+(const Decade Sa, const Decade &b)


( Decade t = a; return Decadeft += b) ; }

, .
Decade int ( 8.5).
8. 299

\ 8.5. Decade int

Decade operator+{const Decade &a, const int &b)


{ Decade t = a; return Decade(t += Decade(b % 1 0 } ) ; )
Decade operator+{const int &a, const Decade &b)
( Decade t = Decadefa % 10); return Decade(t += b ) ; }

( }
,
Visual C++ 6 Decade, a
int ! Visual C++ 6 -
, , operator (, 1 ) .
, , Visual C++ 6, . . Borland, Visual C++ 7
, . -
, , -
"" .

,
. -
++. (. . I),
. -
( 8.6).
:-- .............. ............................................................. ........................................................ ' .................................................... ............. |

| 8.6.
........ .', .................. ... ............. , ................. i.;...'.\, ....... ,*......*,. ...... ..,.. .......... . ............................... ,...,..,!...,. ......................... '. ..... ,... ..... ,.,.,; *'--i->i

Decade operator++ (Decade" &a!


{ return Decade((a += one) % 10); }

:
Decade operator++{ Decade &a)
{ Decade t = ; //
= Decade ( ( a += one) % 10) ; //
return Decade (t) ; //

, -
, . , -
,
.
,
, .
-, ,
8.7.
300 II. -

\ 8.7.
! ~_ .".* ~ ;...... .._ ;; ...;;;.

Decade operator++ ( Decade sa, int }


{ Decade t = a;
a = Decade {(a += one) % 10);
return pecade(t);
1

-
:
int main (void)
{ Decade A = seven, = six;
cout +H-A endl; // eight
cout A++ endl; // eight
cout A endl; // nine
return 0;
i
, , -
, , C++ -
.
,
Decade.
, .
- 8.8.

| 8.8. Decade

ostreams operator (ostream sout, const Decade &a)


{ char * s [ ] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"

out s [a] ;
return out;

,
Decade.


, . -

. -
8. 301

,
. [37] " ".
.
,
. -
, unsigned long.
struct Rational { unsigned long num., denum; };

Rational
, .

() -
. -
8.9.

= 8.9. " "

typedef unsigned long ULONG; //


struct Rational { ULONG num, denim; };
ULONG gcd(ULONG x, ULONG y)
{ if (y 0) return x; return g c d f y , x % y ) ; }
void reduce(Rational &c)
{ ULONG t = {{c.num > c.denum) ? gcd{c.num, c.denum)
: gcd(c.denum, c . n u m ) ) ;
c.num /=t; c.denum /=t; //
1

,
> .

+=. , -
:
[/) + (c/d) = (ad + cb)/(bd)
.
. -
, -
. 8.10.

| 8.10.

RationalS operator+=(Rational &a, const Rational Sb)


{ a.num = a.num*b.denum + b.num*a.denum; //
302 It. -

a.denum = a.denum*b.denum; //
reduce() ; //
return ;

Decade,
,
. .
""
( 8. ).
: ? ,...--..-.-....,. ...
I 8.11. ""

Rational operator+(Rational a, Rational b)


{ Rational t = a; t += b; reduce(t); return t; }

(-
8.12).

| 8.12. /

//
ostreamfi. operator (ostreamS t, Rational a)
{ return ( t ' ( ' a . num ' / ' a . denum ' ) ' ) ; }
//
istreams operator (istream& t, Rationals a)
{ t a. num; t a.denum; reduce(a); return t; }

,
.
:
void main (void)
{ Rational A, B, ; //
A. num = 1;
A.denum = 2;
. num = 1 ;
B. denum = 3;
= + ; //
cout endl; //

( 7 / 6 ) . , -
. , C++
- .
8. 303

, ,
:
void main (void)
{ Rational d[5], ;
cout " 5 : num denum <enter>" er.dl;
for {int i - 0; i < 5; i++) cin d[i]; //
for (i = 0; i < 5; i++) coat d[i] endl //
= d[0] + d[l] + d[2] + d[3] + d[4J; //
cout endl;

-
.
, . -
<Enter>:
1 i
2 2
3 3
4 4
5 5

-
, .
:
(1/D
(1/D
(1/D
(1/1)
(1/1)
(5/1)

,
. . -
( 8.13) -
.

8.13.

Rational operator*(Rational a, Rational b)


{ Rational ;
.num = a.num*b.num; //
.denum = a.derium*b.denurn; //
reduce(); //
return ; //
*
304 II, -


unsigned long ( 8.14). .
$:: ": ;''. ;
;
: " -. 8.14.
. . i- -i Rational - -unsigned
"S" long
,:
,.,..,.,,.....

Rational operator* (Rational a, const ULONG &b)


{ Rational c;
c.nura = a.num*b;
. denurn - a . denum;
reduce ( ) ; / /
return ; //
)
Rational operator* (const ULONG &a. Rational b)
{ Rational c;
c.nura = a*b.num;
c. denum = b. denum;
reduce ( ) ; / /
return ; //


, "" . -
:
int main {void}
{ Rational A, B, ; //
cout " : <Enter>";
cin A;
cout " : <Enter>";
cin B;
cout A * 3 endl;
cout 5 * endl;
ULONG d = 8;
cout * d endl;
cout .d * A endl;
return 0;
\

= (1/2) = (1/4) :
8. ; 305

-
. ,
.
Decade -
.
+= ,
( 8.15).

.15. Rational
int

Rational& operator+=(Rational &, const ULONG &b)


{ a.num = a.num + b; reduce(a); return a; )

, ,
+= 2. 2 += -
C++, . -
(-
8.16).

8.16.

Rational operator*-*- (Rational &)


{ += 1; reduce(a); return a; }
Rational operator-t-t-(Rational &a, int)
( Rational t = a; a += 1; reduce(a); return t; }

,
, :
cout ++ endl;
cout ++ endl;
cout A endl;

( 2 / 3 ) , :
(5/3)
(5/3)
(8/3)

, , > (-
). 8.17. , -
, . "", -
.
.
30g II. -

\ 8.17. >
;
!...., , "... , .1...... ......... ...... , .

bool operator>{Rational a, Rational b)


{ return (a.num*b.denum > b.num*a.denum); }


. Person. -
,
string:
struct Person { string fio; Date BirthDay; };
, .
Person = {"", ( 1955, 12, 21 ) };
Visual C++ 6 -
. , ,
. C++ -
, -
. , ,
. .
, -
. , -
:
.
Person, -
fio .
:
Person{char s [ ] )

:
Person(char *s)

( 8.18),
| ' ...;, ..; , .,... ..,

: 8.18.

struct Person
( string fio;
Date BirthDay;
Person(char *s)
8. 307

, , ,
.
Person :
Person (" !");

:
Person = " !";

:
Person = Person(" !");

() , -
.
, -
.
" ": ,

Person t;

. , ,
.
, - -
:
;
, ;
.
, C++.
, C++ " "
. -
, .
1. . , -
, .
2. . -
. Person
c h a r t ] .
3. . -
.
- .
, , .
, -
BirthDay. -
8.19.
8. 309

. , -
:
Person(const char *s, unsigned int y, Month m, unsigned int d)
{ fio = s; BirthDay.year = y; BirthDay.month = m; BirthDay.day = d; }

, -
:
Date. , ,
Month , . .
. -
, typedef.
, -
. . ,
.
Date ( 8.20).

. 8.20. Date

typedef unsigned int UI;-


struct Date
{ enum Month
{ jan_=l, feb_, mar_, apr_, rnay_, jun_,
jul_, aug_, sep_, oct__, nov_, dec_

UI year; Month month; UI day; //


Date(){} //
Date(const Date &d) //
{ year = d.year; month = d.month; day = d.day; }
//
Date (const UI y, const Month m, const UI d)
{ year = y; month = m; day = d; }
//
Date(const UI y, const UI m, const UI d)
{ if ((0 < m) && (m < 13}) { year - y; month = Month(m); day = d; } }
};

:
1.
, .
2.
. ,
Month . ,
310 II. -

.
C++ .
,
"" ,
. "" :
Date::Date(const , const Month m, const UI d)
{ year = y; month = m; day = d; }
Date::Date(const UI y, const UI mr const UI d)
{ if ( (0 < m) &S (m < 13)) { year = y; month = Month(m) ; day = d; } }

,
. :
, -
; , .
:
Date A; //
Date = (2001, Date::feb_, 22}; /I
Date (2002, Date::feb_, 22); //
Date G(20Q3, 2, 22}; // 4-
Date D(B};. //
Date F = ; //
Date H = Date(2005, Date::feb_, 22); //
Date E = Date(2007r 7, 29); // 4-

Month , -
.
Person:
Person U(" ", Date{1986, Date::jul_, 2 9 ) ) ;

,
Date, .
-
. C++
*. Rational (
8.21).

! 8.21. Rational

typedef unsigned long ULOHG


struct Rational

' He , -
.
8. 311

{ ULONG num, denura;


Rational () { nurr. = 0; denum = 1; ) //
Rational(ULONG n) / /
( num = n; denum = 1; }
Rational(ULONG n, ULONG d) //
( if (d != 0) { num = n; denum = d; } }
Rational(const Rational Sr) //
{ num = r.num; denum = r.denum; }

8.22.

8.22.

struct Rational
{ ULONG num,, denum;
Rational(} :num(0) , denum(l) ( }
Rational(ULONG n) :num(n), denum(1){ }
Rational(ULONG n, ULONG d) : num(n) , denumi(d != 0)?d:denum) { }
Rational (const Rational &r) : r.umfr.num) , denumfr.denum) ( }

, . , -
(. 8.22)
, . -
, ;
Rational(ULONG n, ULONG d):num(n), denum(init{d)) { }

, , :
ULONG init (ULONG ) { return ( != 0) ?y:denurr.) ) ; }

,
, -
-
.
. , , -
.
-.


-
- ( 8.23).
312 II. -

\ 8.23.

struct Old { int a; float b; //


Oldfint x, float ) //
{ = x; b = ; }
};
Old g{01d p) //
( return p; }
void main(void)
{ Old x = g(01d(l, 2 . 0 ) ) ; } //

.
.
,
.
8.24.

: 8.24.

struct Old ( int a; float b;


Old(int x, float ) //
{ a = x; b = y; }

Old g(01d *p) //


{ p->a = p->b+6; // "->"
(*).b = p->a+7; // "."
return *p; // ,

void main(void)
{ Old x = g(new Old(l, 2.0)}; } //


,
. -
. Person:
Person B = Person(" !");
:
const char * > Person
(, 8.13 8.14), ,
,
8. - 313

. -

. -

, (
8.25).

| 8.25.

struct Rational
{ ULONG num, denum;
Rational (ULONG n - 0, ULONG d = 1)
{ if (d != 0) { num = n; denum = d; } }

. ,

. -
:
long -> Rational
:
Rational = 3;

-
,
. -
== . , -
bool, :
bool opera tor== (Rational a. Rational b)
{ return (a.num*b.denum == b. num* a. denum) ; }

-
.
, =
-
.
-
:
Rational > long

C++
:
operator long () { return ( num/ denum) ; }
314 _ II. -^^

-. -

Rational :
int main (void)
{ Rational mm = 11; // inr. -> long > Rational
cout ram endl; // (11/1)
long i = mm; // Rational -> long
cout i endl; // 11
return 0;

, i ,
Rational.
long. -
.
:
Rational f (Rational a)
{ return a; }
long f f (Rational a)
{ return a; } i

:
Rational zz - f (13) ;
long k = f f (zz) ;
13
Rational , zz /1).
-
, k 13.
, . -
Rational -> long, , -
.
long -> Rational C++ -
explicit.
:
explicit Rational (ULONG n = , ULONG d = 1)


, . -
:
Rational ram = Rational (11) ;
Rational zz = f (Rational (13) );

Rational, .
8, _ 315

double
. , -
, .
double, 8.26.
I ......... ; ............................................................... "- ................ "" ......... ; ....... - .............................................................................. - ............ ; ...... - ............................
j 8.26. - double

struct Double
{ double t;
Double (double a = 0.0): t(a) { }
operator doubled { return t; )


double. , ,
:
double operator^ (const Double &a, int b)
( double t = a; // double <- Double
if fb = 0) return 1; // int -> double
else if (t == 0} return 0; // int -> double
else if !b == 1)- return a; // Double -> double
else if {b < G) { t = 1/t; b =- b; } //
double p = 1 ;
for (int i = 1; ! (i > b) ; i++) p *= t; //
return p; '

, :
int main (void)
{ Double d = 0;
cout double (d"-2) endl;
cout double(dnO) endl;
d = 2;
cout double (d^O) endl;
cout double{dAl) endl;
cout double (dn-2) endl;
cout double (d"3) endl;
return 0,-

. -, -
d -
: int -> double double -> Double.
316 11^ -

-, ,
, .
-, double -
,
Double.
, ""
. , -
Java, --
.


Rational -
. ,
"" . -
. +=,
.
, ""
, , . . .
gcd reduce .
reduce "" , a gcd (
8.27).

! 8.27. Rational

void Rational::reduce 0
{ ULOHG t = ( (num > denum) ? gcd (rium, denum) :gcd (denum, num) ) ;
num /=t; denum /=t,-
.
}
ULONG gcd(ULONG x, ULONG y)
{if (y = 0) return x; else return gcd(y, x%y); }

reduce , . . -
. gcd
,
.
+=.
+= (. 8.10) Rational. ,
;
Rational^ operator+=(Rational &a, const Rational &b)
,
:
8. 317

error C2804: binary 'operator +=' has too many parameters


' +='

, , . -
, .
- -
. - "-
", . , -
:
_.__ ()

_ -
. t
Rational, . -
- :
Rational t = 2;
t.operator+=(3)

t . ,
-
.operator.
, .
- , this!
,
-
: *, ->.
, +=
. :
Rationale operator+=(const Rational sb)

.
. , . .
. , .
, (. 8.10)
+= ( 8.28).

: 8.28. -

Rational^ Rational::operator+=(const Rational &b)


{ num = num*b.denum + b.num*denum; derium = denum*b. denurn;
(*this).reduce(); //
return (*this); //
318 II. -

. reduce -
:
this -> reduce();

(*this), this, . . , -
( "" 7.5).
. +=, C++, -
. -
,
:
Rational t, z.;
int it, iz;
t = 2, z = 3; t -1-= z +*= 2; // t = (1, 1 ) , z = (5, 1)
it = 2 , iz = 3; it += iz += 2; //
t - 2, z = 3; t+=(z += 2 } ; // t = ( 7 , 1), z = < 5 , 1)
it = 2 , iz = 3; it+=(iz += 2 ) ; //
t = 2, z = 3; (t += z}+=2; II t - (7, 1 ) , z = (3, 1)
it - 2, iz = 3; (it += iz)+=2; //' it = 7, iz = 3

, "" , "",
.
,
,
. = +=.
8.29.

! 8.29.

Rationale Rational::operator=(Rational b)
( rium = b.num; denum = b.denum; ' return *this; }

:
// t = 7, z = 3
// it = 7, iz = 3

. -
. C++ . ,
, , -
. , ,
, . . [37] -
Date (. 8.20) , , -
1 ( 8.30).
8. 319

; 8.30. ,
Dates AddYear (int n) { year += n; return *this; }
Dates AddMonth (int n) { month = Month(month + n); return *this;}
Dates AddDay (int nj ( day += n; return *this;}

, . -

Date d ( 2 2 , 02, 2003};

:
cout d . A d d Y e a r ( l ) . A d d M o n t h ( l ) . A d d D a y ( l ) endl;

:
23 3 2004

Rational. -
. , -
. :
Rational Rational::operator++()
{ return (*this += 1); }

, ,
. , -
:
Rational Rational::operator++(int)
{ Rational t = *this; -this += 1; return t; }

, -
"" . -
, , :
int main(void)
{ Rational z = 2;
cout ++z endl; // (3/1)
cout z++ endl; // (3/1)
cout z endl; // (4/1)

-
-= .

?
""
-.
. 8.31.
320 It. -

8.31. -
.,.* ,*..,. .. - , ' ;,'."...,,. .,...., 1 -,.,

Rational operator+(int b)
{ Rational t = *this; return (t += b); }


Rational t = 2;
cout t + 3 endl; // {5, 1)
cout 3 + t endl; // 5

, ,
! -
:
cout t,operator+(3) endl;
cout 3.operator+(t) endl;


, Visual C++ 6
. , -
3 Rational. -
,
long. , -
.
Rational, :
cout Rational(3+tJ endl; // (5, 1)

:
cout Rational(3)+t endl; // (5, 1)

, -
.
, "" Decade (. 8.5),
Rational, . C++
.
, , , -
Rational ( , -
) -
:
Rational t = 2, z = 3;
cout t + z endl; // (5, 1)
cout z + t endl; // (5, 1)

. -
-.
8. 321


(. 8.12). -
: . -
, , , -
. ,
( 8.32).

8.32.

ostream& operator (ostream& t)


{ return (t ' ( ' num ' / ' denum ' ) ' ) ; }

. -
R: ,
R.operator (cout) ;

:
Rcout;

, -
! , . -
, "" -
-.
, . -
/ -
.
, -
8.33.

8.33. Rational

typedef unsigned long ULONG;


struct Rational
{ ULONG num, denum;
Rational (ULONG n - 0,- ULONG d - l):num(n), denum(d) { reduced,- }
Rational^ operator+=(const Rational &b);
Rationale operator^(Rational b);
Rational operator++();
Rational operator++(int);
operator long() { return num/denum; }
ULONG gcd{ULONG x, ULONG y]
( if |y == 0) return x; return gcd(y, x%y) ; }
void reduce();

1! . 1264
II. -

void Rational : : reduce f )


{ ULONG t = ( (num. > denum) ? gcdfnum, denum) :gcd (denum, num));
num /=t; denura /-t;

Rational*; Rational: :operator+= (const Rational &b)


{ num = num*b. denum + b,num*denum;
denum = denum*b. denum;
(*this) .reduce () ;
return *this;
J
Rational^ Rational: :operator= (Rational b!
{ num = b.num; denura = b. denura; return *this; }
Rational Rational :: operator++ ()
{ return (*this +=1); }
Rational Rational: :operator++(int)
{ Rational t = *this; *this += 1; return t; }
// -------------- -----------------
Rational operator* (Rational a, Rational b)
{ Rational c;
c.num = a.num*b.num;
c. denura = a.denum*b.denum;
c. reduce (} ;
return c;
}
Rational operator+ (Rational a, Rational b)
( Rational t = a; return (t += b) ; }
bool operator == (Rational a, Rational b)
{ bool t = (a.num*b.denum != b.nura*a. denum) ; return (!t); }
ostreams operator(ostream& t, Rational d)
( (t '(' d.NumO '/' d. Denum () ')'); return t; }
istreams operator (istreamu t, RationalS a)
( t a.n a.d; return t; }


,
. , -
C++ .
:
class _
{ };
5. 323

,
. :
C++ "" ? ,
8.32 struct class
:
int main()
( Rational = 1, = 2; cout endl; return 0; }

. -
2248 :
' R a t i o n a l : : R a t i o n a l ' : cannot access private member declared in class

, , -
" " Rational. ! ,
.
. "" ,
, -
. ,
. -
,
.
public , private .
Rational,
. , -
, Rational.
. -
-
. "" -
, . . " "
Rational. \

,
public, Rational
( 8.34).

8.34. Rational

struct Rational
{ Rational(ULONG n = 0, ULONG d = 1):num(n), denum(d) { reduce(}; }
Rationals operator+=(Rational &b);
Rational^ operator=(Rational b);
Rational operator++();
Rational operator-- tint);
324 II. -

operator long() { return num/denum; )


private:
ULOMG num, denum;
ULONG gcd(ULONG x, ULONG y)
{ if (y == 0) return x; return gcdfy, x%y); }
void reduce();

Rational 8.35.

I 8. 35. Rational

class Rational
{ public :
Rational (ULONG n - 0, ULONG d = l ) : n u n ( n ) , denum (d) { r e d u c e d ; }
Rational^ operator+= (Rational &b) ;
Rational*; cperator= (Rational b) ;
Rational cperator++ ( ) ;
Rational operator++ (int) ;
operator l c n g ( ) { return num/denum; }
private:
ULONG num, denum;
ULONG gcd (ULONG x, ULONG y)
{ if {y == 0) return x; return gcd(y, x%y) ; }
void reduce ( ) ;

, public private,
, , .
. , , -
, . -
.
,
, . -
. :
int main{)
{ Rational = 1, = 2; cour A + endl; return 0; }

, -
- .
: .
,
. . , , -
8. 325

, ,
"". "" - - ,
. -
"", 8.36.
friend.

8.36. Rational, 2

typedef unsigned long ULONG;


class Rational
( public:
Rational (ULONG n = 0, ULONG d = l):num(n), denum(d) { reduced; I
Rational.5 operator+= (Rational sb) ;
RationalS operator^(Rational b) ;
Rational operator++();
Rational operator++(int) ;
operator long ) 1 return num/denum; }
friend bool operator (Rational a. Rational b) ;
friend ostreami. operator (ostrearnS t, Rational r) ;
friend istreamS operator (istreamfi t f Rational& a) ;
friend Rational operator*(Rational a. Rational b);
friend Rational operator+(Rational a. Rational b);
private:
ULONG num, denum;
ULONG gcd(ULONG x, ULONG y)
( if (y == 0) return x; else return gcd(y, x%y); }
void reduce();

. , ,
. " " [37].
, .
9

-
-
. , STL
C++.
. "
" .
.

; , -
.
.


, Pascal
. C++ " "
. -
,
,
,
.
, -
, ()
.


, -
.
328 II. -

array. , ,
. -
:
array (const int left, const int right);
,
. ; -
array. -.
array, -
. STL -
, :
. -
-.
. , -
, 0, 1 . ,
67 .
.
[], , .
[]
, .
.
, -
. -
. , -
, ,
.
, . .
, . , STL,
- assign,
, ,
(, )
.
, ,
.
, : -
, ,
.
-
. ,
: ,
,
.
9. 329

, -
. -
, .
- ( ) ,
! . , :
-.; II

,
, . ,
- -
, .
. -
(,
) , .
double. 9.1.

9.1.
class array
{ public:
typedsf unsigned int UINT;
array(UINT size = 10, double k = 0.0);
arraytint 1, int r, double k = 0.0);
array(const array &a) ;
arrayfconst array sa, int 1, int r) ;
arraytconst double * const begin, const double * const end);
-array () ;
doubles operator[](int index);
arrays operator=(const array &a);
arrays assign(const array Sa, int left, int right);
arrays assignfconst double *begin, const double *end);
arrays operator+=(const double sa);
arrays operator-=(const double Sa) ;
arrays operator*^[const double Sa) ;
arrays operator/^(const double sa);
int Left()const; //
int Right()const; //
QINT size()const; //
doubles max value();

doubles min_jvalue () ;
double summa () ;
double product!); //
void operator-{); //
double* find(double a); //
friend array operator+(const array&a, const array sb);
330 . -

friend array operator*{const array&a, const array &b);


friend double product_inner (const array&a, const array &b);
friend ostreamS operator (ostreamS to, const array Sa);
friend istreamS operator (istreamS to, array &a);
private: double* data; //
double dummy; //
int left, right; //
UINT size array; //

typedef ,
UINT unsigned int.
min, max ,
. find_yaiue
, , .


.
, data. -
, .
, -
.
. .
, -
- Destroy (),
delete[].
deleted: . C++
, -
. , .
, ,
~ (). ,
.
. ,
, delete. -
.
-
. 9.2.

| 9.2.

array::array(const double * const begin, const double * const end)


{ if (begin < end)
9. _ 337

i size_array = (end begin) ; //


left = 0; right = size__array 1; //
data = new double [5ize_arrayj ;
copy(begin, end, data) ;

array: : array (const array &a, int 1, ir.t r)


{ if ( (1 < r) ss (a. left <= 1) && (r <= a. right))
{ left = 1; right = r; size_array = (right - left) + 1;
data = new double [size__array] ;
copy(a.data + (1 - a. l e f t ) ,
a . data + (1 a. l e f t ) + size_array, data) ;

array: :~array{) { delete [] data; data =0; }

,
.
, inline-
. : -
(. 8.36)? -
, " ".
,
, . .
[37], " ".
, . .
delete . -
, - -
. , , -
.
-
array :
double [] = { 1, 2, 3, 4, 5, , 7, 8, 9 };
array A; // "" double [10]
array B(a, a + (sizeof (a) /sizeof (double) }); //
array D(a +3, + 7); //
array C(-2, +5); //
array F(-5, +5, 2.2); //
array G(B); //
array X(F,0,5); // array
array Z(20); //
array W(10u, -1.0); //
332 _ II. -


( 10), ( -
, -).
.
"" .
-
.
, .

(, ).
,
, . 10. -
,
. -
9.3.

! 9.3.
S ...................... .. ............... .. .......................................... . .................................... ....................................... .... ............. i.i --- i.;,_., .i,.; ..............................

doubles array: : operator [] (int index)


{ if (left <= index && index <= right} return data[i] ;
else return dummy;

, , ,
-

. , -
"" ( -
).


-
.
,
. "" ,
, . , . -
data
. = . data
A. d a t a ! :
B.size_array, -
, . ,
= ( ) ,
9. 333

(
9.4) .

- 9.4.

arrays a r r a y : :operator= (const array st)


( if { t h i s != fit) //
{ double *new_data = new double [t . size_array] ;
c o p y i t . d a t a , t.data + t.size_array, new_data) ;
delete[] data; //
data = new_data; //
left = t . l e f t ;
right = t. right;
size__array = t . size__array;
}
return *this;

|35J:
-,
swap , -
. 9.5.

9.5.

void Swap (array sother)


{ swapfdata, other. d a t a ) ;
swap (left, other. l e f t ) ;
swap(right, other. right) ;
swap(size__array, other. size_array) ;
}
arrays array: :operator= (const array & t )
( array t e r n p ( t ) ; Swap(temp); return *this; }

. -
:
, .
assign (
9.6).

| 9.6. assign

arrays array: rassign (const array St, int 1, int r)


{ array tempft, 1, r) ;
334 II. -

Swap(temp);
return *this;
}
arrays array::assign(const double *begin, const double *end)
{ array temp(begin, end);
Swap(temp);
return *this;
i
, -
.
, .
9.7. .

i 9.7. - array

void array: :operator~ ( )


{ sort (data, data + size_array) ; }
doubles array: :min_value (}
{ return *min_element (data, data + size_array) ;
double *array: : find_value (double v)
{ double *i = find (data, data + size_array, v) ;
if (I != data + size_array) return i;
else return 0;

- "". -
, -
. ,
9.8, :
, .

| 9.8. ""

array operator* (const array &a, const array &b)


{ if (a. s i z e ! ) == b . s i z e O )
{ array z ( a . size () ) ;
for (int i - 0; i < a . s i z e O f ++i) z [ i ] = a[i] * b [ i ] ;
return z;
}
else return a;

: -
, . . -
9. 335

BOB. , -
9.9.

.,

double array: : operator [] (int index) const


{ if {left <= index &S index <= right)
return data [index];
else return dummy;


, , z
, -
.
STL
9,10.

9.10.

ostreams operator (ostreams to, const array &a)


{ ostream_iterator<double, char> out (to, " ");
copy(a.data, a. data + a. size_array, out);
return to;
}
double product__inner (const array &a, const array &b}
( if (a. sized == b.sizeO)
( double z = innerjproduct (a. data, a.data+a.size_array, b.data, 0.0);
return z ;

,
, -
: , ?
. 11.

""
Matrix, -
. -
. -
(. . 4).
336 II -

. -
, ,
array ,
.
( 9.11).

.11, Matrix

class Matrix
{ public:
Matrix (int n = l r int m = 1, double k = 0.0); //
-Matrix (); //
//
private:
double **data; //
int n; int ; //
};
//
Matrix: :Matrix (int n, int m, double k):n(n), m(m)
{ int i, j;
data = new double* [n];
for (i = 0; i < n; -t-t-i) data ] = new double [m] ;
for (i = 0; i < n; ++i>
for (j =0; j < n; ++ j } data[i] [ j ] = k; //
}
//
Matrix : : ~Matrix ( )
( for (int i = 0; i < n; ++i} delete [] data[ij;
delete [] data;

:
1. . -
array. array
Matrix.
2. ,
: m [ i , j]
.
"": array -
"" Matrix:
friend class array;
9. 337

"" , , -
. Matrix -
, friend.
; ) .
:
doubles operator!)(int i r int j) { return data[i][j]; )
double operator!)(int i, int j) const { return data[i][j]; }


array -
.

J
, , -
.


Matrix A(10, 12];

:
A ( i , j) = 12;
double a - ( 0 , 0 ) ;

-
[49]. :
struct Index { int , ; Index(int x, int ):(), !){} };

:
doubles operator[](Index i) ( return data[i.x][i.]; }

,
.
:
A[Index(0, 1) ] = 123;

, -
.
-
C++, - -
A I I H J ] . ,
array. 9.12
Matrix .
338 II. -

; 9.12. Matrix array

class Matrix
( public:
Matrix(int n = 1, int m = 1, double k = 0 . 0 ) ;
-Matrix();
arrays operator[](int i) { return data[i]; }
private:
array *data;
int n;

Matrix::Matrix(int n, int m, double k):n(n}


{ data = new array[n];
for (int i = 0; i < n; -f+i)
for (int j = 0; j < n; ++j) datafi][j] = k;
1
Matrix::-Matrix(}{ delete[] data; }

!
, .
Matrix ( ) -
, array. ,
"" [ ], -
"" :
int main ()
( Matrix A(10, 10);
for {int i = 0; i < 10; ++i) A [ i ] [ i ] = 1;
for (i - 0; i < 10; ++i)
{ for (int j = 0; j < 10; ++j) cout A [ i ] [ j ] ' '; cout endl; }
A [ 0 ] [ 1 ] = 123;
for ;i - 0; i < 10; ++i) cout A[0][i] ' '; cout endi;
return 0;
1


, ( !).
:
array ; // "" double[10]
Matrix M(1G, 10);

- ,
:
[9. . _ 339

// i-
// j-

array.
array
Matrix, array .
: ^include. -
array Array. cpp
, Matrix. -
Matrix :
^include; "Array. cpp"

. "" "-
" .



,
, -
. , -
. :
struct tt ( string fio; unsigned long tel; };
:
tt[100000] ;

. ,
. STL -
, -
. -
, . -
, .
, :
tt& operator [] (const string &s)
( for (int i = 0; i < 100000; ++i)
if ( t t [ i ] . f i o == s) return tt[i];

-
- . , -
, -
, , -
340 II. -

,
.


array, , -
, .
,
(. . 4), , -
. -
, .
] ,
.

. , -
, ,
, ,
, .
, . -
, ()
. , -
.
,
. ,
. , -
, .
:
struct Node
( item; Node *next; Node *prev; };


Node, :
Nodeiconst & ) : i t e m ( { ) ) {}


.
STL (STL ) -
"" , -
. ,

, .
9. 341

"", -
"" . "" -
. .
, -
Node. , , -
Deque. Node -
Deque, "" Node.
, 9.13.

I 9.13.

template <class T>


class Deque
{ private:
class Node
{ friend class Deque;
Node(const & a ) : i t e m ( T ( a ) ) ( }
Node{){ }
-Node(){ }
item;
Node *next; //
Node *prev; //
};
Deques operator= (const Deque s.) ; //
Deque(const Deque 5 ] ; //
Node "Head; // ""
Node *Tail; // ""
long count; //
const dummy; //
public:
Deque():dummy((0))
{ Head = Tail = new Node; Tail -> next = Tail -> prev=0; count - 0; }
Deque(const T& a)
{ Head = Tail = new Node; Tail -> next = Tail -> prev=0;
push__front (a) ;
count = 1;
}
-Deque();
bool isEmpty!) const { bool t = (Head == Tail); return t; )
long size() ccnst { return count; }
const T frontt) const
{ if (lisEmptyt)) return Head -> item; else return dummy; }
const T back() const
342 //. -

{ if ( ! isErnpty ( ) ) return Tail -> prev -> item; else return dummy; }
void push_front(const &a) ;
void push_back(const & ) ;
void pop_front() ;
void pop_back();
void Print();

:
Deque dummy,
: front
back, . , -
, ;
-
Deque.
;
"" .

( ^
Deque Borland C++ Builder 6 Visual C++ 7.
Visual C++ 6, Node -
.

.
, .
, . -
, :
teraplate<class T>
Deque<T>: : -Deque ()
( Node *delete_Node = Head;
for (Node *p = Head; p != T a i l ; )
{ p = p -> next; delete deleteJSTode; delete_Node = p; }
delete delete_Node;
}

"" ( -
9.14).

| 9.14. ""

template<class T>
void Deque<T>:.:push_front (const T &a)
1 Node * = new Node { ) ;
9. _ 343

-> next = Head;


-> . prev = 0;
Head -> prev = ;
Head = ;
++count;
1
template<class T>
void Deque<T>: :pop_front ()
{ if (lisEmptyO)
{ Node *p = Head;
Head - Head -> next;
Head -> prev = 0;
count;
delete p;

Deque. -
"" ,
.

""
, : "" ""
(. . 4). [1],
, -
: .
, : -
. -
. ,
.
, ,
"" , . -
:
;
;
.
"-
" '( , smart pointers).
"" , , .
: -
.
9.15.
344 II. -
1
" " '
! 9.15. ""

template <class T>


class SmartPtr
{ public:
explicit SmartPtr (T* p) : pointer(p){}
SmartPtr(const SmartPtr &rhs);
-SmartPtr{);
SmartPtrs operator^(const SmartPtr &rhs);
operator* () const

return ^pointer;

T* operator-;-() const

return pointer;

private:
T -pointer;

, ""
. explicit, -
"-
".
, ,
.
, -
"" , :
SmartPtr<double> ip(C);

, 9.16,
.

I 9.16. ""

template<class T>
SmartPtr<T>::-SmartPtr()
{ if (!pointer) delete pointer; }

-
. ,
-,
9. _ __ _ 345

. *
, ->
( 9.17).

| 9. 17.

& operator* {) const


{ if (Ipointer} return ^pointer;
else return dummy;
}
T* operator->() const
{ if f Ipointer) return pointer;
else return sdummy; /

"" -
, "
:
if ( ! i p ) ...

. 9.18.

| 9.18.

bool operator! () const


{ return pointer = = 0 ; }

. -
" " .
, -
(destructive copy), -
STL.

auto_ptr
STL "" ,
auto_ptr. [24] . , ,
, -
( 9.19).
: ....... "" .................... V, ......... -"" ..... * ....... ,.,. .............................. - .......... ..,.,-.,... .................. ..... .^

\ 9. 19. auto_ptr

template <class T>


auto_ptr<T>: : auto_ptr (auto_ptr<T> &rhs)
346 II. -

{ pointer = rhs.pointer; //
rhs.pointer = 0; //
}
template <class T>
auto_ptr<T>& autojptr<T>: :operator= (auto_jptr<T> srhs).
{ if (this != &rhs)
( delete pointer; //
pointer = rhs.pointer; //
rhs.pointer = 0; //
}
return *this;
1
auto_ptr<T>::~auto_ptr()
i delete pointer; }

, -
. ,
. ,
. , -
"" -
.
.
[35|,
:
void f {)
( auto__ptr<T> p_t ( new T ) ; }

"", . .
auto_ptr, .
. ,
. -
, -
. auto_ptr
.
:
auto_ptr<T> f{)
{ return auto_ptr<T> pt ( new T); }

, "" ,
, : . -
-
.
10

,
, .
,
. , ,
. , . . 9
'
, -
-. , -
. -
-. -
- , .
, , :
,
- -. , -
, , -
.
,
. -
:
. , -
, -
- , -
, .


C++ , .
.
: throw (), try (), catch ()
348 II. -

. :
, ,
; ,
, "" .


.
, . . -
. - -
. ,
, , . -
, ,
. ,
, - ,
.
- , -
nporpaNfMHCTOM. , . . -
,
. - throw,
:
throw __;

"" __ -
, . :
throw ": !"; // 1
throw 13; / 7 2
throw s [ i ] ; /7 3 .

- , -
. ,
.
, :
throw 2*i f c value;

, ,
string s [ 2 3 4 ] ;

- -
. -
, -
.
--
, :
10. . 349

class ZeroArgument)};
ZeroArgument ex;
if (x > 0) double = l o g ( x ) ;
else throw ex;

, - -
:
throw new ZeroArgument{);

, , . . -
"".

( ^
- .

-
, -
.


try, -
"-" catch,
. try
,
:
try ( /* */ }

try
catch. "-" :
catch (__) { /* * / }

_ , -
, :
( ) // 1
{} // 2
(...) // 3

, -
, , ,
. -
"-" : , -
. -
. ,
350 . - ^

.
_ ( )
( ),
.
try ,
catch ,
catch. try -
, "-
". , -
. , catch -
, .
terminate.
catch -
throw .,
t r y . . .catch. ,
, -
. try...catch ,
try, catch:

try { // ,
try ( //
>
catchf...){ }
}
catch { //
try { //
}
catch{...){ }


-
. -
. , -
, , 1754. ,
, 1754, .
. -
-
10.1.
10. 351

10.1.

double f ( i n t k)
(try //
{ if (k > 1754) throw " > 1754!";
if (k < 0) throw " < '!";
if (k == 0) return 1;
else return k * f(k 1);
}
catch(const char *s) //
( cout ss endl; }

, .
cout f(5555) endl;

:
> 1754 !

.
Visual C++ 6 "-
" , "-
" I.#INF. , -
throw
. ? -
, -
. "-" , -
.
, -
, .
catch , .
. ""
main .
,
, . -
: , -
.
10.2.

! 10.2.

double f l ( i n t k)
{ if (k > 1754) throw " > 1754!";
352 //. -

if (k < ) throw " < ! " ;


if (k = 0) return 1;
else return k * f ( k 1);

int m a i n ( )
{ try 1 cout f (55555) endl; }
catch (const char 's)
{ cout ss endl; }
return 0;
}

,
.


-
. -
. ,
, -
. "-"
. , -
, -
.
-
. -
, .

( 10.3).

! 10.3.

struct ErrorTriangle
{ unsigned int a, b, c;
const char *message;
ErrorTriangle(unsigned int x, unsigned int y, unsigned int z,
const char *s): a(x), b(y), c[z), message(s) (}

double triangle{unsigned int x, unsigned int y, unsigned int z)


{ if {(x == 0) | I (y == 0) |[ (z 0))
throw new ErrorTriangle(x, y, z, " !");
if ((x + > z) (*& (x + z > y) && (y + z > x} )
{ double p = (x + + z)/2;
10. _ 353

return sqrt ( * ( - ) * ( - ] * ( - z) ) ;
}
else throw new ErrorTriangle(x, , z, "He !");

.
, -
. , -
. -
. , throw
-, . -

catch . -
10.4.

10.4. catch

int ma in ()
{ try { cout triangle(1, 1, 1) endl; )
catch {ErrorTriangle *e) //
{ cout e -> message ' ';
cout e -> a ';' e -> b ';' e -> endl;

return 0;
I


.
, -
.
, , :
void f l ( v o i d ) throw(const char *s, string;;
void f 2 ( v o i d ) throw IZeroDevide);

- ,
- zeroDevide.
:
void f I ( v o i d ) t h r o w ( ) ;

, .
,
.

12 . 1264
354 //. -

,
, -
. ,
, -
. ,
.
,
unexpected ( ) , terminate , , , -
abort!). .
, -
:
void F(void) //

unexpected ( ) :
set_unexpected(my_uriexpected);
terminated :
set_terminate (my__terminated) ;
-
.
uncaugirc_exfception(), -
true, , -
. -
.

,
, -

.
(. . 9) .
( array). -
-
, -
, .
, -
. :
class bad_Index {}; //
class bad_Range 1); //
class bad_Size {};


,
10. 355

, ,
oducthinner.
array, , -
, . -
10.5.

10.5. array

array::array(UINT size, double k)


( if (size > 0)
{ l e f t = 0;
right = size 1;
si?,e_array = size;
data = new double[size_array];
fill_ri (data, size_array, k) ;
1
else throw new bad_Size;
'
array::array(int 1, int r, double k)
{ if (1 < r)
{ size_array = (r 1} + 1;
left = 1;
right = r;
data = hew double[size_array];
fill_n(data, size_array, k);
}
else throw new bad_Range;
}
array::array(const array &a, int 1, int r)
{ if ((1 < r) ss (a.left <= 1) && (r <= a.right})
{ left = 1;
right = r;
size_array = (right left) + 1;
data = new double[size_array];
copy(a.data + (1 a.left),
a.data + (1 a.left) + size_array, data};
1
else throw new bad__Range;
}
doubles array::operator[](int index}
{ if (left <= index SS index <= right) return data[index];
else throw new badIndex;
356 _ II. -

double array: :operator[] (int index) const


( if (left <= index && index <= right) return data [index];
else throw new bad_Index;

double product_inner (const array &a, const array &b)


{ if ( a . s i z e ( ) == b . s i z e ( ) )
{ double z = inner jproduct (a. data, a. data + a. size_array,
b.data, 0 . 0 ) ;
return z;
}
else throw new array: :bad_Size;
}
array operator* (const array &a, const array &b)
{ if (a. sized b.size()]
( array z (a. size () ) ;
for (int i = 0; i < a.size(); ++i) z [i] = a[i] * b[i];
return z;
}
else throw new array: :bad_Size;

, , "" -
-, -
- array. ,
assign!) , . . -
.
bad_index, , :
double [] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
array (, + (sizeof(a) / sizeof (double) } ) ;
array G(B) ;
double aa;
int index = G.sizeO;
try { aa = product_inner (B, G) ;
mm: cout G [index] endl; // 1,
1
catch (array : :bad_Index *e)
{ cout "Index out of range!" endl;
index = 0;
goto mm;
1


array. ,
10. 357^

. 1 -
bad_index ( try -
), ,
0, try
. -
, , catch.

( }
Borland C++Builder 6
try, .

, , -
-
. -
, . -
, . -
.
, , -
(, ) .
.
, :
( catch ), -
;
( )
catch, -
;
, , -
,
" " (stack unwinding).


C++ , -
. , -
, . . ,
. -
: -
. C++, ,
. --
.
, 11, -
.
358 II. -

array
: bad_Range, bad_lndex bad_Size. -
.
array. -
. , ;
array_exception, /:
class array_exception {}; //
class bad__Range: public array_exceptioii { } ; // -
class bad__Index: public array_exception { } ; // -
class bad_Size : public array_exception { } ; // -

,
10.6.

10.6.

class exception { / / . . . };
class logic_error : public exception { / / . . . };
class domain_error : public logic_error ( / / . . . };
class invalid_argument : public logic_error { / / . . . };
class length_error : public logic_error { / / . . . } ;
class out_of_range : public logic__error { / / . . . | ;
class runtime_error : public exception { / / . . . } ;
class range_error : public runtime_error { / / . . . };
class overflow_error : public runtime_error { II...}
class underflow_error : public runtirr,e__error { / / . . . } ;
class bad_cast : public exception { //... };
class bad__alloc : public exception { / / . . . };
class bad_tipeid : public exception { II... } ;
class bad_exception : public exception ( II... } ;
class i s s _ b a s e : : f a i l u r e : public exception { II... };
.

. -
, exception. -
:
#include <stdexcept>

, try "--
" catch.
catch : ( try)
, .
catch .
10. 359

:
bad_aiicc, new
;
bad_cast bad_typeid, RTTI
(Run-Time Type Information! . . //);
runtime_error, - -
.
- what ,

Borland C++ Builder 6. 10.7.

! 10.7. what

^include <stciexcept>
ttinclude <iostream>
^include <string>
using namespace std;
void f ( ! f throw runtime_error("a runtime e r r o r " ) ; }
int main (}
{ string s;
try { s.replaceddd, 1, 1, ' c ' ) ; }
catch (const exceptions, e)
{ cout "Got an exception: " e.whatO endl; }
try { f () ; }
catch {const exceptions e)
{ cout "Got an exception: " e.whatO endl; }
return 0;
\

:
Got an exception: basic__string
Got an exception: a runtime error

-
replace ,
runtime_error f.
catch;
, -
- -
. -
. 11.
11

: -
. , -
, .
C++ , -
.
. C++
.
: , ,
.
- () -
(). - "" (
), - (
). C++ , -
.
.
i

.
, . -
, :
, .


,
.
:
class __: [_] _5__
{ }
362 II. -

- ( ) (
-) . -
-. -
, . -
.
: public ()
private (). - protected
(). ,
( ), ,
. -
. , -
: , -
(public).
, (private)
.
, , , .
" "
protected.
: , -
, .
,
- .
. 11.1 -
.

11.1.
-


struct class

public public private


protected public private
private
public public public public
protected public protected protected
private public
public protected protected protected
protected protected protected protected
private protected
11. 363

11.1 ()


struct class
public private private private
protected private private private
private private

, ,
, . .
public, -
. , -
protected , private
.



. -
.
"":
- "" -, .
. [23] , "
- C++ -
: " ".
, Derived ()
Base (), ,
Derived Base. ! -
, , .
- ,
. Windows,
, .
, , ,
Derived. - ,
11.1, .

11.1- ""

class Clock { . . . ) ; //
class Alarm: public Clock {...}; //
void settime{Clock &c, time t); //
364 . -

void setalarm (Alarm &a, time t ) ; //


Clock C; // -
Alarm A; // -
settimefC, t ) ; //
settime (A, t ) ; // -
setalarm(A r t ) ; //
setalarm(C, t ) ; // :

, -
.
-
: -
, .
Base 11.2.

11.2.

class Base
( public:
Base () :a(-l) {)
void Print{} { cout a ';'; }
private: int a;

>'
class D01: Base
{ public:
void Print(){ cout b endl; }
void set(float b) { this -> b = b; }
private: float b;
};
class D02: Base
{ public:
void Print() { Base::Print(); cout a endl; }
void set(int a) { this -> a = a; }
private: int a;
I;
-
print , .
: DOI D02. DOI float
print ( ) . , set .
- D02 : -
, -
. ,
11. 365

- .
- -
, ::.
-
. D02 P r i n t ; ) ,
. -

void Print()
{ Print{); cout endl; }

. -
Print!) D02 :
, . Print DOI
DOI.


, , -
, .
.
,
. 11.2.
-
.
: , -
,
.
-, . -
, 11.3.

; 11.3.
;..'.:... ,.... ;; ._ .-. . .;...,u.i.^u.i
class Point2D
{ public:
// Point2D(): x(O.O), y{0.0){) // A
Point2D{double x, double y): x(x), y(y) {}
Point2D(const Point2D fit): x(t.x), y(t.y) (}
void Print() const { cout '<' x ',' >'
double getx() const { return x; }
double getyO const { return y; }
private:
double x, y;
366 It. -
f
class Point3D: public Point2D
{ public:
Point3D(double x, double y, double z) : Point2D(x, y) , z ( z ) U
double g e t z ( ) const ( return z; }
private:
double z;
i,
" " Point2D,
:
. " "
Pointso , -
-
. -
-:
Point3D(double x, double , double z) : z(z) { Point2D(x, ); }

, -
-. , . . ,
. (protected)
- ,
:
Point3D(double x, double y, double z):z(z)
( this -> x = x; this ~> = ; }

, , -
. , -

Point2D ;
Point3D b;

. -
( ), -
,
:
Point2D a; //
Point3D b; // -

. -
, -
. :
Point2D b(l, 2) ;
Point3D d(3, 4, 5) ;
Jf. 367

//
//

,
. -
"": ,
.
, .
"", ,
-
. , -
:
d.operator=(b);

, -
, ,
. ,
.
,
getxo g e t y O , ( ):
Point3D d ( l , 2, 3) ;
cout d . g e t x t ) ' , ' d . g e t y O ' , ' d.getz {) endl;

:
1 2, 3

, -
. -, , , -
;
. -, -
, -
. -, -
:
, . ( )
. ,
11.4.
: -"" " '"_ ' """

11.4.

class
{ public: Base () { cout "Base " endl; }
~Base() 1 cout "-Basel)" endl; )
368 !!. -

class Derived: public Base


{ public: Derived!) { cout " D e r i v e d f ) " endl; }
-Derived!) { cout "-Derived!)" endl; }
\
int main{)
{ derived a;
return 0;

:
Base ()
Derived()
-Derived()
-Base()

, "" "" LIFO:


" ".


, C++
" ". ,

, , (. 11.1).
,
:
class Clock { . . . ) ; //
class Alarm: private Clock { . . . } ; //
void settime(Clock &c, time t ) ; //
Clock ; // -
Alarm A; // -
settime(, t ) ; //
settime(A, t}; // -

, Alarm
Clock. , .
,
settime(reinterpret_cast<Clock &> () , t); //
, , -
.


static__cast "" ,
Alarm Clock "".
11. 369

D , ,
D . -
" "
. , -
, , , , .
:
class Date { . . . } ; //
class Address { . . . } ; //
class Person {
public: ...
private:
string name; //
Date birthday; //
Address address; //
iJ /.

, Person
Date, Addres string. He -
(. 9.11). ""
"" "".
,
, , , "
"? . 9 -
. C++,
STL . -
"" , . .
-
. [23],
,
. :
0 , ;
- -
, .
, , ? -
, 11.5.

1 11.5.
class GenegicStack
{ protected: // " "
GenericStack(} ;
-GenericStack() ;
370 //. -

void push (void * o b j ) ; //


void * pop(); //
bool empty() const; // ?
private:
struct Node ( //
void * data; //
Node * next; //
Node (void * Data, Node * Next)
:data(Data), next(Next) {} //
1;
Node * top; // ""
//
GenericStack(const GenericStack &rhs);
GenericStack7 operator=( const GenericStack srhs);

template <class T>


class Stack: private GenericStack
{ public:
void push (T * obj) { GenericStack ::push(object); ) //
* pop ()
{ return static_cast<T*>(GenericStack::pop()); } //
bool empty(} const { return GenericStack::empty(); } // ?

- (GenericStack)
-,
. -, -
. ,
:
, -
. [23], "
, ,
".
.

C++ .
( ),
, -
, .
. ,
, 11.6,
Base print - Derived,
.
11. 371

11.6.

class Base
{ public:
Base(int x): a(x) {}
void Print()const { cout a ';'; }
private:
int a;

class Derived: public Base


{ public:
Derived (int x, float y): Base(x), b(y! {}
void Print()const { cout b ';'; }
private:
float b;

:
Base o b ( l ) , *pb; //
Derived(2, 2 . 0 ) , *pd; //
, :

,
, . , ,
,
.
"" ,
.
, -
:
pb = new Base{2); // ->
pb -> Print(); // Base::Print()
pb - new Derived(3, 3.0); // ->
pb -> Print(); // Base::Print()

, ,
(). , -
: .
Shape () Ellipse () Rectangle
(). , -
, ,
.
372 II. -

, -
:
((Derived *)pb) -> Print();

static cast<Derived *>(pb) -> Print();

v
)
static__cast , . . "",
.

, . . -
,
.
-
-.
11.7.

! 11.7.

class Base
{ enum Object_type { , D }; //
public:
Base () : type () {} //
//...
private: II...
Object_type type; //
};
class Derived: public Base
( public:
Derived()( type = D; } //

:
void Print (const Base *po)
{ switch (po -> type)
{ case E a s e : : D : //
case Base::B: //
break;
11. 373

break -
, . . .
. -
,
. -
, .
--
,
.
-
, C++ -
-. -
, virtual,
.
print , 11.6:
virtual void Print() const //
{ cout a '; '; }
virtual void Printf) const //
{ Base::Print (); cout b ';'; }

virtual, -
.
pb = new (2); // ->
pb -> Print ; // : :Print (}
pb = new Derived{3, 3.0); // ->
pb -> Print(); // Derived::Print()

, : -
, -
.
: :
\
delete pb;
, -
: -
.
-
:
virtual -Base(){};


, -
.
374 II. -

.
1. .
2. - -
.
3. ; , .
4. , -
( -
) (
virtual ) -
.
5. .
6. ( ) -
.
, -
C++. [1] , C++
:
(compile-time polymorphism ~
),
;
(run-time polymorphism -
), .
, , -
.

RTTI dynamic_cast
,
: , .
-
, , -
, . C++ , .
-
RTTI (Run-Time Type Information -
).
'
dynamic_cast,
.

1
"" J37],
11. 375

, "", . -
(
)
static_cast reinterpret_cast. ,
. -
:
dynamic_cast<TMri*> ()
.
.
,
- - ,
.
( ), . , -
.
. [37], dynamic_cast<T*>(p) -
: ", -
, ?" dynamic_cast<T&> (r),
. , * , : ",
, ." , -
, -
. "" .
C++, -
, typeido.
, <typeinfo>. -
type_info,
. -
typeido ,
Borland C++
Builder 6. -
11.8

' 11.8. typeid

^include <iostream>
^include <typeinfoiop>
tinclude <conio.h> // getchf)
using std::cout;
using std::endl; -
class A //
{ virtual void f(void){},; } ;
class B: public A // -
{ virtual void f(void)O; };
376 II. -

void main () {
char ;
float X;
// type_info::operator^()
if (typeid(C) = = typeid(X))
cout " X ." endl;
else cout " X ." endl;
// : "double" < "int"
cout " " typeid(A).name(] ":"
(typeid(A).before(typeid{A)) ? true : false) endl;
A a, *pA;
b, *pB;
pA - a;
cout typeid!*pA).name(); //
pB = Sb;
cout typeid(*pB) .name () ; //
pA = Sb;
cout typeid("pA) .name () ,- //
getcn (}; //

: -
.
, . -
, , ,
. . -
.
,
. -
.


, ,
. : -
. -
.
, . (-
60 60 , -
). -
. , -
, ?
.
11, 377

-
. C++ -
. ,
.
. -
( ,
) -
().
-,
.


-
linker ().

,
. s i n ( x ) ,
,
, .

.
,
.
. , -
f. -
,
. -
,
, , , .

,
(
extern).
, , -
.
static. , C++ -
. -, -
static .
,
, . , static
private ( ). ,
, - ml.cpp 2.
378 //. - ^

( , , ,
):
// ml.cpp
void f1(void) {...); //
static void f2(void) //
// m2.cpp
fit); //
f2(); //

static -
, . , -
, (
static) ( ,
). ,
fi 2.
, ml.cpp:
// ml.cpp
void fl(void) {...}; //
static void f 2(void)(...}; //
fl(>; //
// m2.cpp
static void f1(void) {...}; //
fl(); //
f2(); //
// m3.cpp
static void f2(void) {...}; //
fit); //
f2(); //

, : static
,
main. "": Java
main,
. ,
" " :
main.
extern , -
. :
// ml.cpp
int = 1; //
void f(void); //
int main()
11. 379

( f(); // 1
= 2
f () ; // 2
return 0;

// 2.
extern int x; //
void f(void) //
{ cout endl; }

ml.cpp ,
f, 2..
(extern) -
( ). , -
extern , . .
extern . , static
.


. 2,
(). -
"".
. ,
, .
. , if
, .
. , ,
.
. ;
.
. , -
.
. , -
.
. C++ ,
.
,
.
380 II. -


static ,
. C++ -
, . -
C++ ( , -
, ). -
std.
.
-
, .
:
namespace
{ // }

namespace .
, , ,
, . . . [37, . 211],
.
-
, -
. , , /, -
.
( ) -
:
_::


. ,
, , ;
std::ccut

:
std::endl

, -
using:
using __::;

using!
using namespace _;

using , namespace, .
-
11. 381

.
, -
( )
. ,
.
, -
. . ,
, . ,
C++ ( ):
namespace DCS = Departament_of_Computer_Science;
i
.
,
"" .
std. ,
. [37, . 228], ""
:
// ml.cpp
namespace A {
void fl(void) {...};
int x;
I
// m2.cpp
namespace A (
void f2(void) {...};
int y;
class A{};


. ,
:
namespace A {
void fl(void) {...};
int x;
void f2(void) {...};
int ;
class A{};
}

: . .
, ,
.
382 _ !1. -

.
, -
, , , :
void A: : f l (void) {...};

.
:
namespace Outer { //
int i;
namespace Inner { //
void f() ( i++; } // Outer: :i
int i;
void g() { i++; } // Inner::!;

Borland C++ Builder 6 -


, :
namespace BORLAND_SOFTWARE_CORPORATION { //
/* namespace */
namespace NESTED__BORLAND_SOFTWARE_CORTORATION ) //
/* namespace */

// namespace
namespace BI = BORLAHD_3OFTWARE_CORPORATION; //
// ,
namespace NBI =
BORLAND_SOFTWARE_CORPORATION: :NESTEDJ30RLAND_SOETWARE_CORPORATION;

,
.


,
, .
, -
: : . , -
-
, . .
. , -
, Windows (,
windef.h):
: :max (5, 6)
11. __ _ _ 383

static C++
() . -
, . .
. -
Borland C++ Borland 6 , -
Visual C++ 6.
1 1.9.

I 11.9.

// ml.cpp
ttinclude <iostream>
extern void func (void) ; //
namespace f //
float pi ^ 3.1415926F; // pi
)
int main ( ) f
// float pi = 0.1; // 1:
std::cout "pi = " pi std::endl;
func ( } ;
return 0;
I
/ / ml . cpp
ttinclude <iostream>
namespace ( // namespace
float pi = 10.0001F; // pi
void f(void) { //
std::cout "First func() called; pi = " pistd: : endl;

void func (void) { //


ft);
std::cout "Second f unc { ) called; pi = " pi;

:
pi = 3.14159
First func () called; pi - 10.0001
Second funci) called; pi = 10.0001

ml.cpp , -
pi. main 1 , -
pi . ,
384 II. -

-
( pi ).
, -
.
2. ,
pi, .
2. f t ; . -
f u n c ( ) , -
f < ) . ml.cpp -
, .
ml.cpp f f ) , pi
2..


,
. , "-
", -

. (-
, ), --
/ .
ttindude. ,
#include "_"

.
C++, . . -
. .
:
^include <_>


include. -
, -
.

, -
,
. :
"" .
^define .
:
11. 385

// myfile.h
Hfndef MYFILE // "" ?
#define MYFILE //
//
#endif

,
^include "myfile.h"
MYFILE ("") .
. , "" -
. -
. ,
math.h Borland C++ 5 :
/* math.h
Definitions for the math floating point package.
*/
/*
* C/C++ Run Time Library - Version 8.0
*
* Copyright (c) 1987, 1997 by Borland International
* All Rights Reserved.
*
*/
/* $Revision: 8.10 S */
flifndef _MATH_H
ttdefine _MATH_H

, "" ___.
Visual C++ 6 :
/***
*math.h definitions and declarations for math library
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* This file contains constant definitions and external subroutine
* declarations for the math subroutine library.
* [ANSI/System V]
*
* [Public]

13 . 1264
386 . -

#if _MSC_VER > 1000


#pragma once
#endif

ttifndef _INC_MATH
#define INC MATH

"" _INC_MATH.
-
/ , , , ,
, , , -
, .
.
12

.

. -
C++ , .
,
. .


.
STL.
, C++ -
[38]. -

C++ . ,
10 , ,
, . [37]
. ,
C++: , -
- .
.
,

" ", -
" ".
. , -
.
-
,
385 It. -

. , -

[10] .

- .
.

. 4 ,
(. 4.18), -
(. . 7). , -
, -.
- , . -
C++
- . -
, -
:
(* )( );


int (*fp) < i n t ) ;

fp , int -
int. ,

int *fp(int);

-
int, int.

; .
. :
void fl(void){ }; //
void f2(void){ }; //
void f*pf)(void); //
pf = fl; // fl
pf = f2; // f2

, .
:
12. 359

void f I ( v o i d ) { }; //
void (*pf)(void) = f l ; //

:
(* )( );

:
( );

, pf :
(*pf) ;
:
pf ( ) ;

, , -
. -
, 12 (Visual C++ 6).

I 12.1.

void f ( i n t k)
f static void (*g)(int k) = f; //
if (k > 0) //
{ cout "call " k endl;
g(k-l); //
cout "return" k endl;
.
}

,
. f ( 3 )
:
call 3
call 2
call 1
return 1
return 2
return 3

( ) -
.
,
C++ .
390 II. -

, typedef, .
. :
float (*p[5])(float);

(Visual C++ 6), 12.2.
j.._:,........,,..,......
| 12.2.

double f(double) // ""


{ cout 'f endl; return 0; }
int main(void)
{ double f*p[3])(double); //
[0] = sin;
[1] = cos; //
[2] = f; //
cout [0](1) endl; // sin(l.O)
cout p[l](1) endl; // cos(1.0)
cout [2](1) endl; // f(1.0)
return 0;

, :
0.841471
0.540302
f

-
, typedef. -
typedef
, , :
typedef >(*<_>)(< >);
,
typedef float (*PF)(float);
PF, :
PF pi; //
PF 2[5]; // - float (float)

. -
typedef , int,
:
int(*function)(int *)
12. 391

, . typedef -
:
typedef int ('function)(int *};
function t (int);
_
typedef "
":
int (*t ( i n t ) ) (int*};

( ) function
typedef ,
. tt, -
i n t ( * f ) (int, int*)
(int*, int). , :
int (*tt(int*, i n t ) ) ( i n t , int*)

Visual C++ 6 .
, typedef,
. , ,
typedef, " " -

. ,
typedef . -
:
void f(void)
{ typedef int (*t)(int); } //
int main(void)
{ t a; } // t



, . . , -
. stdlib.h -
, :
, , . -
:
void qsort(void *base, //
size t num, //
size_t width, //
int (*compare)(const void *eleml, const void *elem2));
392 _ II. -

, ,
. -
void *. void C++ ,
.
( ),
.

1, -1 . . [37]
,
. :
struct User
{ char *name; //
int dept; //
}
User V[100] ;

-
:
int crr.pS (const void* m, const void* n)
{ return strcmp (static_cast<const User*> (m) ->name,
static_cast<const User*> (n) ->name) ;

:
int cmpD (const void* m, const void* n)
{ return (static_cast<const User*> (m) ->dept //
static_cast<const User*> (n) ->dept) ;

"" -1, , 1, , -
, . , , ,
: ( (User * ) m ) .
:
qsort ( (void *) V, 100, sizeof (CJser) , cmpS) ;

:
qsort ( (void *) V, 100, sizeof (User) , cmpD) ;

,
. . , -
- -
, C++ -
, .
12. 393

-
PF , -
f ( ) = [, ], :
double R(PF f, double a, double b, double epsilon)

, epsilon .
= - 1.5+ 1.2 [0, 2]
10~5 :
float f(float x) //
( return (** - 1.5* +1.2); }
int main(void)
( double result = R(f, 0.0, 2.0, 0.000001); }

- -
. , , :
double R(PF f - cos, double a = 0.0, double = 2.0,
double epsilon = 10e-5)

:
double = R ( ) ;

-
:
double R ( P F & f , double a, double b, double epsilon); //
double R ( P F *f, double a, double b, double epsilon); //


f (),
:
(*f}();

, -
, :
double R(const PF &f, double a, double b, double epsilon);


,
C++.
.
I. -
?
394 II. -

2. ?
3. " ", -
?
:
. .
.
, . -
.
, :
void f(double m[], int n)
-
. ,
, : NULL
. -
. , -
. , "-
" , ""
, . ,
"" . ,
Intel 4 ,
. , -
, Intel . -
-
. , 12.3,
Visual C++ 6, Borland C++ 5.
: ' " ' " ;

! 12.3.

void fl(double b [ ] , int k) //


{ for (int i - 0;i < k; i++) b[i] += 1; }
void f2(double b[], int k) //
{ for (int i = 0;i < k; i++) b[i] *= 2; }
void F(double a[], int n, void (*f){double m[], int n)...)
{ typedef void (*pf) (double rn[], int n) ;
double **pp = (double **)f; // ""
while (*pp != NULL) // NULL
{ pf *p = (pf*)pp; // ""
(*p)(a, n); //
pp++; //
}
\
int main(void)
12. 395

{ double V [ 1 0 j ;
for (int i = 0; i < 10; ++i) V [ i ] = i;
for (i = 0 ; i < 10; ++i) cout V [ i ] ' ';
cout endl;
F ( V , 10, f l , f2, 0 ) ; // ""
for fi = 0; i < 10; --i) cout V [ i ] ' ';
return 0;

: -
1, .
;
0 1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18 20

F
:
double ** = (double * * ) & f
, 4 . ,
& , . .
, ;
HULL , , *
;
"" , -
. pf *p = (pf *)pp
-- ""
"" ;
"" "-
" (*) (, ) . , . . ""
-
;
"" , ""
.
, -
.
C++:
double ** = reinterpret_cast<double **>(sf);
pf *p = reinterpret_cast<pf *>(pp);

, -
,
396 _ II. -

. , , -
:
. ,
, -
.
.

, -
-.
va_. . .
:
enum typeFunc { typel = 1, type2 = 2, end = 0 };

, , :
void fl (double b[], int k, int n) //
( for (int i - 0; i < k; i++) b[i] += n; }
void f 2 (double b[], int k) //
{ for (int i = 0; i < k; i++) b[i] *= 2; }

,
. , -
,
. --
, .

12.4.

[ 12,4.

void ff (double a [ ] , int n, typeFunc tp [ ] , . . . }


{ //
typedef void (*pfl) (double r n [ ] , int n, int k) ;
typedef void (*pf2) (double m [ ] , int n} ;
va_list p; //
va_start(p, tp) ; //
for (int i = 0; tp[i] ; i++) // !=
{ switch ( t p [ i ] )
{ case typel: // 1
{ p f l pp = va_arg(p, p f l ) ; //
pp (a, n, 3) ; //
break;

case type2: // 2
12. _ 397

( pf2 = va arg(p, p f 2 ) ; //

(, ) ; //
break;

va_end(p) ; // ""
}

,
"" . ,

:
(pfl (va_arg(p, pf 1) ) ) (a, n, 3);
:
typeFunc tp[3] = { typel, type2, end } ; //
:
ff(V, 10, tp, f l , f2); //
NULL , . . -
.
int main (void)
( double V[10]; //
for (int i - 0; i < 10; ++i)
V[i] = i; //
for (i =0; i < 10; ++i)
cout V[i] ' '; //
cout endl;
typeFunc tp[3] = ( typel, type2, end } ; //
ff(V, 10, tp, "fl, f2); //
for (i = 0; i < 10; ++i)
cout V[i] ' '; // -
cout endl;
return 0;
"
:
0 1 2 3 4 5 6 7 8 9
6 8 10 12 14 16 18 20 22 24

-
,
.
395 II. -


.
-,
typedef , -
, . -
, " ":
" ". [35]
: , ,
" " .
-
. "-
" [47J.

: , , -
.
.
-
, . -
, , .
. .
, ,
, . -
: "-
" .
, -
, .
, -
" ", , -
(. . 1). . 12.1.
4 : "", " ", "" "".
"". -
, " ". -
, -
, -
"". ""
, - .
( , )
: -
.

-. 12.5 (
^include namespace). -
12. 399

, " "
.

. 12.1. ,

12.5.

int main(void)
( string s = "il23456789"; //
enum States //
{ Start,
NextLetter, //
Error, //
Finish
1;
States x = Start; //
int i = 0;
while((x != Finish) && (x != Error)) //
{ switch(x) //
{ case Start:
if (isLetter(s[i]JJ{ i++; x = NextLetter; }
else x = Error;
break;
case NextLetter:
if (isNextLetter(s[ij)) { i++; x - NextLetter; }
else x = Finish;
break;
case Error:
cout s " !" end!;
break;
400 _ //. -

cout s endl;
return 0;

isLetter isNextLetter , -
.
( ), -
, .
,
" ", .
-, typedef, -
: " " -

:
typedef function (*function)
C++ "" -
:
typedef void !*fp)(const char ch);
typedef fp (*States)(const char ch) ;

. typedef,
.
- -. -
return -
. , -
12.6.

I 12.6.

fp Finish(const char ch)


{ return (fp)NULL; } //
fp Error(const char ch)
{ cout ch " !" endl;
return (fp)NULL; //
}
fp NextLetter(const char ch)
{ if (isNextLetter(ch)} return (fp)NextLetter;
else return (fp)Finish;

fp Start(const char ch)


{ if (isLetter(ch)) return (fp)NextLetter;
else return (fp)Error;
12. _ 401

int main (void)


{ string s ="112345.";
States x = Start; //
int i = 0;
while (x != NULL}
( x = ( S t a t e s ) x ( s [i++] ) ; } //
cout s endl;
return 0;

:
return - "" -
, -
" ";
"" x - (states)x(s[i++]),
;
, , NULL
Finish Error. Finish
, . ,
"" (, , -
) , ,
.
-
. , ,
"" . ,
switch ( , )
. -
( )
-, .
(. 12.6) .
. -
. , ,
. -
, " "
.

-

, -
C++. , -
. ,
402 II. -

. 5,
-, .
.
- ,
( ) . , -
, . -
( ) .
-
, . ,
( )
. - -
.
(. 5.16) - -
( - 12.7).

I 12.7. -
; .'.... ;

struct isPrime (
bool operator() (int number) const
{ number = abs(number);
if (number == 0 I | number == 1) return false;
int divisor;
for (divisor = number/2; number%divisor != 0; divisor);
return divisor == 1;
}

- :
list<int> L;
list<int>::iterator pos; //
// 20 40
for (int i - 20; i <= 40; ++i) L.push_back(i);
pos = find_if (L.beginO, L.endO, isPrimeO); //
if (pos != L . e n d O ) cout *pos " " endl;
else cout " " endl;
//
cout count__if (L.beginO, L . e n d O , isPrimeO) endl;

-
. - -
.
-
, -
12. _ 403

.
,

.
,
. -
, -
(. 11.5).
[1, 34, 49].
,
.
, , -
.
, , -
"- -
".
" " ,
. -
, , .
-
(, , -
).
Person (. . " " . II).
class Person (
public : . . .
private :
string name; //
Date date; //
Address address; //
int dep; //
int children; //
bool sex; //

:
vector<Person> V(100) ;

-,
, , . -
, -: ,
, . -
Person, .
- ,
. lessthan ( 12.8).
404 II. -

12.8. lessthan

template <class type_elem>


class lessthan (
public:
less (type_elem v) ; v_(v) {} //
bool operator!) ( type_elem v)
{ return v < v_; }
private:
type_elera v_;

(equaisto ,
greaterthen )
.


, -
, STL

, -
.
- :
template <class T>
vector <Person> filter{vector <Person> &v, lessthan<T>(x));

.
, -
. , -
, - -
. ,
: ,
, , .
12.9.

12.9. -

template <class type_eleni, typename Comp = equalsto<type_elem> >


class Compares
{ public:
Compares(const type_elem& v a l ) : v _ ( v a l ) { }
bcol operator!) ( type_elem val) { return Comp(val); }
private:
type_elem v_;
I;
2. 405

- :
template <class , class Cornp >
vector<Person> filter (const vector<Person> &w, Compares<T, Comp>(x))

:
vector<Person> {10);
bb = filter(bb, Compares<int>(3));

:
bb - filter{, Compares<Date, lessthen<Date> >(Date(22, 02, 1953)));


- , -

. STL : unary_function
binary_function. " ",
. -
. 12.10
.

I 12.10. -

#include <functional>
template <class Arg, class Result>
struct unary_function {
typeclef Arg argument_type;
typedef Result result__type;
};
template <class Argl, class Arg2, class Result>
struct binary_function {
typedef Argl first__argument_type;
typedef Arg2 second argument type;
i
typedef Result result_type;
};
template <class _Tp>
struct equal_to : public binary_function<_Tp,_Tp, bool>
{ bool operator()(const _Tp& x, const _Tp& y! const f return x == y; }
1;
template <class _Tp>
struct plus : public binary_function<_Tp,_Tp,_Tp>
{ _Tp operator () (const _Tp& x, const _Tp& y) const { return x + y; }
406 _ . -

template <class _Tp>


struct negate : public unary__function<_Tp,_Tp>
{__Tp operator ( ) (const _Tp x) const { return -x; }
};
template <class _Tp>
struct logical_and : public binary_function<_Tp,_Tp,bool>
{ bool operator (} (const _JTp& x, const _?& y) const { return x && y; }
};
template <class _Tp>
struct logical_not : public unary__function<_Tp, bool>
{ bool operator () (const _Tp& x) const ( return ! _ x; }
);

, -
: , . -
,
,
( ). -
, . -
Borland C++ Builder ,
12.11 .

; 12.11. -

template<class Arg>
class factorial : public unary_function<Arg, Arg>
{ public:
Arg operator () (const Arg6 arg)
{ Arg a = 1;
for (Arg i = 2; i <= arg; i++) a *= i;
return a;

-, . -
, , :
int init[7] - { 1, 2, 3, 4, "5, , 7 };
deque<int> d(init, init + 7) ; // -
vector<int> v(7); // -
transform(d.begint) , d.endt ) , v. begin (} , factorial<int> f) ) ;

transform .
-
-: (). -
12, 407

, -
.
-, , -
. ,
. ,
100 - v -
(. 12.10) find_if. -
:
bind2nd(equal_to<int>, 100)


.
- , -
!.
,
. : -

.
:
! ( < 100)

:
notl(bind2nd(les5<int>, 100))

r.ot2.

-

, - -
-~ [10]
.
- (. . 9), -
, . ""
[10], 23 . -
, -
-
. -
-
.
, singleton (""). "-
" , -
408 //. -

,
. . -
, -
. Windows

. .
"" ,
. , -
, ,
. -
.
. ""
12,12.
; """' : ' - ,"". ; -' :"<
| 12.12. singleton

class Singleton
{ public:
static Singleton * Instance(); //
private:
Singleton(); //
Singleton(const Singleton &sg) ;
Singletons operator^(const Singleton &sg) ; //
-Singleton ); //
static Singleton * instance^;
};
Singleton * Singleton::instance_= 0; //
Singleton * Singleton::Instance!)
{ if (instance^ == 0) instance_ = new Sigleton();
return instance ;

-. -
singleton
, -:
Singleton *p = Instance ;

- -
.
Adapter,
. -
.
12. ^ _ 409

, -
, ,
. -
. . 9 -.
-
: .
, .
-
. -
, ,
. C++ :
class Adapter: public Queue, private Deque


, ( -
) "" .
" ", .
, :
virtual void f (void) = 0;
,
, . -
,
.
, -
, " "
. " ", -
.
. -
,
. ,
shape (),
-.
window (), -
.
.
,
"" . -
[4], -
Windows.

410 II. -

, -
, .
, .
- 12.13.

| 12.13.

template <class T>


cla.ss Queue
( public:
class EmptyQuee}}; //
explicit Queue'); //
bool empty() const = 0;
const front!) const = 0;
const backf) const = 0;
void push_back(const &) =0;
void pop_front() = 0;
I;
. - -
, -
.

1994 , ,
(Erwin Unruh)
. , -
, . -
-
, -
.
:
, 2 . ,
, ,
:
double pow2(const int n)
{ double n2 = 1;
for (int i = 1; i < n+1; ++i) n2 *= 2;
return n2;
12. 411

:
cout pow2(0) endl;
cout pow2(10) endl;

. ,
, -
.
, .
,
,
, - , . -
.
, -
.
: -
, .
"" , ,
, "" . -
, "-
"
, .
,
. , -
, .
:
. -
. ,
, ,
.
, - -
. : -
. C++ . , -
- (
).
-
. :
2= 1
2" = 2 2" - 1
, -
. , -
, , " ".
12.14.
412 -

\ 12.14.

tfifndef POW2_HPP
#define POW2_HPP
// 2 N
ternplate<int N>
class Pow2 {
public: enuiti { result = 2 * Pow2<N-l>: : result };
};
//
templateo
class Pow2<0> {
public: enum { result = _ 1 };
};
ttendif // POW2_HPP

, -
. , :
^include <iostream>
#include "pow2.hpp" //
int main ()
{ std::cout "Pow2<10>:iresult = " Pow2<10>::result '\n'; }

. -
. ,
. , , ,
.
, C++
. ,
. -
, -
, 231
.
(. . 1).
(
). , ,
:
template<int n>
struct Factorial {
static int const value = n * Factorial<n-l>::value;
i
1;
12. 413_

ternplateo
struct Factorial<0> {
static int const result value = 1;


Borland C++ Builder 6, . .
, , -
.

, -
, , " ( -
12.15).

12.15. "

template<int , int N>


class Pow {
public: enum { result = * PovKK, N-l>::result 1;
};
//
template<int K>
class Pow<K, 0> {
public: enum { result = 1 };

"" () :
cout "Pow<3, 0>::result = " Pcw<3, 0>::result '\n';
cout "Pow<5, 3>::result = " Pow<5, 3>::result '\n';

:
.
:
template <typenaine T>
inline product (int dim, T* , * )
{ result = 0;
for (int i = 0; i < dim; ++i)
result += a[i] * b[i];
return result;
}

,
, :
4t4 II. -

int main()
( double a [ 3 ] = { 1, 2, 3 };
double b [ 3 ] - { 5, 6, 7 };
cout "product(3, a, b) = " product(3, a, b) endl;
cout '"product (3, a, a) = " product(3, a, a) endl;
}

,

. ,
:
a[0]*b[0] + a[l]*b[l] + [2]*[2]

12.16.

I 12.16.

//
template <int DIM, typename T>
class Product {
public:
static result (* , * b) {
return *a * *b + Product<DIM~l, T>::result(a+1, b+1);

//
template <typename >
class DotProduct<l, T> {
public:
static result (* , * b) ( return *a * *b; }
};
// "" Product
template <int DIM, typename T>
inline product (* , * b)
{ return Product<DIM, T>::result(, ; }

, -
. -"" -
, . -
-"" :
cout "product<3>(a, b) = " product<3>(a, b) endl;
cout "product<3>(a, a) = " product<3>(a, a) endl;

,
.
III


Windows
13. Windows

14.
13

Windows


, -
. -
.
CharToOem, . "-
" Microsoft (-.)
Application Programming Interface. -
" ", ,
, . -
API. API Windows
WinAPJ Win32 API. 32 ,
, 32- 16--
. , Win64
[33, 43].
WinAPI 2000 ,
, . ""
.
, -
Windows. .


,
. , -
, , , -
. -
Microsoft , -
,
Windows.
143. 1264
418 lit Windows

"" include , -
,
win. ,
, . Microsoft
typedef #define. -

. Windows
C++. , -
Pascal, .
C++,
, .
IBM PC, -
Windows.

. , , windef.h -
: 1

typedef unsigned char UCHAR;


typedef int INT;
typedef int BOOL;
typedef unsigned short USHORT;
typedef unsigned int UINT;
typedef long LONG;
typedef unsigned long ULONG;
typedef unsigned long DWORD;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef float FLOAT;

, . -
Windows : ,
. :
() ,
(). , sz ,
(string terminated by zero); i
(integer); (pointer) .
. ,
:
typedef FLOAT *PFLOAT;
typedef ULONG *PULONG;
typedef USHORT *PUSHORT;
typedef UCHAR *PUCHAR;
typedef char *PSZ;
13. Windows . 419

, L
( . 4.8).
, ,
L long "". Win32 API -
"".
win -
:
typedef UINT WPARAM;
typedef LONG LPARAM;
typedef LONG LRESULT;

.
, , API,
. -
, ttdefine.
,
, . (-
) , ,
,
. -
. 13.1.

13.1.

(char); (const)
by (byte)

i
, ( )
, , "" "" (count)
b f (bcol); "f" "" (flag)

w (word)

I (long)
(unsigned)
dw (dword)
fn
420 III. Windows

13.1 ()


s
sz ,
h (handle)
(pointer)

, "". ,
IpSzFileName , 6, ,
; fdwAccess , -
, ( -).
h,
handle (, ). "-
". [431 ( .
descriptor). , -
, "". ""
, -
, .
, , "".
, Windows "".

Windows.

Unicode
, ,
, .

, Microsoft ASCII.
-
char. C++ (
Intel 1 ).
, Windows, -
, . . Windows
.
, -
. -
, -
Unicode. Intel Unicode
2 . wcharj;, -
"" .
13. Windows 421


Borland C++ Builder 6 , wchar_t
, int. , "-
" , .

, "" , -
L:
wchar_t *ws = " !";

wcs,
str. :
char *strcat(char *dest, const char *src);
wchar_t *wcscat(wchar_t *dest, const wchar_t *src);
, , .
,
cout ws endl;
, ( ).
Windows WinAPI.
WinAPI ( char
CHAR) "" ( WCHAR,
wtypes.h). Windows "" Unicode.
Microsoft WinAPI
TCHAR ( tchar.h),
, , , -
, . -
.
,
. -
. , -
, , .
,
.
1. TCHAR, LPTSTR,
LPCT3TR.
2. ( -
):
(" ");
_(" ");
TEXT(" ").
422 ///. Windows

3.
sizeof(TCHAR).

4. <windows.h> <tchar.h>.
-
.
5. <windows.h>
: ^define UNICODE #define _UNICODE.
-
:
1. .
2. .
3. , .
4. .
, , -
13.1.

13.1. Unicode

ftdefine UNICODE // Unicode


#define JJNICODE
ttinclude <windows.h>
^include <tchar.h> //
#include <conio.h>
^include <iostream.h> //
//
BOOL PrintMsg(HANDLE hOut, LPCTSTR pMsg);
BOOL PrintStrings(HANDLE hOut, ...);

int main(char argc, char ** _targv)


{ int k; //
HANDLE hStdOut; // ""
//
hStdOut = CreateFile(_T("CONOUT$"), GENERIC_WRITE, 0, NULL,
QPEN__ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;
SetConsoleMode(hStdOut, //
ENAELE_WRAP_AT_EOL_OUTPUT I ENABLE_PROCESSED_OUTPUT);
TCHAR* pr = _T(" : "); //
PrintMsg(hStdOut, pr); //
cin k; //
13. Windows _ 423

//
PrintMsg(hStdOut, CharUpper(_T (" : \") ) ) ;
//
TCHAR s t r l [ 3 ] ; _ i t o t ( k , strl, 10); s t r l [ 2 ] = 0;
for (int i = 0; i <= 10; i++l //
{ TCHAR str2[3]; _itot(i, str2, 10); s t r 2 [ 2 ] = 0;
int d = k * i; //
TCHAR s t r 3 [ 3 ] ; _itot(d, str3, 10); s t r 3 [ 2 ] = 0;
TCHAR *st = new TCHAR [25]; //
for (int j = 0; j < 25; ++ j ) //
at[i] - _ T ( " \ 0 " ) ;
//
st = wcscatfst, str2);
st = wcscatfst, _T("*"));
st = wcscat{st, strl);
st = wcscatfst, T("="));

st = wcscat(st, str3) ;
st = wcscatfst, _T("\n"));
PrintMsgfhStdOut, st) ; //
)
getch ( ) ; //
return 0;

-"", -
. Windows CONINS
CONOUTS . -
. ,
Windows, .
"" -
, ,
,
.
, , -
"" .
,
.
, , , -
. , -
.
, -
13.2.
424 III. Windows

\ 13.2.

BOOL PrintStrings(HANDLE hCut, . . . )


{ DWORD MsgLen, Count;
LPCTSTR pMsg;
va_list pMsgList;
va_start(pMsgList,hOut);
while ((pMsg = va_arg(pMsgList, LPCTSTR)) != NULL)
{ MsgLen = Istrlen(pMsg);
if (!WriteConsole(hOut, pMsg, MsgLen, sCount, MULL)) return FALSE;
)
return TRUE;
};
BOOL PrintMsg(HANDLE hOut, LPCTSTR pMsg)
( return PrintStrings(hOut, pMsg, NULL); }

Printstring -
, v a _ . . .
writeConcoie, -
"" , , -
Win API, -
.

&.! :)! and Setting&VFirstlUoM



-1:
0*7=0

5*7=35
6*7=42
7*7=49
8*7=56
9*7=63

. 13.1.
13. Windows 425


.
. 13.1.
,
.
Borland C++ Builder 6 MS SDK Help Files
Microsoft MSDN, .


Windows ,
"" .
, ,
, . -
" " . -

" ", -
WinAPI. , -
. "" , ""
.
Windows.

""
""
: ,
. .
-
, .
main, . -
winMain, -
.
1. .
2. .
3. .
4. .
( ) , "
.
13.3. ,
.
426 III. Windows

13.3.

int WINAPI WinMain( HINSTANCE hi, // "" ()


HINSTANCE hP, // "" ()
LPSTR IpszCmdLine, //
int nCmdLine) //
{ MSG msg; //
HWND hwnd; // ""
static char szClassName [] = "FirstPrograrn"; //
//
if (IRegClass(hi, WndProc, szClassName)| return FALSE;
//
hwnd = CreateWindow(
szClassName, //
" ", //
WS_OVERLAPPEDWINDOWIWS_VISIBLE, //
CW_USEDEFAULT, CW_USEDEFAULT, //
CW_USEDEFAULT, CW_USEDEFAULT, //
, //
, //
hi, // ""
NULL); //
if (Ihwnd) return FALSE; //
ShowWindow(hwnd, SW_SHOWNORMAL) //
UpdateWindow(hwnd); //
while(GetMessage(&msg, 0, 0, 0) //
{ TranslateMessage(Smsg);
DispatchMessage(&msg);

return msg.wParam;
:
, -
: -
"" .
:
Regciass (
). ( 13.4) -
, , ,
.
,
,
.
13. Windows 427

13.4.

BOOL RegClass(HINSTANCE hlnst, WNDPROC WndProc, LPCTSTR szName)


( WNDCLASS we; //
we. style = CS_HREDRAW | CS_VREDRAW; //
wc.lpfnWndProc = WndProc; // ,
we. IpszMenuName = NULL; // ,
we. IpszClassName = szName; //
we. hlnstance = hlnst; // ""
//
wc.hlcon = LoadIcori(NULL, IDI_APPLICATION) ;
wc.hCursor = LoadCursor (NULL, IDC_ARROW) ;
//
wc.hbrBackground = (HBRUSH)GetStockObject (LTGRAY_BRUSH) ; //
wc.cbClsExtra = we. cbWndExtra = 0;
return (RegisterClass (fiwc) != 0) ; //

.
we. style = CS_HREDRAW I CS_VREDRAW;

,
- (CS_HREDRAW) (CS_VREDRAW)
. ,
.
, -
. WndProc.
, ( ).

we. IpszClassName = szName; //


we. hlnstance = hlnst; // ""

.
. ,
, . .
.
( ).
, , -
. - , ( -
ico) ( cur) -
. ,
.
428 III. Windows

-
.
, , -
API RegisterCiass. -
, ,
.
. "" ,
.
, ,
(WS_OVERLAPPEDWINDOWIWS_VISIBLE), -
( ).
. -
, showwindow.
, .
Updatewindow. -
"" .
,
, ,
. -
13.5.

! 13.5. ( )

LRESULT CALLBACK WndProc(


HWND hwnd, // ""
UINT msg, //
WPARAM wParam, //
LPARAM IParam) //
[ switch(msg)
{ case WM_CREATE: //
MessageBox(hwnd, " WM_CREATS", //
"!", //
MB_OK|MB_ICONWARNING); // <!>
return 0;
case WM_LBUTTONDOWN //
MessageBox(hwnd, " WM_LBUTTONDOWN ", "!",
MB_OK.|MB_ICOtIINFORMATIOH) ; // <i>
return 0;
case WM_DESTROY: //
MessageBox(hwnd, " WM_DESTROY ", "!",
MB_OK|MB_ICONQUESTION); // '<?>
PostQuitMessage(0); // WM_QUIT
13. Windows 429

return 0;

//
return DefWindowProc (hwnd, msg, wParam, IPararn)

, --,
. -
. WM.
MSG, -
.

.
, (. 13.3) -
Windows
, , -
. " -
, -
Windows , -
.
MessageBox{)
. -
, .

, .
, .
. 13.2.

;
! |

WM_DE5TROY
^)
^
i OK ;j

. 13.2.

, ,
, ,
. 13.3.
, -
. (
Windows), , -
.
430 III. Windows

. 13.3. ""


, Windows, -
: , , -
, , , -
. ,
, . . ,
. -, , -
.
, (-
13.1),
, . -
10 . Windows
,
. , -
, , -
.
:
1 10 -
. ,
.
,
.
;
( ,
) . -
;
13. Windows 431

-
;
-
.
. 13.4.


1 3
xZ 7

4 9 _
5
9

. 13.4.

-
, -
, -: -
WinAPI.



Windows, ,
.
Windows .
""
, .
,
, ( ).
Windows ,
.
,
.
"" .
: ?.
-
.
. -
: no-,
432 III. Windows

. :
.
no-
.
-
, .
.

ftdefine:
#dsfine IDM_MULT_1 1001
tfdefine IDM_MULT_2 1002
#define IDM_EXIT 1003
ttdefine IDM_ABOUT 1004

,
.
"": "" -
, "" :
static HMENU hMenu, hFirstMenu, hAbout;

, (, "")
,
.

WM_CREATE,
(. .5) , 13.6.

| 13.6.

hMenu = CreateMenu(); //
hFirstMenu = CreatePopupMenu(}; //
AppendMenu(hFirstMenu, MF^STRING, IDM_MOLT_1, " no-");
AppendMenu (hFirstMenu, MF_3TRINGr IDM__MULT_2, " ") ;
AppendMenu (hFirstMenu, MF__SEPARATOR, 0, NULL); // -
AppendMenu(hFirstMenu, MF_STRING, IDM_EXIT, "");
//
AppendMenu (hMenu, MF__POPUP, (UINT) hFirstMenu, "");
hAbout = CreatePopupMenu(); //
AppendMenu(hAbout, MF_STRING, IDM_ABOUT, " ");
//
AppendMenu(hMenu, MF_POPUP, (UINT)hAbout, "?");
SetMenulhwnd, hMenu); //
DrawMenuBar(hwnd); //
13. Windows 433

, , "" -
, Windows.


, -
WM_CREATE . ,
,
Createwindow.
. -
"". . -
, , -
. ( ) -
, .
13-7.

1 13.7.

struct OneButton {
long style; //
char 'text; //

const int NH = 11;


OneButton flags[NN] = //
{ BS_CHECKBOX, "x 1", BS_CHECKBOX, "x 2",
BS_CHECKBOX, "x 3", ES_CHECKBOX, "x 4",
BS_CHECKBOX, "x 5", BS_CHECKBOX, "x 6",
BS^CHECKBOX, "x 7", BS_CHECKBOX, "x 8",
BS__CHECKBOX, "x 9", BS_CHECKBOX, "xlO",
BS_CHECKBOX, "Bee"
};
OneButton GroupFlags = { BS_GROUPBOX, " " };
OneButton button = { BS_PUSHBUTTON, "" };

,
GroupFlags. -:
ttdefine ID_ALL 4010

, ,
40004009. , -
-"":
static HWND hGroupFlags;
static HWND hflags[NN];
434 III Windows

, -
:
. . -
:
HOC hdc; // -
TEXTMETRIC tm; //
hdc = GetDC(hwnd); // ""
//
SelectObject(hdc, GetStockObject(SYSTEM_FONT)};
GetTextMetrics(hdc, stm); //
cxChar = tm.traAveCharWidth; //
cyChar = tm.tmHeight; //
ex = tm.tmAveCharWidth;
//
= tm.tmHeight + tm.tmExternalLeading;
//
SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
ReleaseDC(hwnd, hdc); //

, ,
( 13.8). ,
.

; 13.8.
; '....: '. ', .' : ': .:::: ."...!

//
hGxoupFlags = CreateWindow)
"button", GroupFlags.text,
WS_CHILDIWS_VISIBLEIGroupFlags.style,
cxChar, cyChar,
21 * cxChar, 7 * cyChar + 5,
hwnd, 0, hlnst, NULL);
//
cxChar = tm.tmAveCharWidth + 5;
cyChar = tm.tmHeight * 2;
for (i = 0; i < NN/2; i++)
hflags[i] = CreateWindow(
"button", flags[i].text,
WS_CHILD|WS_VISIBLE|flags[i].style,
cxChar, cyChar + tm.tmHeight*!,
60, cyChar/2f
hwnd, () (4000 + i), hlnst, NULL);
13. Windows 435

II
cxChar = tra.tmAveGharWidth + 85;
cyChar = tm.tmHeight * 2;
for (i = 5; i < NN; i++)
hflagsfi] =? CreateWindow(
"button", flags[i].text,
WS_CHILD|WS_VISIBLE|flags[i].style,
cxChar, cyChar + tm.tmHeight*(i - 5),
60, cyChar/2,
hwnd,(HMENU)(4000 + i), hlnst, NULL);
//
static HWND hLabel;
cxChar = tm.tmAveCharWidth + 160;
cyChar = tm.tmHeight;
hLabel = CreateWindow(
.
"static",. " ",
WS_CHILD|WS_VISIBLE,
cxChar, cyChar,
160, cyChar + tm.tmHeight/2,
hwnd,0,hlnst,NULL);
//
static HWND hButton;
cxChar = tm.tmAveCharWidth + 160;
cyChar = tm.tmHeight*6 -f 5;
hButton = CreateWindow(
"button", button.text,
WS_CHILD[WS_VISIBLE|button.style,
cxChar, cyChar,
160, 2 * tm.tmHeight,
hwnd,(HMENU)ID BUTTON, hlnst, NULL);

, .
"" -
"" .
( static). 1 -
- ID_BUTTON:
tdefine ID_BUTTQN 2000

: -
, - -"":
const char *szList[10] - "", "", "",
"", "", "",
"", " ", "", ""
436 III. Windows

tfdefine ID_LISTBOX 3000


static HWND hListBox;

-
:
cxChar = trrutmAveCharWidth + 160;
cyChar 2 * tin. tmHeight;
hListBox = CreateWindow(
"listbox", NULL,
WS_CHILD!WS_VISIBLSIWS_VSCROLL|LBS_NGTIFY|WS_BORDER,
cxChar, cyChar,
160, 5 * tm.tmHeight,
hwnd,(HMENU)ID_LISTBOX,hInst,NULL);
for {i = C; i < 10; ++i] //
SendMessage(hListBox, LB_ADDSTRIKG, 0, (LPARAM) szList[i]);

:
SendMessage(hListBox, LB_SETCURSEL,0,0);

WM^CREATE
:
return 0;


(. 13.5) -
wM__coiAMD. , -
. ""
-,
-. -
wParam.
.
13.9.

13.9.

WM_COMMAND:
switch(LOWORD(wParam)i
{ case IDM_EXIT: //
SendMessage(hwnd, WM_CLOSE, 0, OL) ;
return 0;
case IDM_MULT_1: // no-
SendMessage(hflags[10], BM SETCHECK, FALSE, OL) ;
13. Windows 437

for (i = 0; i < 1C;


( SendMessage(hfIags[i], BM_SETCHECK, FALSE, OL);
EnableWindow(hflags[i], TRUE};
m2[i] = result[i] = 0;

return 0;
case ID__ALL: // ""
case IDM_MULT_2: //
// ?
if (!SendMessage(hflags[10], BM_GETCHECK, 0, OL))
{ SendMessage(hflags[10], BM_SETCKECK, TRUE, OL) ;
for (i = 0; i < 10; ++i) .//
{ SendMessage(hflags[i], BK_SETCHECK, TRUE, OL) ;
EnableWindow(hflags[i], FALSE); //
m2[i] = 1 + 1 ;
I

else
{ SendMessage(hflags[10], BM_SETCHECK, FALSE, OL!;
for {i = 0; i < 10; ++i) //
{ SendMessage(hflags[i], BM_SETCHECK, FALSE, OL) ;
EnableWindow(hflags[i TRUE); //
m2[i] = result[i] = 0, //
I

return 0;
case IDM_ABOUT: // " "
if(!IsWindow(hAbout)) //
hAbout = CreateWindowt
szClassAbout, " ",
WS_POPUPWIOW|WS_VISIBLE|WS_CAPTION,
CW__USEDEFAULT, CW_USEDEFAULT,
strlen(" ") * ex * 1.5
hwnd, 0, hlnst, MULL);
return 0;
case ID_BUTTON: //
for (i = 0; i < 10; ++i)
{ r e s u l t [ i ] = ml * m 2 [ i ] if(result[i]) All = true; }
if (All) //
{ if(!IsWindow(hMulti)) //
hMulti = CreateWindow(szClassMulti,
" ",
WS POPUPWINDOWIWS VISIBLEIWS CAPTION,
438 III. Windows

CW_USEDEFAULT,CW_USEDEFAULT,
strlenf" ") * ex * 1.5, * 12,
hwnd, 0, hlnst, NULL);
}
else
MessageBox(
hwnd, " He !",
"",
MB__OK|MB_ICONERROR) ;
return 0;
case 4000:case 4001:case 4002: //
case 4Q03:case 4004:case 4005:
case 4006:case 4007:case 4008:case 4009:
i = LOWORD{wPararn) - 4000; m2[i] = i + 1;
if(!SendMessage(hflags[i], BM_GETCHECK, 0, OL))
SendMessage{hflags[i], BM_SETCHECK, TRUE, OL) ;
else
SendMessage(hflags[i], BM_3ETCHECK, FALSE, OL) ;
return 0;
case ID_LISTBGX: //
if(HIWORD(wParam) == LBN_SELCHANGE)
{ ml - SendMessage(hListBox, LB_GETCURSEL, 0, 0) + 1; }
return 0;
J

, , -
, , -
, "". -
SendMessage, "",
- , -
. , :
SendMessage(hflags[10], BM_GETCHECK, O f OL)

:
SendMessage(hflags[ij, BM_SETCHECK, TRUE, OL) ;
EnableWindow{hflags[i], FALSE);

ID_ABOUT -
.
(popupwindow) . -
, . -
. "", -

:
13. Windows 439

if (IRegClass(hi, WndAbout, szClassAbout)) return FALSE;


if (IRegClass(hi, WndMulti, szClassMulti)) return FALSE;

.
WM_PAINT.
. , -
,
WinAPI, - Graphics Device Interface (GDI) -
. 13.10
.

i 13.10.

LRESULT CALLBACK WndAbout {


HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM 1 Pa ram)
{ HOC hdc; //
PAINTSTRUCT pc; //
RECT rect; //
switch (msg)
f case WM_PAINT:
hdc = BeginPaint {hwnd, spc) ;
GetClientRect (hwnd, firect) ;
SetBkColor{hdc, GetSysColor (CCLOR_BTNFACE) ) ;
DrawText (hdc, " \ n \
-1, &rect, DT__CENTER|DT_VCENTER) ;
EndPaint (hwnd, &pc) ;
return 0;
}
return DefWindowProcihwnd, msg, wParam, IParam) ;

BeginPaint ,
GetciientRect ,
, ,
SetBkColor.

DrawText,
, .
' \ ' . -
.
EndPaint ( ) .
. 13.5.
440 III. Windows

. 13.5.

, ,
. ,
. ,
13.11, WM_PAINT.

13.11.

LRESULT CALLBACK WndKulti (


HWND hwnd,
U INT ir.sg,
WPARAM wParam,
LPARAM IParam)
{ HDC hdc; // -
PAINTSTRUCT pc; //
RECT rect;
char szString[200] = {0}; /'/
char szBuffer[20] = {0}; //
switch (msg)
f case WM_PAINT:
for (int i = 0; i < 10; --i) //
{ if <m2[i]l
{ . sprintf (szBuffer, "%2d x %2d = %2d\n" , m2[i], ml, result [i] );
strcat (szString, szBuffer) ;

hdc = BeginPaint (hwnd, &pc) ;


GetClientRect (hwnd, Srect) ;
SetBkColor (hdc, GetSysColor [COLOR_BTNFACE) ) ;
DrawTexr(hGC, szString, -1, &rect, DT_CENTER| DT_VCENTER) ;
EndPaint (hwnd, &pc) ;
return 0 ;

return DefWindow?(hwnd, msg, wParam, IParam);

.
-
73. Windows 441

, / stdio.h.
"" .

, .
,
(. 13.6), (. 13.7).

[= F< \
1 X 7= 7
i
3x8 --*

2 7= 5 8
7 = 21 7 8 = 56
4 7-28 8 8 = 4
5 7 = 35 8 = 80
7 = 42
7 7 = 49
8 7 = 56
9 7 = 63
7 = 70

. 13.6. . 13.7.


,
, .
, -
Windows. WinAPI?
, WinAPI
. ,
. -
Windows,
, -
. , -
. , -
-
.
( ) -
:
we. hbrBackground = (HBRUSH) (COLOR_BTNFACE - 1) ;

, -
. "" , , .
.
" Windows 95"
"HelloWin", " ".
44g III. Windows

-, , -
. -,
. , "" "": -
, , .
-
.
"".
-, , "" - -
- , MFC VCL -
.
"",
WmA.Pl
14


. Windows
WinAPI -
. "" .
, Borland
, -
. Pascal Delphi, a
C++ C++ Biulder. -
- VCL (Visual Compo-
nent Library), , ,
. -
. -
,
.

1
C++ Builder. ( -
)
. .
(<Ctrl>+<F9>) (<F9>) -
, "" ,
"" Windows: , -
.
WinAPI , -
. , ,
VCL ,
.
444 III. Windows

2
( )
.
(. 14.1).

Standard j Additional {. Win32 1 Susteml DatnAceea! DataCortiolH dbExwess] DataSnaol BDE 1-lLL
A 35
I l* ^j i* :~
. 14.1.

GroupBox ( -
, ) Button ()
Standard.
11 checkBox
(),
( ).
.
,
<Shift>. -
.
,
. 14.2.

. &i . -inui
.- GioupBoxV " : GroupBon2 "
:;
CheckBoxl CheckBox2 ' iCornboBuKl C

. . ~-5 ;
' CheckBox3 Check8ox4
-', CheckBon5 CheckBonG

1 CheckBox? CheckBoxB

! OieckBox9 CheckBoxlO

CheckBonll Bultonl

. 14.2.

. -
(Design Mode) Object Inspector,
, 14.3.

.
14. 445

Properties

AJigi albione
AlphaBlend .false
Alpha8tendVak255
S Anchors
AuioScrotl
AutoS'i
BiDiMode

BorderStyle
j Boide Width
j Caption

. 14.3. Object Inspector

-
. , ,
, . -
. , -
, , . -
, .
.
.
, caption (. )
. .
:
Font , ;
Align ;
TabOrder
<>;
Tabstop <>;
Hint , ;
G Enabled / ;
visible / ;
G Left, Top, width, Height -
.
, ,
(Name). , -
. , -
checkboxi , -
TCheckBOK* checkboxi.
446 III. Windows

Tag ( ).
,
, .
, -
. ,
Checked, true, , false,
.
ComboBox items. ,
. .
.
, Text itemindex,
() .
, .
, .
, . 14.4.

"*""""
|-1 '""'1 - "'
1
! 1 2 (;,; ( -
> '.
" 4
5* 6
7 8

,

| : '.

. 14.4.

. TMainMenu
,
. 14.5.
. -
:
?, .
:
-
;
;
.
14. 447

(Tl fm Main ->Main Menu!

AIM-F4

. 14.5.

, & caption
"". ,
, <>+<>.
" " (
shortcut). Radioitem
().

(. . 14.5) : -
, . . -
: , -
-
, .
, /
/. -
, : -
. ,
.
Events
Object Inspector, . 14.6.
onclick,
. , -
Object Inspector
.
- (. 14.7).
448 III. Windows

^-- ; ^i
:
|cb_all - _^J

Properties Events j

I Action JTJjt.
I OnClick cb_alOck
[ QnContextPopi
!
i OnDtsgDrap
i DnDtagOver
\ OnEndDQck

DnE ndDfag
: DnEnter
. ,; OnEnit
. OnKeyDown .
' OnKeyPress ^J
All shown

. 14.6. Object Inspector


,
.

Main.cpp

Main.cpp j ResA'cpp] About.cppj


ttJ Qjj Multiply Classes

ivoid fastceJJ. TfmHain: : c b l O C l i c k ( T O b j e c t ^Se.

. 14.7.

, ,
.
N2->Checked = !(cb_all->Checked);
H3->Checked = cb all->Checked;
14. 449

N2 , N3 .
onclick ,
,
cb_aii->checked.
, cb_aii, -
, checkBox
. " ", 14.1.

14.1. / (1)

if (cb_all->Checked! !
cbl->Checked = true;
cbl->Enabled = false;
cb2->Chec:<ed = true;
cb2->Enabled = false;
cb3->Checked = true;
cb3->Enabled = false;
cb4->Checked = true;
cb4->Enabled = f a l s e ;
cb5->Checked = true;
cb5->Enabled = false;
cb6->Checked = true;
cb6->Enabled = false;
cb7->Checked = true;
cb7->Enabled = false;
cb8->Checked = true;
cb8->Enabled = false;
cb9->Checked = true;
cb9->Enabled = false;
cblO->Checked = true;
cblO->Enabled = false;
}
else {
cbl->Enabled = true;
cb2->Enab!ed = true;
cb3->Enablea = true;
cb4->Enabled = true;
cb5->Enabled = true;
cb6->Enabled = true;
cb7->Enabled - true;
cb8->Enabled - true;
cb9->Enabled - true;
cblO->Enabled = true;
I
450 _ III. Windows

, 14.2.
'!. ..................... ................... ..... - ............. .......... - ........................................................................ ....... ...................... ...... . .......... ......... ; ............. ..............
I 14.2. / ( 2)

if (cb_all->Checked) {
for (int i 1; i < 11; i++) {
TCheckBox* cb_control =
(TCheckBox*) freMain->FindCornponent ("cb" + InrToStr (i) ) ;
cb_control->Checked = true;
cbcontrol->Enabled = false;

else {
for (int i = 1; i < 11; i++) {
TCheckBox* cb_control =
(TCheckBox"] FindComponent ("cb" + IntToStr ( i ] ) ;
cbcontrol->Enabled = true;

FindComponent ( )
. , -
TComponent* ( ). -
, , cbx, -
1 10.
,
-:
cb_all->Checked = false;


cb_all->Checked = true;

.
,
. 14.8.

4
. File { New j
New Form. , ""
Label . -
, . 14.9.
14. 451

' .*tt*l

'- 17 ::
5- . - 1> '

17 '-I- -P ;'.

17

. 14.8.


; ; ;
. -
! ','.-"-'.
" ".
', - -
"" .

Copyright - !iic,2t)Q3

01

. 14.9.

, h, cpp
( ) dfm (-). h- -
. ? -
:
fmAbout->ShowModal();

:
, .
closed.
.

5
: Grid
(, ) . . 14.10.
452 III. Windows

. 14.10.

, -
. h- .
.
.
, -
.
, Projects | Options
( . 14.11).

Forms.
,
14.3.

14.3.

int , k = 0;
TfmResult* resfrrn = new TfmResult (this) ;
= ComboBoxl->lternIndex+l;
for (int 1 = 1 ; i < 11; i++){
TCheckBox* cb_control = (TCheckBox*)FindComponent("cb" + IntToStr(i));
if (cb_control->Checked){
resfrm~>AddResultLine(i, y);
14. 453

if (k > 0) resfrm->Show();
else MessageBoxf this->Handle, "", "", M3- OK | MB ICONEXCLAMATICN);

Project Options for MuBtiply.ene

Pascal . Linker Advanced Linket


Version Info I Packages Tasm CQRBA j CodeGuard
Forms Application I Compiler | Advanced Compiler; j C++-

Mainform: ffmMdn

Aulo-cieale forms' Available f rams'


fmMain
IrrAboul

Cancel Help

. 14.11.

, , , .
-
. -
, ,
ftddResuitLine(i, ) -
.
, ,
, WinAPl MessageBox.
, , -
. , , , -
,
. 14.12.
AddResuitLine. C++ Builder ,
. -
. 14.13.
454 III. Windows

f

'- 17

'- OK*re. . , Pw*r


1" ,' 7
7 14

3' 7 21

4" 7 29
5" 7 35
L. 7 42
,.- 7 43

3" 7 56
7" '
' 63"

10 -7 70

- ,-* " '*


/] | gaxt+Buiidar 6 | | {E:\pi IVForWlAbjilde.,. } ^^Doc.doc^bc^j|B^ Multiply

. 14.12.

[Add Method

.Method Name: jAddResuttUne

AddtoDa^l^TlrnRe^ll jj

Arguments: pnt , inly

i (* Eunclion Cor^ttuctor Deslrucla

\ Function Result: void * \

| P Pjjblic Private Pt2tected FXifelished

;~ Diiectives
. abstract , const (3]
i virtual (2) [7 iastcaStil
-
; Mgssage Handle;: i
[___; ._ . :
Implementation details ~
j " Callinherited * Injine P i;i,.'i :rh;irft '

; File: Epr^FWL^^buierl^FleslJlt.cpp

, Reset IQ] | QK Cancel I ApprV


I- ' i - 1_ 1

. 14.13. Add Method


14. 455

, , :
StringGrid->RowCount = StringGrid->RowCount+l;
StringGrid->Cells[0][StringGrid->RowCount-l] =
IntToStr(x) + " * " + IntToStr(y);
StringGrid->Cells[l][StringGrid->RowCount-l] = IntToStr(x*y);


.
: onFormShow .
:
StringGrid->Cells[0][0] = "";
StringGrid->Cells[l][0] = "";
StringGrid->FixedRows = 1;

,
! , , -
. , , ,
, .
C++ -- -
.
( ) - ,
. "" -
, . , -
, , .
4 , , -
, , .
,
, .
-
. [4] "" [8].
"" " " -
[10].
[7, 9, 14, 39, 40, 41, 45].
[91, Pascal,
[39,40]. , [14] -
.

Visual C++ 6, [19, 26, 28, 46], "", -
, [19].
[3, 32, 36] . -
. Windows -
[43], -
[33] [50].
455


. [37, 38], [23, 24, 25], -
"C++ In-Depth" [1, 15, 20, 35],
.
[49]. STL
[2], [42] . -
"" . STL.
, , C++. , C++
(
).
.
, , ,
. -
!
IV

1, Borland
2. Microsoft Visual C++ 6
3. C++
4.
1

Borland

-
, , -
, , , ,
. -
(Integrated Development Environment IDE). -
, , -
, .
-, -
, , ,
. -
: Borland C++ ,
Borland C++ 5 Borland C++ Builder 6. ,
. . .
" ".

Borland C++ 3.1


10 ( 1992 ), -
, -
. , -
IDE Borland ( ).

, Windows: ,
, , "" ""
. ( Windows)
.
-
DOS Windows. DOS,
. . . Windows .
462 IV.

(
). ,
:
Bin ;
Include ;
Lib (
obj) ( lib) , -
;
Examples ;
Doc
;
TVision - -
OWL VCL.

IDE

Bin
,
( ).
Windows, -
. .1.

'''"Debug " Project""Options

F.I Help; F2 Save F3 open Alt-F9 Compi le F9 Hake 10 Menu

. 1.1. Borland C++ 3.1


1. Borland 463

. -
:
? ;
File (, , . .);
d Edit (Clipboard);
Search ;
Run ;
Compile ;
Debug ;
Project ( );
Options ;
Window ;
Help .
<F10>.
" " <11>+<>.
, :
File | Quit;
<Alt>+<F> <Q>;
<A3t>4-<X>.


( -
)
.
, :
File | New;
<Alt>+<F> <N>.
,
(. . 1.1).
. :
File | Save;
<Alt>+<F> <S>;
<F2>.
, -
( Bin )
464 IV.

nonamexx.cpp ( .).
. 1.2.
<Enter>.
,
File | Save As, .

Fl Help Enter directory path and n le-name mas

. 1.2.

Edit "".Sear Run"'Compile "Debug Project" Options" " winclow""He

1
Fl Help== renter directory patn-.ana rile-natie mask

. 1.3.
1. Borland 465

:
File | Open;
<Alt>+<F> <>;
<F3>.
,
. 1.3.
( ) .
.


.
, , -
,
. Project , -
, , .


:
1. Project j Open project. ,
(. . 1.3), -
.

1 '-Ins Add Del.'Delete- Options>'<Space Includes. -*-1 Edit, Fly Menu

. 1.4.
466 IV.

2. (. 1.4) ,
( <Insert>, Project | Add Item).

<DeIete>, Project | Add Item.
Compile | Build all
Compile | Make :
,
.

,

Compile:
Compile <Alt>+<F9> ;
Make <F9> ;
Build all .
Make Build all -
.
Run (
<Ctrl>+<F9>). -
. -

( Window ] Output), ( -
<Alt>+*<F5>).
( Run | Arguments).


-
. -

. ,
, ,
, .
( -
<Ctrl>+<F2>, Run | Program reset).


, -
.
1. Borland 467

<Ctrl>+<F8> ( Debug) Toggle


breakpoint). ,
. -
, . -
, , -
. .


.
, , -
<Ctrl>+<F4> (
Run | Goto cursor).

()
, -
.
:
G "" , ( <F7>,
Run | Trace into).
"" ,
( <F8>, Run | Step over).


, -
. ,
.
:
Inspecting ( <Alt>+<F4>, -
Debug | Inspect) (. 1.5);
Evaluate and Modify ( <Ctrl>+
+<F4>, Debug | Evaluate and Modify) (. 1.6);
Watch ( <Ctrl>+<F7>,
Debug | Watch) (. 1.7).


Borland ( , , Microsoft)
-
.
<F1>. "" ,
468 IV.

" " -
. Help -
. :
Contents ,
(. 1.8);

^include <iostreani.h>

int mainQ
unsigned Int year = 0;
begin: 'h** J hspecti ng year,' f*3?I ]
cout " : ";
if (CO<year)&SCyear<121))
{ int digit = year&lQ;
int one = year / 10 % 10;
if Cone != 1)
eise IT ( == 2)|Kdigit ==
cout year ! "end! ;
else cout " !"11;
else cout year" flerlu<<endlj
else goto begin;
return 0;

Ip F1Q Menu

. 1.5. Inspecting

^include <iostream,h>
int main(}
unsigned
begin:
cout "3
if C(0<ye
{ int dig
int one
if (one
else i
cout
else
}
else
else goto b^gin;
return 0;

-1 Help | Enter expression"to evaluate

. 1.6. Evaluate and Modify


1. Borland 469

File Edit Search Run Compile'" Deb'uo "Froiect-


BC3,1\BOOKS\GOD-LET.CPP
^include <i0$tream.n>
int main
unsigned int year = 0;
begin;
cout " : "; cin year;
if (CQ<year)<b&(year<121))
{ int digit = yearSSlO;
int one = year / 10 % 10;
if Cone t = 1)
eise i hgit == 4,
cout year ro^al"end
else cout " !

Fl Help' F7 Trace F8 Step

. 1:7. Watch

^include <iostrean

int main
{ H
unsigned i,
begin: IMenus and Hot Keys
cout "
if ((Ckyea
{ int digi
int one Assembler (built-in) . Functions
if (one Borland ** Language Header Files
{if (dig Coraiand Line (BC) lOStream cla
else if "Cowmanc! Line CBCC) Uti"Htieb
cout > : Container Classes
else > -> "Error Messages -

Fl Help-on help'i Alt-Fl Previous topic Shift-Fl""* Esc close help'

. 1.8.

Index ( <Shift>+

G Topic search (
<Ctii>+<Fl>), -
C++ : -
470 IV.

"
";
D Previous topic .
Help :
Editor Commands
;
Borland C++ Language C++ -
, ;
Command Line () ()
;
Functions , ;
Header Files ,
include;
Error Messages .

Borland C++ 5
,
, C++.
Windows, -
-
.

IDE
Win-
dows, . -
. 1.9.
, Borland C++ 3.1
, .
File | Exit.
" " Windows <Alt>+<F4>.


Borland C++ 5 . -
: .

File I New I Text Edit.
1. Borland 471

View Project Script X00' Debug Options Mndow help

, 1,9. Borland C++ 5

(. ), -
. ,
.
, ,
getcho conio.h.
,
""
( ). conio.h , -
Borland, Visual C++ 6.
. :
File | Save File | Save As;
Of <Alt>+<F> <S>;
1 <Cirl>+<S>.
(. 1.11), -
nonamexx.cpp (
).
<Enter>.
,
File Save As, .
472 IV.

File Edit Search View Project Script Tool Debug Options WJndow

, .1.10.

& Save File As


_-._^_.__^__-_^___^- __--~. ^
:

'\ *i| arraydin.cpp JJ3 |.


3 harp int. cpp ci] refer .cpp
!
i*i^
j-i] Decode, cpp ;C| returnstr.cpp

JKJ dimarray.cpp [Oi) nghrpointcpp
I" -4 cf| First. cpp ^ strcspn.cpp
L * ci] funcpoint.cpp c| strings. cpp
|c| macros. cpp [e^ strijsije.cpp
|c:)mnogofun.cpp jo:| swapointer.cpp
J ^newOOOl.cpp
cj] prajOOOO.cpp

, ici] projOOOS.cpp

JP |J3prolQ10I,tpp

% _
r
: 14 jj j

: |D*+ souice (",cpp;".c) ^~3

. 1.11.
1. Borland 473

:
File | Open;
<Alt>+<F> <>.
,
. 1.12.

& Open file


Filename: Directories:
c\.. VnpeKTbi-bcSVwork
Cancel
^_.
. chafpoinl.cpp ! > DDCumenls andSc Help
j Decode.cpp
I dimarray.cpp
first
! First.cpp !
1 Funcpoinl.cpp
] macro s.cpp
1
mnDgo/un.cpp

Drives

, 1.12.

, , -
" " (<F9>, <Alt>+<F9>, <Ctrl>+<F9>), -
Borland C++ 3.I.
, . 1.13.

. 1.13.

/ , -
, . 1.14.

. 1.14.

,
. 1.15.
474 IV.

eyjocunwnts and SeHiugs\Fiisl\MoM rtony

. 1.15.


, .
"" ,
,
. , File 9 ,
. ,
File I Open (. . 1.12)
.


-
, . -
File | New ( Project.
,
. 1.16.
( Project Path and Name) no .
Target Name.
:
Target Type Application [.exe];
Platform Win32;
Target Model Console.
Static,
Libraries .
1. Borland 475


m
: OK
i **
X Cancel 1
1
pTojoms" \u. Browse

Ad ancedl
Class Libiary MFC, j f
Dynamic Library [.dll] Kelp |
. :
Static Library [for ,] [.lib]
Static Library (for. dll]i I'b] QWL
Import Library [.lib] _

Platform; i : awcc VBX


CTL3D
Target Model; Libiaries:
i Console |H| OL
.". .'.""" . - - - - - - ' |

! . . -, ' Muftithcead

Target Expert
. 1.16.

: ^documents and SGtlmgsUirstUwH ^/ bc5\wof}<\proiQ006.ide


[ ] " axe"

. 1.17.

,
. 7.
:
1.
(delete node).
2. (
. 1.18).

c:\doctfments and settings\fifstW>H '1\ bc5iwoih;\pfoj0006.ide

.cpp t.cppl

. 1.18.
476 IV.

3. ;
(. . ),
.


-
. -
, "" --
, .
.
.
Debug.
" ", -
Borland C++ 3.1, - , -
Windows.


, Help. -
, :
Contents , ,
;
Keyword search ( <F1>) .
<F1>
;
Windows API , Windows
.
Windows ,
. 1.19. -
.
-
C++ (. 1.20).

. ,
, -
, . 1.21.
,
. 1.22.
(, vector), -
2 , 3 -
, .
1. Borland 477

: Borland ,O+ User's Guide

; I

Bbi&pifre hawwwe ""


, . " ".

Environment Options
Project Options
Project Manager
!
ClassExperl
TargelEnpeit
Debugger
Browser
Hew to...

. j

. 1.19.

; Borland C+* User's Guide

] I

..

2. Q ""
getebrk _

gelchar
gelche
Li'.-l " i: .; .
GelCilpboardToken
GetCommard
gelcutdir
gelcwd
geldate
geldfree
Qelolsk
geldla
getenv
get! at
get! aid
g elf lime

I
liJ

. 1.20.
478 IV.

,
,
.

^ ,
[ ]
. .

- ,
"".

(* [
, <"" >
<

>

. 1.21.

I : Standard C++ Libiaiy Help

! |

1,.
1 vector!

2, .

"". -

Adapting global functions


Adapting member functions
Adjacent differences
adjacent_djffeience
adjacentjind
advance

; 1. ., , ,

. 1.22.
1. Borland 479

Borland C++Builder 6
, , -
2002 . -
, Borland, ,
Visual C++ 6. ,
STLpoit (
3). -
, -
, Windows API.
.

IDE
Win-
dows, . -
,
(. . 14). -
, File | Close All.



File I New I Other.

Data Modules ] Business | Web Documents ! WebSnap ] WebServices


New | ActiveX I Mullilier Forms Diabgs ] Projects

Application Batch File File CIX CL* Data


Application Module

CLX Form CLX frame CLX Thread Component


Object

Control Panel ControlPanel Cpp File DataModule DLLWizaid


Application Module

.:: l-ton t

' I Cancel Help

. 1.23. { )
480 IV.

, ,
Borland C++ Builder 6.
. 1.23.
Console Wizard New -
. , . 1.24.

Console Wizard

Source Type
; UseVCL
" Use CLX
" Multi Threaded
] sole Application

Specify project source


.

01

. 1.24. Console Wizard

Console Application
C++. -
, . 1.25.
.

Unill.cpp
Pioiectl - Classes

int n i a m ( i n t acgc, char" a r g v [ ] )


i(
return 0;

1: 1 Modified Inser!

. 1.25.
1. Borland 481

Save Uiiit! As

| increment.cpp jjf] undefined.cpp


|C}| Inherited.cpp itj visok.cpp
[Cj Adapter, cpp g| keys, cpp

array 5. *i] pow2.cpp
;| behavior,cpp d| Queue.cpp
!j<] binfile.cpp
iJJ-]cassert.cpp jCl| Rus alpha, cpp
[CJ] CopyFile.cpp [| strueonst.cpp
M] File 1.cpp M Structure, cpp
51 File?, cpp Cllstrusize.cpp
-*,
^TempFunc.crjp
;jK] Functions.cpp Jitypeid,cpp
iicj
*-" J Functors.cpp ^Jjumolfunc.cpp

1
: 3 CcwpaHi-
L:.;..........

: [C++Builde'unit [*) !

j

. 1.26.

Sara Pfojecll As

iRualpha.bpr
(strrus.bpr
!cassert,bpr Istruconst.bpr
I Cop/File, bpr ! Structure, bpr
Istrusize.bpr
! functions, bpr jTernpFunc.bpr

I Functors, bpr Slypeids.bpr
! increment, bpr Surnolfunc.bpr
I Inherited, bpr ! undefined, bpr

. 1.27.

163. 1264
!

482 IV.

. :
File | Save File | Save As;
<Alt>+<F> <S>;
<Ctrl>+<S>,
(. 1.26), -
unitl.cpp.
. ,
( File | Save Project As) -
( File | Close All) -
(
. 1.27).


, .
( bpr),
.
"" , -
File | Reopen. , -
,
File | Open Project ( <Ctrl>+<Fll>).
, (. . 1.27),
.

,
/ ,
" " (<F9>, <Alt>+<F9>, <Ctrl>+<F9>), -
Borland C++ 3.1. -
, . 1.28.

. 1.28.



Borland, .
Run.
1. Borland 483


Borland C++ Builder 6 , ,
, -.
Windows, -
. -
24 ,
, . 1.29.

: ^Documents a nd SettingslFirstUpplieation DatalMicrosotttlnte... [W


Cgpeitc ^

> - ; X


dSMS 5 Hefc Files
Runtime Library Reference
C++Builder Command-line Tools
C++Build et Language Guide
CodeGuard User's Guide
Creating Custom Components
'ij Customizing Help
Developing COM-based Applications
; Developing Database Applications
Developing Distributed Applications
Error Messages and Warnings
Image Editor Help
Integrated Translation Environment
InterBase Express Reference
Help, Intranet ActtveX Online Hdp
Object Pascal Reference
: 222003., 5:26 Programming with C++Builder
Quick Reports Help
SQL Builder Help
Standa(d C++ Library Help
TeeCnart Help
Using C++6uilder
Visual Cwnponent Library Reference
What's New in C++Buiider
WinSght Users Guide

. 1.29.

, -
(. . 1.19). C++

C++.
2


Microsoft Visual C++ 6
1998
98- . -
.

IDE
Windows, -
. -
. 2.1.

File | Exit Windows -
<Alt>+<F4>.


Visual C++ 6 . ,
, .
, ,
, -
.

. -
-
.
. -
.
486 IV.

Build /DebJB X,. Find in Files 1 \FindinFilas2 \ Resuli \ SQL Debusing /

. 2.1.

Files Projects I Workspaces Other Documents I


=
^|ATLCOMAppWiza'd" i eject aame:
2] Cluster Resource Type Wizaid
2] Custom AppWizard
.ogaliorv
^Database FVojecl
^DevStudio Add-in Wizard CADocumenlsand Srttings\Fjrst ^.J
JJlSAP! Extension V^irard
iJpM^sJie
JJ5 MFC Ac!ivd< CdnlioWizard *' Create \ workspace
^ MFC AppWiiard |dll] " jidab -..:[-;r' -> - -j; ;-'
MFC AppWIzard (exe] ^)
^4New D al abase Wisard
JT| UiililyPioiect
^] Win32 Application

^laltorms:
Win32 Dynamic-Link Library g___
j Win32 Static Libraiy

. 2.2. New, Projects


2. Microsoft Visual C++ 6 487

1. File | New.
2. (. 2.2) Projects
Win32 Console Application.
3. Project name, First.
Workspace ,
. 2.3.

1
Workspace
1
Workspace ' f i r s t : 1 p t o j e c t ( s
! first files

it] Header Files


Files

dassView | J) FiteView I

. 2.3.

(
) ,
Debug 5 .
, , .
dsw. -
, .
488 IV.


.
1. File | New (. . 2.1).
2. (. . 2.2) Files
C++ Source File.
3. File names, First.
(. 2.4).

First -Microsoft VKuolO


'9* Edit View [nsett Ercnert guild loot undow Help

>? Weikspae* Ri Fiist.c.jtji


1
' First : Ipi:o]ect(3
<103treaB.h>

ClassView _g FileView

. 2.4.

, Build
MiniBar, . 2.5.
,
. 2.6. , Borland
, (Press any
key to continue).
2. Microsoft Visual C++ 6 489

Run <Ctrl+FS>
Build Breakpoint
<F7> <F9>

Compile Go <F5>
<Ctrl+F7>
Stop build
<Ctrl+Break>
. 2.5. Build MiniBar

. 2.6.


,
( File | Close Workspace).
,
File | Resent Workspace . -
, , -
File | Open Workspace.
(. 2.7) dsw.
, -
.
.
490 IV.

Open Workspace

: | Q

Debug

: Workspaces (dsw;.mdp|

. 2.7.


Visual C++ 6
(Win32 Debug), (Win32 Release). -
. -
().
Build | Set
Active Configurations -
. ,
Project | Settings.

Debug, -
Debug, . 2.8.

. 2.8. Debug

Visual C++ 6 -
: ,
,
.
2. Microsoft Visual C++6 491


Debug | Go ( '
). . 2.9.

. 2.9.


, Stop Debugging ( <Shift>+
3

C++

http://www.research.att.com/~bs/homepage.html C++,
C++, .
D http://anubis.dkuug.dk/jtcl/sc22/wg21/ C++.
, , -
.
http://msdn.microsoft.com -
Microsoft. , .
http://rsdn.ru .
-
, ,
.

, RSDN.
. ,
.
http://clubpro.spb.ru/ --
C++, rsdn .
www.cuj.com C++ "C++ users
journal", , -
, .
http://www.gotw.ca/gotw/index.htm Guru Of The Week, -
. -
, .
[35].
http://www.aristeia.com/right_frames.html - ,
C++.
494 IV.

http://staff.develop.com/slip/ -- ,
C++.
http://www.firststeps.ru/ " ", -
.
http://www.cppwmeste.r2.ru/ "
".
-
:
http://www.codetools.com/
http://www.codeproject.com/
http://www.codenet.ru/
http://www.codeguru.com/
http://www.doc.ic. . uk/lab/cplus/c++. rules/
http://www.helloworld.ru/
http://www.emanual.ru
http://www.sourcesl.ru
http://www.rusdoc.ru
[25]
STL, -
:
http://www.sgi.com/tech/stl/ - STL;
http://stlport.org/ , ,
, Inprise Borland C++ Bor-
land 6;
http://www.boost.org/ "" , -
, -
. .
http://gcc.gnu.org -
C++.
http://www.digitalmars.com/ --
.
- ,
, .
,
. ,
Google.
4

,
.
.

1. . C++. C++ In-


Depth, . 3 / . . .: , 2002. 336 ., .
2. . STL C++ / . . .:
, 1999. - 240 ., .
3. ., ., . : , , -
/ . . ,: , 2001. 768 ., .
4. . - /
. . .: , 1997. 464 ., .
5. . . 2- / . . -
.: , 2002. - 272 ., .
6. . . : / . . -
.: , 1988. 191 ., .
7. .
C++ / . . .: , 2002. 320 ., .
8. . - -
C++, 2- . / . . .: ; .:
, 1998. 560 ., .
9. . + = / . . -
.: , 1985. 406 ., .
10. ., ., ., . -
. .
/ . . .: , 2001. 368 .,
.
496 IV.

11. . . C++. . - .: ,
1996.-272.
12. . Windows 95/NT / .
. .: -, 1997. 640 ., .
13. . ., X. . C++.
- UML /
. . - .: , 2002. 1152 .
14. . ., . .
C++. , 3- / . . .: ,
2003. - 848 ., .
15. ., My . . C++.
C++ In-Depth, . 2 / . . .: , 2002. 384 ., .
16. . ., . / . . -
.: , 2001. 381 ., .
17. ., . / . .
3- ., . .: , 2001. 352 ., .
18. . . Borland C++ 5 / . . .: -,
2000.-976., .
19. ., ., . Microsoft
Visual C++ 6.0 / . . .: ; .:
, 2001. 864 ., .
20. . . C++. C++ In-
Depth, . 1 / . . .: , 2002. 256 ., .
21. . Borland C++ 5. / . . .: , 3997.
560 ., .
22. . . C++. . .: - - -
, 1999. - 400 .
23. . C++. 50
. . /
. . .: , 2000. 240 ., .
24. . C++. 35 -
.
/ . . .: , 2000. 304 ., .
25. . STL. -
/ . . .: , 2002. 224 ., .
26. . ., . . Visual C++ MFC / . .
.: -, 2000. - 1040 ., .
27. . . STL. .: , 2003. -
262 ., .
4. ^ 497

28. ., ., . . . MFC Visual C++ 6. -


/ . . .: , 2003. -
992.
29. . . C/C++. -
. .: , 2002. 464 ., .
30. . ., . . C/C++. -
: . .: , 2002. 240 ., .
31. . . C++. .: , 1996.
560 ., .
32. ., . : -
/ . . .: , 2002. 688 ., .
33. . Windows :
\132- 64- Windows /
. . .: ; .: , 2001. 752 ., .
34. . . ++ , . -
. : , 2003. 428 .
35. . C++. C++ In-Depth,
. 4 / . . .: , 2002. 400 ,, .
36. . , 5- . /
. . .: , 2001. - 672 ., .
37. . C++, . . / . . -
.: ; .: , 2001. 1099 ., .
38. . C++. -
/ . . .: , 2000. 448 ., .
39. . C++. .
. . / . . - .: , 2001. -
.
40. . C++. -
/ . . - .: , 2002. - 496 .
41. ., ., ., X. C/C++. -
. .: , 2001. 640 ., .
42. . C++ / . . -
.: , 2001. 336 ., .
43. . . Win32 / .
. .: , 2001. 464 ., .
44. ., . C++ / . . .:
-, 2002. 416 ., .
498 IV.

45. ., . . . -
, .
/ . . .: , 2001. -
736.
46. . Visual C++ 6: .: , 1999. 576 .,
.
47. . ., . ., . . -
. , 2002, 8.
48. . C++ / . . .: -,
2000. 416 ., .
49. . C++: . .: , 1999.-
320 ., .
50. . Windows. .: ,
2002. - 1072 ., .
51. Programming languages C++. International Standart ISO/IEC 14882:1998(E).

27
28
: 28
207, 408 310
195, 408 312
: :
152 181
149 115
152 267
148 412
145. 147 :
145, 148, 154, 177 195
265 195
v 149 195
263 195
377
67
212
265
274
224 35, 47
209, 216 122, 226
210 370
210 33
272, 398 67
210, 214 ctype.h 95, 97
212 math.h 65
61, 176, 195, 210, 334, stdarg.h 270
401 stdio221
195 stdlib.h65, 100, 156
210 STL62, 214
215 STLport 222
: string.li 135, 159, 188
382 / 221, 241
381 . . 500
500

(.): 125
62 22. 56
183 34, 55
128 47
61 19
62, 176, 183, 196, 340 125
26, 67 :
349 281
349, 351 367
/ 225 310
225 307
395
372
332
: 371
21, 55 371
221 389
224 405
225 276
127, 225 275-277, 290, 296, 365
55, 225 261
223 7476, 401
d
/: 212
251 298
423 372
251 20, 24, 34, 36, 49. 50-52,
257 54-56, 59,60, 63,71,73, 124
: 154
203 107
203 94, 106, 113
199, 239 24, 43, 53
198 27
199 57, 59
202 43
417, 418, 419 411
323
409 411
:
253
212
201
444
281
259, 260
262
398

: 205
173 171
77 162
171 ,
: 344
252 331
221 367, 409
501

373 :
330, 342 "" 37
333 "" 37
367 19
: 37, 95
203 38
171 21
167 74, 76
: 73
pragma 141 124
using 188
126
:

267, 268 21, 62, 70
171, 195 432
136 429
265, 270 :
203 358
323 357
187, 195 403
380 358
129, 178 21, 50
165, 264, 408 18
362 19
184 229
203 378
196 100
184, 195 , 166
249 69
- 356
181
21
381 146
377 128
228
19, 62
445
:
126 116
351 116
158 :
188 197
179
428 311
191 203
264 164
- 296 107
249 119
19, 21, 23, 30, 128
36, 38, 43, 63, 101, 184, 314, 323, 347, - 311
373 . . 502
502

(.): 347
186 365
80 324, 370
185, 190 354, 358
388 324
68, 77, 87, 204, 361, 40] 427
: 387
37, 50, 56 374
37, 50
361
21
361
409
362, 363
439
327, 329 363
417 365
430 408
374 184
375 403
353 :
348 452
348 :
357, 359 49, 158, 420
184, 187, 195 420
224 :
206 106
207, 208 107
208 109
209 17
206 18
206 :
206
450
206
444
209
445
206
205 377
- 204 224
206 398
> 208 398
207 34, 71
205 "" 37, 43
"" 37
28
41
228 41
. 229 36, 59
231 152
229 39
190 38
229 Windows 230
380 138
322 47
409 412
362, 363, 366 122, 124
503

20, 22, 46 100


41 102
30 20, 47, 213
149 boolalplia 184, 241
38, 42 end] 233
41 244
131. 365 241
365, 366 241
341 105
312 196
164, 307, 366, 403 144
330 134
307, 308 114
365 114
307, 364 119
367 165, 330, 332
310 134
306 107
366 107
346 107, 121
. 452 119
105, 195, 407 106
114
STL238
121
172, 195
119, 166
STL 169
107
340
106
196
109
211
187
195, 200, 340
107
187
327
339
121, 123, 157, 190
171, 204 123
. 339 12]
439 134
370 163, 167, 168
345 162
173 3S9
143, 145, 206 109
149 191
:

431
431
432
: 431
224, 236 446
240 431
100,411 431, 442
NULL 149 387, 4 1 1
101 410
103 138
504

: 348, 349
RTTI 359 406
376 406
347, 354 366
350 317, 319, 321
93 215, 402, 403
50 :
, 201 38, 42
151 177
: 21, 26
362 161
362 377
377 148
403 390
:

438
433
409 433
357, 361, 365 451
363, 368 428, 439
363 425, 433
361 19-21, 23, 28, 30, 32, 50, 51,
409 55, 57, 58, 60
363, 368 typedef43, 112
36! 20, 26, 225
60, 88 19, 31, 67
19, 23, 34, 46, 225
278, 281 28, 33, 34
152 348
39, 45
357
27, 28, 244, 396, 429,
436
379, 380, 381, 391 349
77 39, 45
260 34
382 52
228 23, 26, 30, 31, 32
164 23, 25, 28, 46, 54 '
82 30, 31, 32, 33, 54, 55, 106
387 29, 32, 54
: 31
349 D
30

332 32
287 " 30, 31,56
177, 268 20, 34, 59
: delete 164
337 new 164
366 sizeof 36
193, 307 37, 51
343 56, 317
505

145, 250 111


223 113
/ 235 107
298 59, 186, 263, 366
223 165, 327
56 101
403
37
44
203
55, 56, 59, 72 :
24, 46, 53 165
46, 53 167
57, 198, 328 175
37, 152 165, 166
46, 53, 60 387
53 :
51 148
115 166
235
319 199
V 145
I1U/1J- |/^\- 1TJ 109
44 289
319 394
' 319 260
57 405
37, 49, 71, 72, 328, 332, 265
366 263
54 112
54 144, 263
79, 137, 365, 392
382 176
146, 164, 206, 344 264, 270
51 :
49 204
53 289
37, 44 426
187 179
53, 329 84
52 144
235, 253 72, 73, 91
: 79
280, 411 62, 67
280 189
407 71
: 63
59 63, 70
main 179
348 94, 343
379
407
236
408
232
204, 407
506

157 364
301 57
315 278
337 290
330 :
354 249
339 249
299 :
307 111
90
211
300
<>
402
261
305
89, 361
303
294 374
- 316 374
301 374
361 374
94 366
392 :
110, 113 204, 212
: 175
70, 85 :
70, 82, 84, 297 195
70 195
: 195
154 195
154 195
227 195
21, 24, 26-28, 30, 31, 34, :
38, 44, 50, 51, 54, 56, 59, 60, 64, 71, 423
221, 2*22, 232
77, 78, 87, 379
221, 222
164, 170, 174, 327
222
21, 22, 29, 31
249
21
31, 38 227
426 223
26, 67, 77, 78, 84, 278, 364 228
82 234
86 222
81 222, 257
21 228
> 48 222
181 :
80, 281 407
29, 32 404
21, 58 :
145 368
146 344
: / 223, 248
250 394
6 250
267
507

401

314
150, 248, 401 239
297 :
51, 148
184
374
166
314 126
50
178
375 278
375
207
375
229
52
:
52, 150
141
18, 99
246
18, 99
/
100
241
100
272
99
288
356
283, 284
188, 418, 419, 429
284
234
283, 284
395
272
<> 450
407 276
411
253
276, 278, 284
:
273
179, 421
273
179, 425
274
309, 323, 330
274
:
198
367
129
236 :
274 445
377 446
383 171
382 164
383 420, 447
380 421
188 227
380 227
188, 191, 380 242
76 226
295 251
80 223
79 251
295 421
157 223, 229
- 295 227
- 175 201
508

19, 21, 222 C++ 43, 60, 70, 83, 88, 94, 150,
: 159, 179, 180, 183
347 18, 183, 195,
348, 350, 354 405
357 186
347 / 221
249
447 204
447
: 405
. 104 191
92, 115, 332, 348 240, 244
436 222
111 222
111 123
: 123
117, 329 105, 127
218 129
117 129
: 169
235 128
235 129
235 130
235 131
: 128
413 140
413 169
: 171
17! 129
365 128
353 132
171 :
171, 284 280
171 23, 25, 29, 238
264 / 225
340 201
172 273, 279
269 273
89 256
72, 394
270
72
72
233
93
233
265, 267
19,21, 36, 52, 56
343
70, 265
174
266
259
184
260
19, 63, 67,
175
288
174, 332
21, 46
509

35, 43, 53 153


164 153
173 203
348, 350, 352 187
204 "" 419
38, 43, 184 147, 369
42 157
191 395
24 157
387 176
158, 332 165, 343
267 151
21, 58 149
42 175
137 409
42 371
49, 51 203
35 179
46, 47 371
421 270
46, 121 169
395 317
396 287, 288
229 149, 167, 169, 175, 394
161 147, 388
43 162
19,42, 106 180
21 145
. 48 151
204 389
: 147, 268, 395
64 166
24
242
31
377
23, 34
287
174

171
:
286
227
173.
248, 249
173
228
171
242
143
228
152
229
147 229
203 228
148
247
148 221, 228
153 228, 231, 232
510

: 175
235 64
241 251
, 453 136
: 425
452 > 334
444 378
443 63
276, 287 136
: 63
295, 317 353
295 301
: 333
299 123, 135, 162
299 285
388, 402, 404 402
217 63
218 425, 427, 429, 432, 439
217 69
407 428
18,62,409, 414 244
API 417, 428 387
API Windows 229 70
inline 331 79, 89
main 19, 68, 179 190, 240
377 85, 88
277 101
96 155, 391, 396
373, 374 229
214, 262 438
259 95, 214, 402
158, 262, 264 124, ISO, 313
251 243
[68 63, 69
251 251
63, 67 426
157 115
67 116, 272, 273, 274, 278,
289
352
272 264, 265, 395
65 354 -
18, 378,425,428 335
377 302
116 392
350 353
246, 252 218
19, 67, 69 62, 71, 354
246 414
67 136, 143
131 67, 69
511

0 176 0 40, 58 '


0 403 0 35
0 252
0 92, 115
0 135
0 283 345, 387
0 197
X 0 93, 369, 413
0 369
420 0 410
0 133
0 403
0 412
425, 428, 0 133, 172
443 0 92, 114, 144, 411
0 - 402
- 242
434
,

34
0 40, 47, 58
Q 35, 40, 41, 58
0 40 430, 434
0 34, 37, 38, 40 0 444, 446
0 40, 47 0 431, 445
0 40 0 431
0 35 0 450
0 123 0 444
0 35, 40, 41, 58 0 430
0 34 0 435
0 36 0 430, 444, 445
0 58
0 35 279
saftline
119991 . ,
. , . 8
.:(095)232-0023
w w w . s o f t l i n e . r u e-mail: info@softline.ru



SoftUne? Microsoft
, .. -
,
- -
Borland
.
SoftLine-direct
www.jo fj [in.
SoftLine
. .rosyitdint


, SoftUne?
COMPAQ.
(Microsoft, CA/Ptatinum, Rational, SilverRun,
Quest).
Si (Centura, Merant, Microsoft).
? (PlanisWare, PlanView, Microsoft).
( Allaire, Borland, IBM, Microsoft, -
Allround Automation, ComponentOne, Crystal Decisions, Janus,
Sitraka, Stingray).
9 {Compaq, Fuji, Intel, MainSoft, Sun, Sybase, Tenberry).
IB (NuMega, Intuitive Systems, Segue).
(InstallShield, Wise Solutions).
(Remedy, RoyalBlue, CA, Network
Associates).
IS (Adobe, Allen Communications, cl>ck2learn.com,
eHelp, Macromedia, Quest, Ulead). sitraka "
SoftLine aire>
a SoftLine, -
, , -
GA
(
), -
RAD - . - InstallShield
- ,
, -
Microsoft Dei/eloper Network, Sun Developer Essentials -
- - SoftUne Support Subscription, -
- .
SoftLine .
i SYBASE