You are on page 1of 337

by S t e p h e n R.

Davis

IDG
BOOKS
WORLDWIDE

iDG
An

Books Worldwide,

International

Data

Group

Inc.
C o m p a n y

F o s t e r C i t y , C A C h i c a g o , IL I n d i a n a p o l i s , I N N e w Y o r k , N Y

- *
2003

32.973.26-018.275
94
681.3.07
"""
.. . . .
. . .
""
: info@dialektika.com, http://www.dialektika.com

94

, , .
C++ "", 4- . : . . : . :
'"", 2003. 336 . : . : . . .
ISBN 5-8459-0160- (.)
, ,
C++. : -
.
1
C++, "" ,
"". C++
, .
. ,
C++ , ,
, .
.
32.973.26-018.275


.

,
, ,
IDG Books Worldwide, Inc.
Copyright 2001 by Dialektika Computer Publishing.
Original English language edition copyright 2000 by IDG Books Worldwide, Inc.
All rights reserved including the. right of reproduction in whole or in part in any form.
This edition published by arrangement with the. original publisher, IDG Books Worldwide, Inc.,
Foster City, California, USA.
Windows is a trademark of Microsoft Corporation... For Dummies, Dummies Man, and the IDG
Books Wordwide logos are trademarks under exclusive license to IDG Books Worldwide, Inc., from
International Data Group, Inc. Used by permission.
ISBN 5-8459-0160- (.)
ISBN 0-7645-0746- (.)

"", 2001
& IDG Books Worldwide, Inc., 2000


I. C++

21

1.
2.
3.

4.
5.
.

23

33

41
46

55

67

6.
7.
8. C++
9.
10.
11. C++

69

79

90

100

111

119

III . ""

139

12. -
13. C++
14.
15.
16. : !
17. :
18.
19.
20.
IV.

141

144

149

160

173

179

187

201

211

225

21.
22. -:
23.

227

233

245

V.

265

267

280

286

299

306

24.
25.
26.
27.
28.

VI.

315

29.
.

317

322



C++
,



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

I. C++
1.
C++






GNU Windows
GNU C++

C++




Convert ...

2.


, C++

,




3.

12
13

14
14
14
14
15
15
15
16
16
17
17
17
17
17
17
18
18

19
21
21
22
22
23
23
25
26
26
27
27
27
27
28
28
29
29
30
30

31
31
32
32
33
34
35
36
36
37
39






4.



C++








5.


while

for



39

40

41

42

42

44

44

44

45

47

47

48

48

49

49

50

51

52

53

53

55

55

56

57

59

60

62

63

II.

65

6.







7.








,
C++
wchai i

8. C + +

67

67

69

70

70

73

74

75

77

77

78

80

81

81

82

82

84

84

86

87

87

88

88

89


90


91


93


93


93


94


94


94


96


96

9.
98


98


99

100


101


104


104


105


106

main()
107

10.
109


109

110
FunctionDemo

111
sumSequenceQ

111
MainModule.cpp
113

113
GNU C++
114

Visual C++

114
#include

115
C++
116

. C++
117


117

117

"" 1

118
"" 2
121


124


124


124


125


126


128


129


129

BUDGET
132

I I I . ""
137

12. - 139


139


140

"-"
140


140


141

13. C + +



14.


-
-
-

-
-
-


-
-
-
15.














LinkedListData
16. : !

" ..."
17. :



142

142

142
143

144

147

147

148

148

149

149

150

150

151

151
153

154

155

156

158

158

159

160

160

161

161

161

162

162

164

165

165

165

166

167

168

168

171

171
171

172

173

173

174
174
174

177

177

178

178

179
182
182


18.


main()


,
19.




"" ""

20.


-
-
-
BUDGET2.CPP
IV.
21.




22. -:







23.






: BUDGET3.CPP

10

183

185

185

186
186

188

190

191

195

196
196

196

197

198

198

199

199

199

200

201

203

206

209

209

209

210
210

212

212

215

223

225

225

227

229

229

231

234

235

237

239

241

243

243

246

248

249

251

251

253

V.
24.

263

265

:

?
operator+()
operator*-+()
-





25.
, , ...





26. -
...
fstream
strstream


""
27.


?
28.




265

266

269

269

271

272

273

274

275

276

277

279

279

280

281

282

283

283

285

285

287

290

291

294

296

299

300

301

303

306

306

307

308

312

313

VI.
29.

315

317

317

318

318

318

320

320

320

320

321

321

322

327

11

,
" " ,

. (Stephen R. Davis) ,
, C++ " ", More C++for Dummies Windows 95 Programmingfor Dum
mies. Valtech,
(, ).

TxuuoqafiHOctnu
, , , ,
. ...
"" .
( Corder)
(Claudette Moore), .

, (Susan
Pink) (Kelly Ewing) (Colleen Wil
liams). , (Greg Guntle),
(Garrett Pease) (Jeff Bankston) (,
) .

(Suzanne Thomas), . ,
, , ,
.

.
, .
, , ,
.
, , ,
. , ,
.
, , More C++for Dummies,
, .
C++,
, : srdavis@ACM.org.


0<5*
C++ " ".
C++ ,
.

/
, , C++.
: -
( ,
).
C++,
"" , "".
C++ ,
.
.
, ,
, . ^ C++
, , , .
Windows.
. C++, Windows 95
Programming for Dummies.

tfuucoe

C+

C++ -
, ANSI
(International Standards Organization ISO). C++ ,
,
.
, C++ .
ANSI ISO C++:
.
, C++

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

14

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

,
, ,
:
i mom!

:
//

void ma in()

, ,
, ,
.
, ,
.
, myFavoriteFunction () .

. , :
<Ctrl+C>, , <Ctrl>,
<>. "" .
, File^Open.
File
.

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

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

15

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

/ &

:
| / ;
S ;
| S .
.

.
.
.
. ,
, , <Enter>.
.

& ...
2, 3 4
BUDGET. ,
.
, C++
. ,
, . ,

. ,
.

BUDGET. , .
, ,
BUDGET
- . ,
. ,
(, , ).
, , BUDGET,
C++.

16

/. & C+ +
C++.
.
.

2.,
C++

.
:
C++. , .

& 3. "
:
-
.


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

\

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

5.*

C++.
. ,
, .

6. qecsufltca
""
?
.

17

/
,

, .

. ,
.

.
, ,
.
,
!

18

C++

/ ...
,
, .
.
,
.,, :.


& ...
s C++
^
S ..
S
S
S
S

v., . , .
.
, .
, ( !), , .
,
, , - . ,
: " , ".
. (
. , ,
- ,
.)
,
. .

, C++.
, .

C++

Muitix.
, , ,
(, ). , ,
.
, ,
Muitix ,
Unix (Un-ix, Mult-ix , ?).
,
. ,
.
, , ,
.

1.

21


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

nfwzficuuta
, ,
C++.
(, , ).
C++ . , Microsoft Word
. DOC MS DOS ..
. , , ,
.
,
,
1
. - . .
C++ .
, ? . ...

//
;
.- ,
.-, . ,
, .

.
.
Visual C++ Microsoft.
, ;
-
(,
, Windows
). ,
C++, GNU C++.

, , , . . .

22

I. C++

Internet.
-
. GNU C++ .
GNU "GNU Unix"
(GNU is Not Unix). C++,
. GNU
,
(Free Software Foundation).
GNU C++ ,
C++.
C++ "
2
( C++) .
GNU C++ Windows.
, ,
Visual C++.
, www.deloreon.com (
, GNU C++),
GNU C++ ,
.
. , ,
.

nefitfou//
C++.
CONVERT. CPP,
.


C++
. GNU C++
r h i d e . ,
GNU C++ . r h i d e
Convert. .
1. MS DOS MS DOS
1
Windows ^.
GNU C++ , .
r h i d e MS DOS.
2. : \CPP_For_Dummies\Chap01 (,
).
,
MS DOS, .
, ,
C++,
. . .

1.

23


.
ChapOl, r h i d e MS DOS.
, New File. ,
(
).
: ,

. C++ : ,
1.

rhide
Windows-.

Windows "" ,

r h i d e .

, r h i d e . r h i d e

, (80x25

). r h i d e , :

, , ,

"" .

, r h i d e

"" Borland.

//

// :

// Fahrenheit = NCelsius
* (212 - 32)/100 + 32
//

#include <stdio.h>

#include <iostream.h>

int main(int nNumberofArgs, char* pszArgs[])

//

int nNCelsius;

cout << " : ";


cin >> nNCelsius;

//

//

int nNFactor;

nNFactor = 212 - 32;

//

//

//

int nFahrenheit;

nFahrenheit = nNFactor * nNCelsius/100 + 32;

C++
, , int main i n t Main, ,
. . .

24

I. C++

//
cout << " :
cout << nFahrenheit;
return 0;
Save As File
Conversion.cpp.
,
C++!


Conversion, cpp
.
Make Compile
<F9>. r h i d e ,
. , C r e a t ing Conversion. no e r r o r s .

GNU C++
,
.
, .
Bad command or f i l e name , MS DOS
gcc.exe , .. GNU C++.
c : \ d j g p p \ b i n ,
gcc.exe . GNU C++ ,
a u t o e x e c . b a t SET PATH=c: \djgpp\bin ; IPATH&.
.
g c c . e x e :
Conversion.cpp:
No
such f i l e
or d i r e c t o r y
{ENOENT) , gcc (
MS DOS). ,
: \dj gpp\dj gpp. env LFN Y.
\
: GNU C++ , i
. , .
;
GNU C++ -
, .
, .
, ,
Conversion, cpp.
, 13 c i n > > n C e l s i u s ;
cin>>>nCeiEius;
, .
r h i d e :
Compiling: Conversion.cp p
I n functio n ' i n t m a i n ( i n t , cha r * * ) ' :
Conversion.cpp(13) E r r o r : p a r s e e r r o r befor e
There were some e r r o r s

1.

'>'

25

, GNU C++
">>>" 13- .
parse ,
C++.
, ">". , <F9>,
Ccnversion.exe.
1

C++

, C++ , .
GNU C++ ,
?
. GNU C++ ,
">>",
.
: , .
, , GNU C++
.
; , .
.
, . ,
GNU C++ , . ,
] , ?..

n/tozftaMMbt
.
CONVERT.EXE .
.
GNU C++, Run
Run <Ctrl+F9>.
,
. - ,
100. <Enter>
, .. 212. , r h i d e
, ; r h i d e
,
. " ",
.
, User Screen
Windows <+5>.
MS DOS. 25 ,
.
! GNU C++, ,
.

GNU - Windows
, GNU C++ Windows.
Windows- GNU C++ ,
, Visual C++, .

26

I. C++

Windows- -
. Ccnvension.exe 32- ,
Windows, Windows- .
, 32- 16-,
. ,
Windows. , GNU C++,
MS DOS.
Windows- :
. C++
Windows-.

GNU C++
GNU C++
r h i d e . , ,
<F1>. .
Helpolndex.
, GNU C++,
, (,
Microsoft Visual C++). " i n t " <F!>,
, , ,
. GNU C++
. ,
C++, GNU C++
.

nfwtfiaMM
, , ,
.
Conversion. , .

C++
, ,
:
// ,

#include <stdio.h>

#include<iostream.h>

i n t main(int nNumberofArgs, char* pzArgs[ ])

/ / . . . ...
return 0;
}

, ,
.


, Conversion,
. , GNU C++ ,
, .

1.

27

.
,
. .
C++ (//)
. .
, , ,
, 80 .
.
.
, .
, , /*
*/; C++
.
C++ (
) .
.
.
,
.
, , .


C++ , .
, Convert.
, . ,
, (
, ; ,
).
,
, .
, , ,
.
.
, .

( !).
, C++ . , f u l l
speed Fulispeed, , .

i n t nNCelsius; .
, . ,
.
.
"" ,
:
= 10
= 3 *

28

I. C++

, *.
? - .
10,
20, 30 -1. .
= 10. C++,
.
C++ . , II,
nNCelsius. (
, ,
i n t i n t e g e r ) .
C++ .
-z a-z 4 .
, 0 9 (_).
, .
,
.
, myVariable.
, .
, , .

lengthOfLineSegment.


, cout cin , -,
I/O (input/output) ( ,
).
I/O " " cout
( "-"). C++ cout .
.
.
nNCel
s i u s . C++ .
10
C++. nNCelsius ,
.

. C++
, - .
, , . ,
, .
, Conversion " "
nNFactor

, ,
. . .

1.

29

. 212 32.
"'' ("-"), "212-32".


.
. "" (,
), "
".
, C+i " = "
. ,
" = " , , . ,
" nNFactor 212-32".

Convert ...
, Conversion, ,
. : " * "
, " / " , " + " . , ,
, .
nNFactor (, )
nNCelsius ( ).
100 32.
nFahrenhe.it.
" :"
nFahreriheit.

30

I. C++

C++ .
\ S , ,
.
.
X

1 .
1, .
C++ . = 1;

1 . , 3.
, C++ , .
, C++.

, C++, "",
.
.

[ - + 4 = v

.Hv_

, ,
. , C++ ( ,
!).
, :
i n t x;
= 10;
i n t ;
= 5;
, , ,
i n t ( ).
,
.

2.

31

//
, , ,
, .
:

= 1;
= 2.3
= " - ."
=

C+ + . ( , C++
. ,
5.) C++ _
.
, .
,
.
, .
ir.t:
i n t ;
= 1;
C++ i n t . ,
, .
.
, ,
6. C++, 90%
i n t 7 .
, ir. t
. ,
, ( ) :
. ,

.

.
.
, -
.

, ++
C++ i r . t .
,
.
, ,
. . .
; ,
. . .
-
. . .

32

I. C++


.
nValuei, nVaIue2, nValue3.

(n.Valuel f nValue2 ( nValue3)

/ 3

, .
, I, 2 2 5. 5 3, 1-|, 1,666....
( ), (
) ,
. 1,666 "" 1.

. (,
).
. ,
:
+ nValue2/3 + nValue3/3

1, 2 2, 0.
, , 1.
0, , , 0.
, , .


i n t
.
2 147 483 647, -2 147 483 648, ..
4 8.


i n t
. , , C++
, (
). ,
. ,
, , . C++
1.0 , 1.5.
1.
C++ ,
fican. " ", ,
( )
, .
, i n t :
float fV&lue;

, int
, int
, ,
int -2 ?2 +2^-1, . . .

2.

33

, fValue
f l o a t .
: fValue
. ,
.
( f l o a t ) :
1/3 + 2/3 + 2/3

0.333... + 0.666... + 0.666...,

1.666...

,
,
.
, .
,
, ( ,
i n t ) .

.
C++ ,
.
, , 1.0 1. 0.9 1.1?
1? C++ ,
.


,
, . 1000
,
200 .

.
,
, .


.
,
,
15 .
, 1/3
0.333....
, -
(
). , 1, 2, 2, ,
1. 666667.

34

I. C++

C + + ; ,
, 0 . 9 9 9 9 9 9 C + +
1.


f l o a t ,
.
i n t 2 .
f l o a " -18, .. 1 38 ^.
, C++
.
.
123 000 000
- , 123 456 789
.

*/
,
. . 2.1
C++ .
2.1. C++

int
float
double

OF

long


f l o a t : ,

char

string

________^_________

; , , .

"this is
string"
10L

,
i n t . GNU C++
Microsoft Visual C++ l o n g i n t

l V a r i a b l e long dVariable
double :
// 1

long lVariable;

lVariable 1

, , float
1ffs ; ,
. . .

2.

35

// double
double dVariable;
dVariable 1.0;
:
i n t nVariable = 1; //

. .
char ,
. ,
, ( s t r i n g
, ). 9,
" ", .
.
( ),
, .


(, 1, 0.5
*') , . = 1;
1 i n t . 1 long, n = 1L;.
: 1
, 1L .
, ,
.
1 1.0.
, double.
1. double, f l o a t .



char s t r i n g . ,
, . . 2.2
.
2.2.

\
\t

\0

\\

.
. ,
" \ "
:

36

I. C++

\t
. ,
, . "
, ,
, : \\.



"

C++ MS DOS
MS DOS
. , r o o t \ f o l d e r A \ f i l e
f i l e folderA, root .
, MS DOS
C++ . C++
, \\.
MS DOS root\folderA\file C++
root\\folderA\\file.

A
C++ .
, .
nValue :
//

// nValuel double

i n t nValuel = 1;

double fValue = nValuel + 1.0;

, ,
.
.
nValuel double.

, :
//

// fVariable nVariable

float fVariable = 1.0;

i n t nVar iable;

nVariable = fVariable;


, ,
(,
fVariable
nVariable).


,
, , , (
).
, d v a r i a b l e double.

2.

37

,
. , ,

( long ):
- nVariable = IVariable;

C++ .
i n t
. ""
; ,
;
.

int

long

float

'

sz

double

character
;

.-

string


(demotion),
(promotion).
C++
; , C++
.

38

I. C++

/ ...
S
^
s
^
^

*77

/ / ,
+S * - .
: , , ..
.
C++.
.
, ?
C++ ,
;
:
int varl;

int var2 = 1;

varl = 2 * var2;

, v a r l var2. var2
l, var l var2.
C++.

SuHa/i/
, .
v a r l op var2 op .
,
. , C++,
. 3.1.

3.1.

1
1
2
2

+ ()
- ()

++ ()

, 1

()

, 1

3.

39

.3.1

* ()

/ ()

%()

( )

+()

- ()

()

, , , ,
. ,
:
float var = 133 / 12;

,
.
. ,
4 15 , 3. C++,
15, 4, 3.
int var = 15 % 4; // var 3
, C++
:
IntValue % IntDivisor

IntValue

(IntValue / IntDivisor)* IntDivisor

:
15 % 4

15 (15/4 )
15 3*4
15 12
3

* 4


,
( 2, "
").

d/
C++ .
C++ (
), . .
. , 1 + 2
3, , ( ,
i n t ) .
, , ,
.

40

I. C++

, .
C++ ,
. , , 1;.
, 1, i n t .

z = * + w;
:
* + w
*
X

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

nc/uujjca
.
, .
int

var

= 2 * 3 + 1 ;

,
2 * 4 = 8. , + 1 = 7.
. . 3.1
, , , ..
7 { , C++ ).
,
?
i n t var = 8 / 4 / 2 ;
: 8 2 2 4?
,
( ).
8 4, 2, 2, 1.

/ 100 + 32
100 32. ,
100 32? :
/ (100 + 32)
132.
,
/ 100 4 32
:
( / 100) + 32
? C++
. ,
. ,
, .
:
, .

3.

41

tjHafutMX

.
, (
).
, ,
-.
+ , -, ++ -.
:
int varl = 10;
int var2 = -varl;
" - " .
"" ( v a r l )
. .
"" .
++ - .
1
( . increment () decrement (). . .).
.
11.
int var = 10; //
var++;
// ; II

( ), ( ).
, .
( ).
, 5.
(++ ++) 6.
, ++ ,
5.
:
//
int nl, n2, ;
nl - 5;
n2 = ++nl; // - nl 2 -
nl = 5;
= nl + + ; // nl 6, - 5
, .2
nl ,
n l .

oneftcunoftoe/
,
. " = "
.

42

I. C++

.
.
C++ ,
variable = variable # constant

# - . ,
, :
nVariable = nVariable + 2;
, nVariable
nVariable.

.
,
, ,
.
.
nVariable += 2;

: " nVariable 2".


-f-f , 1 ,
. ,

, . ,

C++ ,

,
, .

3.

43


/ ...
S
/
S C++
s

C++
* * .
(+), (-) {*).
.
.
, .
, .
.
. . ,
, .
.
. , ,
, .
.

//
: "
1
, ?' . ,
"" . ,
, (,
).
.

//
/totf
C++ .
Convert ,
, , .
C++ .
. 4.1.
: t r u e () f a l s e ().

44

I. C++

4.1.

; ,
!=
>,<
>= , <=

;
, ; , { )

, ; , > ==
( < ==)

&&

; ,

I I

; , ,

; ,

. 4.1 .
. ,
, 0, :
== 0;
== =.
, C++, ,
, !
= 0; //' , ==
"" (>) ""
(<). , t r u e .
int nl = l;
i n t n2 = 2;
n l < n2
"" "" ,
. , , -
t r u e , .
> < , nl
2, .
" " (>-), " " (<=),
, .
&& () | | ().
:
// , 2 nl
(nl < n2)&& (2 < ) ;
: " "
nl <= n2 (nl < n2) I I (nl == n2)


, ,
. : , , , ..,
:
. , , (a==b) (! (>) &&! (<)). . .

4.

45

1, 2, 3 .
4.535887 (
- , ~
11
, ).
f l o a t , C++ ,
.. , ,
,
.
:

float fl - 10.0;

float f2 - fl / 3;

fl== (f2 * 3

0) /

,
. , ,
. f2 , ,
3.3333, 3f. ,
. 3.3333 3, , , 10.0, 9.9999.
, .
.
.
, ,
, C++ .
, :
float fl = 10.0;

float f2 = 120 % 11;

f1 == f2;
// ?

f 1 f 2 (
3, " ").
. :
, .
:
float
float
float
(fl -

fl = 10.0;
f2 = fl / 3;
f3 = f2 * 3 . 0 ;
f3) < 0 . 0 0 0 1 SS

(f3 - f l )

< 0.0001;

, f 1 f2 -
( 0.0001);
.

C++
:
! && 2
1 , ,
2.
1 || 2

! ,
2, .
, ,
. . .

46

/. C++

C++ ]., ,
( &&) ( I |), 2
.


> , > 10
. , t r u e (),
f a l s e ().
, 2, "
", () .
, C++ t r u e f a l s e .
> 10?
C++ i n t . 0
false, (true).
> 10 0 (false) , , 1 (true).
Microsoft Visual Basic f a l s e t r u e
,
0 (false), -1 (true).
ANSI C++
bool, t r u e f a l s e .

' C++
,
, , .. ,
: 0 1. ,
, ,
. .
,

.

. , ,
.


, , ,
10. C++
. , , var 123.
123 1*100+2*10+3*1.
100, 10, 1 10.
123 = 1 * 100 + 2 * 10 + 3 * 1,
:
123 = 1 * 102 + 2 * 10 1 + 3 * 10
, 1.

4.

47


10
, , ,
. ,
12
{.. 20).
,
( "", ,
). :
123i, = 1 * 8'" + 7 * 1 + 3 * 8 = 173 ,

10 8 : 10 . 8
. .


, , ( ,
?). . 123
:
12 3-.U =

0*128

1*64

1*32

1*16

1-8

0*4

1*2

1*1

011110112

, ,
4, 8, 16 32 ( ..) , .
.
, ,
. , ,
.
,
.
, 123,, 01111011,.
.
0
15, (hexadecimal), ..
16. hex.
0 9 ,
10 15 :
10, 11 . . , 123, 7Bi6.
123

161

3(-..

II)*

16

VBie

4, 8, 16
32 , 1, 2, 4 8
( 0).
, , ,
7: .
, ,
.
0 (
). , 7 [6 07.
, 07 123, 0x123 291.

, . *
, , ,
.
12
, . . .

48

\. C++


,
, .
:
XIX + XXVI = XXV
:
) IX+VI : i V "" I , XV;
) +-, , , XL.
. ,
, (
, , X
, x*i v XL).
...

<5*

C++ , ..
0 I . . 4.2 ,
; " ".
4.2.

: 0 1 , 1
&
I


. ,
(, , ). (),
( ). ,
( , ). C++
. ,
4 , i n t
32 .
, . 32
.


&, | ~
. 0 f a l s e 1 t r u e ( ,
), ~ :
~ l ( t r u e ) 0 ( f a l s e )
- (true ) 1 (false )

4.

49

& :
l(true) & 1 (true) l(true)

1 (true) & 0 (false) O(false)

O(false) 5 0 (false) O(false)

0 (false) & 1 (true) 0(false)

|:
l(true)
1 (true)
0 (false)
0 (false)

I
I
I
!

1
0
0
1

(true)
(false)
(false)
(true)

l(true)
1(true)
(false)
1(true)

, " " (XOR),


. t r u e ,
- ( !) .
(. 4.3).

4.3.

1
_

, , ,
.


. ~.
~ .
~0110 2

1002 ;

(0x6)
(9)

, - 0x9.
&:
0110 2
&

0010:

, : 0 & 0 0. 1 & 0
0. 1 & 1 1, 0 & 1 0.
.
,

0110 2
0x06

&

0x03

0x02

&

0011
0010 2

, , & 0x3 0x2.


( 0x6 | 0x3. ,
.
. .)

50

I. C++

, &, |, .
.
// BitTest

//

//
~,&,I

#include <stdio.h>

#include <iostreara.h>

i n t main(int nArg, char* pszArgs[])


{
//
c o u t . s e t f ( i o s : :hex, i o s : : h e x ) ;

//
int nArgl = 0x1234;
int nArg2 = OxOOff;

//

// NOT

= " << nArgl << '\"

cout << "Argl


- " <<
nArg2 << ' \ "
cout <:< "Arg2
= " << -nArgl <<
"\
cout << "-nArgl
M
= "
-nArg2 "\

cout -nArg2
// -

cout << "nArgl & nArg2 = "

<< (nArgl s nArg2)

<<
"\n";
cout "nArgl | nArg2 "

(nArgl | nArg2)

<<
"\n";

cout << "nArgl


2 = "

(nArgl

2)
<<
retui:n );

(,
main) c o u t . s e t f ( i o s : :hex) ;
( , ).
. nArgl, nArg2,
.
:
Argl
= 0x1234
Arg2
Oxff
-nArgl
Oxffffedcb
~nArg2
= OxffffffOO
nArgl S nArg2 = 0x34
nArgl | nArg2 = 0xl2ff
nArgl nArg2 = 0xl2cb

4.

51


.
- + . ,
| |. ,
.
& . ,
,
:
0x1234
&
OxOOOF
0x0004

0001
&
0000
0000

0010

0100

0000
0000

0000
0000

1111
0100


.
,
.
1. , 1, ,
1 ,
. . 4.4.

4.4.

1 -^

1 ->

1 -

1 -

,
01.
, , C++,
( !):
(databaseValue & 1011) == 0010

*
-

1011 , ,
, .

52

I. C++


3 SfHOU ...

S
S
/

, ,

, .
,
. :
,
.
, <Ctrl+C>,
. ,
. .
, ,
.
, , ,
-
( 4, " ").
: (
), .

xoqoucnftozftauutbt
/
,
, , ,
.
+ - if:
i f (m ~ n;
-
//, ,
//
}

else

5.

53

//2-
//,
//
}

m > .
t r u e , .
, .
e l s e : , C++ , ,
.
if ,
.
, , ,
C + + .
.
, !
i f :
// BranchDemo .
// ,
// ,
#include <stdio.h>
#include <iostream.h>
int main(int argc, char* pszArgs[])
(
// ...
int argl;
cout << " argl: ";
cin >> argl;
// ...
int arg2;
cout << " arg2: ";
cin >> arg2;
// , :
if fargl > arg2)
{
cout << " 1 , 2\n";
}
else
{
cout << " I , 2\";
return 0;
}
, , .
"argl arg2" , cout
" 1 , 2\"; . ,
, e l s e : cout
<<
" 1 , 2\.";.

54

I. C++


,
.
,
.
.

.
,
.
,
.
.

while
while.
:
while
{

{)

// ,
//
}
. var > 10,
v a r l == var 2 . ,
. ,
, . , while ,
,
( ,
... ).
,
? :
// WhileDemo .
//
// while
#include: < s t d i o . h >
# include: < l o s t ream. h>
i n t main(ir.t

arg,

char*

pszArgs[])

//
int loopCount;
cout: << " loopCount:
cin
>> loopCount;

";

/ /
while (loopCount > 0)
{

loopCount

l o o p C o u n t 1;

out << " "


<< loopCount << " \";

5.

55

return 0;
}

whileDemo ,
loopCount.
w h i l e . loopCount.
, (
), loopCount 1, .
,
loopCount .
, .
, 5.
, :
4

,
.
, 4
.
w h i l e , d o . . . w h i l e .
, ,
.
do
{

//

} while ();

, do. . .while
.
while
do. . .while. -
,
.


-
. ,
WhileDemo
:
//
while (loopCount > 0)
{

loopCount = loopCount 1;
cout " "
<< loopCount << " \";
}

, :

56

I. C++

//

while (loopCount > 0)

loopCount--;

cout << " "

loopCount << " \";

.
.

, while. ,
:
//
while (loopCount-- > 0)
(
cout << " "
<< loopCount << " \";
}

, , C++
. ,
; ?

. , ,
.

loopCount--, --loopCount loopCount


; loopCount
1, .

WhileDemo,
1? ,
- - l o o p C o u n t 0 .
loopCount-- 1
.
, ,
, ,
. .
, ,

,
.

for
for.
while. for :
for (; ; >
i
//...

}
for .
,

5.

57

. ,
for.
. while, for
, .
,
for () .
, .
,
,
.
for while:
;
while ()
/ / ...

for . C++
, , C++
for ( -
).
for .
ForDemo , whileDemo, while
for.
// ForDemo. .

//

// for
#include <stdio.h>

#include <iostream.h>

int

main(int

arg,

char*

pszArgs[])

//

int loopCount;

cout " loopCount: " ;

in >> loopCount;

// ,

for (int i = loopCount; i > 0; i>


{

cout << " " << i-1 << " \";


}

return 0;
}

ForDemo , WhileDemo. loopCount


.
i
loopCount. , i
. , 1 i
.

58

I. C++

fo r ,
, .
for .
// ForDeno. .
/ /
// for
#include <stdio.h>
#include <iostream.h>
int main!int arg, char* pszArgs[])
{
//
int loopCount;
cout << " loopCount: ";
cin loopCount;
// ,
for (int i = 1; i <= loopCount;
cout << " " << i " \";
}

return 0;
,
for ,
. C + + ,
for . ,
r e t u r n , .. ,
i .
,
, ,
.
: for while,
? for ,
for (, , )
.


, .
, ,
- .
:
while (loopCount > 0)
f
cout << " " <<loopCount << " \"
}

loopCount,
. ,
. ,
.
, .., , .

5.

59

... ! ,
, Microsoft ... ,
.
,
; ,
.


C++ break
continue. ,
, - . ,
. ,
.
, ,
, . .
C++ break.
. break
,
.
break :
while()

if (- )
{

break; //

// break,

}
//
break, BreakDemo

// BreakDemo .

//
,

//

//

#in1ude <sraio.h>

#include <iostream.h>

in: main(ir.t arg, char* pszArgs[])


{

//

int accumulator = 0;
out << " , "

<< " \";

cour. < < " "

<< " "

<< " \";

//

for(;;)

//

int value = 0;
cout << " : ";

cin >> value;

60

I C++

// ...

if (value < 0)

// ...

break;

// ...

accumulator = accumulator + value;

//

//

cout << "\ "

accumulator

"\";
return 0;
( , ,
)
for. .
,
. ,
break .
, break
.
.

,
. accumulator
, .
c o n t i n u e . ,
.
. ,
, :
while (1)
//

cout << " ";

can
inputVal;

//

if (inputVal < 0)

//

cout " "


" \";

//

continue;

//

5.

61


. ,

(
?).
? ( , , '.')
, , ,
.
,
. BreakDemo
,
. , NestedDemo,
, , .

, .
// NestedDemo .
//
,
//
.
//
,
//
0.
#include <stdio.h>
#include <iostream.h>
int main(ir.t arg, char* pszArgs[])
{
out << " "
<< " .\ "
<< " "
<< " \"
<< " , "
<< " \ \";
//
int accumulator;
do
{
//
accumulator = 0;
cout << "\ \";
//
(;;)
{
//
int value = 0;
cout << " : ";
cin >> value;
// ...
if (value < 0}
{
// ...
break;
}
// ...

62

I, C++

//

accumulator = accumulator + value;

// ...

cout "\ "

<< accumulator

<< "\";
I / ...

// ,

//

//

} while (accumulator != 0 ) ;

cout << " \n";

return 0;

//
,
.
if,
:
switch ()
{
case c l :
// , ==
cl
break;

case c2:

// , == 2
break;
default:
// ,
//
1
{int, long char); c l , 2,
. switch :
, ,
case.
, .
, d e f a u l t .
:
cout << " 1, 2 3 : " ;
cin >> choice;
switch (choice)
{
case 1:
// " 1 "
break;
case 2:
// "2"
break;

5.

63

case

3:
//' " 3 "
break;
default:
out << " 1, 2 3\"

, switch
if ( if-); , -
, switch .
switcn
break, .

64

I. C++

. w &:

Od?uw,uuHtw! O(buu,uuHtw! Mf-

!' . MHt

& &...

( ) ,
.
,
.


/ ...
S
^
S
S
S


\S , .
, -
.
( !) .
, .
C++ , .
.

" " "". , sumSe
quence{) ,
.
.

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

/{
.
FunctionDamo, , 5
NestDeno, . Func
tionDemo , .
.
NestDeno .
. ,
, .
.
FunctionDemo , NestDemo
sumSequence {] .

6.

67

C++
. .

// FunctionDemo

//
.

//

//

#include <stdio.h>

#include <iostream.h>

// sumSequence

//
, ,

//
//
.

int sumSequence(void)

//

int accumulator =
0;
for (;;)
{

//

int value = 0;

cout << " :


";

cin >> value;

// ...

if (value < 0}
{

// ...

break;

// ...

// accumulator

accumulator = accumulator 4 value

//

return accumulator;

int main(int arg, char* pszArgs[J)

cout << " \"

<< ". \"

<< " .\"

<< " \"

<< " \";

// ...

int accumulatedVaiue;

do

I
// ,

//

cout << "\ \";

68

If.

accumulatedValue = sumSequence(};
//
<< " "
<< accumulatedValue
"\";
/ / . . .
} w h i l e ( a c c u m u l a t e d V a l u e != 0 ) ;
c o u t << " \ " ;
r e t u r n 0;

sumSequence ()
,
m a i n ( ) .
NestDemc.

, main(>
accumulatedValue sumSequence () .

sumSequence ().

accumulatedValue, .
, ,
, 0. ,
.
,
. ,
.

sumSequence ()
i n t sumSequence (void).
. ,
sumSequence () NestDemo.
: main
, (, NestDemo )
sumSequence (). ,
main , .


C++.
, .
C++,
:
< > name(< )
{
retui<>;
}

,
. ,
. , square (10)
10 , 100.

6.

69

, . -
, void. ,
void,
( FunctionDemo).
void, .
FunctionDemo sumSequence ( ) ,
.
void,
i n t fn (void) i n t fn ().

FunctionDemo . sumSequence ()
,
.
main ()
sumSequence () (
, , ).


sumSequence () .
, double char (
5, " ").
, void.
. ,
, . ,
, void-.
, ,
.
void echoSquare(>

cout << " :";

cin >> value;

cout << "\n"value*value "\n";

return;

,
return (
). .
r e t u r n void- .
,
.


,
, .. .

.

70

II.


,
. s q u a r e {>,
d o u b l e :
// SquareDemo
// ,
# i n c l u d e <stctio . h>
#include <lostrearn.h>

// square

//
doubleVar

//
returns doubleVar

double square(double doubleVar)

return doubleVar * doubleVar;


// sumSequence

// ,

// ,

// .

//

int sumSequence(void)

//

int accumulator^ 0;
for(; ; )

//

double dValue = 0;

out << " : ";

cin >> dValue;

// ...

if (dValue < 0)

// .
. . :
break;

// ...

int value = (int]square(dValue);

//

// accumulator

accumulator^ accumulator + value;

//

return accumulator;

int main (int arg, char* pszArgs

6.

71

cout " \n"


<< " . \."
<< " ^"
<< " \"
<< " .\"
" \."
<< " , \"
<< " \.";
// ...
int accumulatedValue;
do
{
// ,
//
cout << " \ \." ;
accumulatedValue sumScquence();
//
cout << " "
<< accumulatedValue
"\";
// ...
} while (accumulatedValue != 0 ) ;
cout << " \n";
return 0;
}
, FunctionDemo,
. squar e ()
.
sumSequence () : ,
, squar e () .


.
. , :
int product(int argl, int arg2)
{
return argl*arg2;


, :
: accumulator = accumulator + (int)dValue;
( i n t ) dValue ,
dValue (
double) i n t .
.
C++
. C++
: ,
.
.

72

//.

main ()
ir.ain() ,
(, ).
C++
, , main ().
, ,
.
main() ,
.
main (), C++
.

JCefiezjUfetca/
C++
(function overloading).
, ,
C++ .

( ),
:
void someFunction(void)
{
//...
}
void someFunction(int n)
(
//...
}
void someFunction(double d)
i
//... :
}
void someFunction(int nl, int n2)
{
//... ,
//
)
C++ , someFunction (void) , someFunction ( i n t ) ,
someFunction(double) , s o m e F u n c t i o n ( i n t , i n t ) .
,
. .
, void . ,
SumFunction (void) SumFunction () .
, someFunction () ,
, .
, .
, - ,
(, ,
). , ,
. , ,
.

6.

73

,
.
:
int intVariablel, intVariable2;

double doubleVariable;

//
someFunction();
// someFunction(void)

soraeFunction(intVariablel);
// someFunction{int)

someFunction{doubleVariable); // someFunction(double)

someFunction(intVariablel, intVariable2);

// someFunction(int, int)

//

someFunction(1);
// someFunctionfint)

someFunction(1.0);
// someFunction(double)

someFunction(1,2 ) ;
// someFunction(int, int)

,
.
(
) .
()
:
int someFunction(int n ) ;

// someFunction(int)

double someFunction(int n ) ; //

:
int someFunction(int n);

double d = someFunction(10) ,

//

i n t
double. :
int someFunction(int n ) ;

double someFunction(int n);

double d = someFunction(10);

//

// ?

C++ ,
double- .

Hfwtfiotnunotf
,
, ,
.
sumSequence () square , ,
, . ,
.
( C++).

74

//.

- ,
main () , . :
int main(int argc, char* pArgs[])

someFunc(1, 2) ;

i n t someFunc(double argl, i n t arg2)


(
//... -
}

someFunc () main .
, someFunc ( i n t , i n t )
void. , , .
, C++
. , "
13. : .
main()
. .

. :
int someFunc(double, int);

int main(int argc, char* pArgs[])

someFunc(1, 2);
|

int someFunc(double argl, int arg2)


{

//... -

( ,
), someFunc ()
some?unc (double, i n t ) . mainf) ,
1 double. , main () ,
someFjnc () .
, , .
, ,
.

/
. ,
, . l o c a l Variable f n {):
int globalVariable;

void fn{}
1
, ,
. .

75

int localVariable;
static int staticVariable;
}
f n () l o c a l V a r i a b l e .

. , fn () ,
.
g l o b a l V a r i a b l e
.
s t a t i c V a r i a b l e -
. ,
( ,
). s t a t i c V a r i a b l e fn () . ,
l o c a l V a r i a b l e , s t a t i c V a r i a b l e
. fn () s t a t i c V a r i a b l e
- , f n ().

76

II.

/3 * ...
^
/
S
/
/

J[JM , ~
4 / 1 / 1 * ;
.
. , ,
, . C++
. ,
,
.
,
, .
, C++ char.

71
. ,
, .
,
. ,
, .
, :
cin >>
if (valuc.l >= )
{
in >> value2;
if ;-/alue2 >= 0)

,
, , ,
. ,
, , ,
. .

7.

77


. valueArray,
128 . ,
.
int value;
// ,

// 12 8 int

int valueArray[128];

// ,

// ;

// 128
for (int i = 0; i < 128;

cin >> value;

// ,

//

if (value < 0) break;

valueArray[i] = value;

( ) valueArray.
( i n t ) ,
,
, .
valueArray 128 .

valueArray.
, .
valueArray [0], valueArray [1] ..
valueArray [i] i- .
i , .. char, i n t long.
valueArray , vaiueArray [ij i n t .



( ),
.
// ArrayDemo

// .

//

#include <stdio.h>

#include <lostream.h>

//

int sumArray(int integerArray[],


inr sizeOfloatArray);
void displayArray(int integerArray[], int sizeOfloatArray);

int main(int nArg, char* pszArgs[]}

//

int nAccumulator = 0;

cout << " ,"

78

II.

<<
cout <<
<<
<<

" \";
" , "
" "
" \";

//
int inputValues[128];
int umberOfValues = 0;
for f; numberOfValues < 12 8; numberOfValues++
{
//
int integerValue;
cout << " : ";
cin >> integerValue;
// ...
if {integerValue < 0)
\
II ...
break;
// ...
inputValues[numberOfValues] = integerValue
//
displayArray(inputValues, numberOfValues);
cout << " "
<< sumArray(inputValues, numberOfValues)
"\";
return 0;
// displayArray
// integerArray sizeOfloatArray
void displayArray(int integerArray[], int sizeOfArray)
{
cout << " "
<< " :\";
for (int i = 0; i < sizeOfArray; i++)
{
cout.width(3);
cout i ": " integerArray[i] << "\n";
}
cout << "\n";
// sumArray
//
int sumArray(int integerArray[], int sizeOfArray)
{
int accumulator = 0;
for (int i = 0; i < sizeOfArray; i++)
{
accumulator += integerArray[i];
}
return accumulator;

7.

79

ArrayDemo sumArrayf)
displayArra y (), .
.
i n p u t v a l u e s .
for.
integerValue . ,
break . , integerValu e
.
nuraberOfValues
. for.
. for
, 128, ..
( 128
, ).
i n p u t v a l u e s ,
128. ,
,
.
. , ;
, .
main
. displayArra y () for,
.
accumulator . sizeOfArray
, .
, C++ 0, 1.
, , for ,
i sizeOfArray. 128
integerArra y accumulator? ,
sizeOfArray, .


,
. , - , .
:
- , .
,
.
.
, , :
float floatArray[5] = {0.0, 1.0, 2.0, 3.0, 4.0);

f l o a t A r r a y [0]

0,

f l o a t A r r a y [1] 1, f l o a t A r r a y [2 ] 2 ..

.
, ,
floatArra y . C++ (
, ). ,
:
f l o a t

80

f l o a t A r r a y [ ]

{0.0,

1.0,

2.,

3.0,

4 , 0 } ;

II.


, 1.
(1).
1. C++
0! C++
valueAriray [01. C++ ;
128- i n t e g e r A r r a y [127], i n
tegerArray [128].
, C++ .
i n t e g e r A r r a y [200]. , C++
i n t e g e r A r r a y [-15].
.
.
. ,
, .
,
10- .
, .
, ,
. ..,
, .
, 20 .
(, ?). ,
. , -
- ( !). , ,
, - .
, (-
, , ,
).
. 20-
(arra y L;*0]) 10- ,
( , ).
! 2 0 ] - , .
, ,
.

i n t e g e r A r r a y 1128].
,
, .


, ArrayDemo ,
, . ,
,
.

.
.
displayArray () , Su
mArray ;) .

7.

81



.
.
,
( ).
C++ :
int intMatrix[10][5];

10 5 ,
50 . , i n t M a t r i x 10
, 5 .
, i n t M a t r i x [0] [ 0 ] ,
i n t M a t r i x [ 9 ] [ 4 ] .
i n t M a t r i x .
10 , .
, :
i n t

i n t M a t r i x [ 2 ] [ 3 ]

{{1,

2,

3 ) ,

(4,

5,

6}};

:
i n t M a t r i x [0] I, 2 3, a i n t M a t r i x [1] 4, 5 6 .

. C++
f l o a t , double, long,
.
.
, , :
c h a r

sMyNarae[]

{ ' S

' t ' ,

' e ' ,

' p ' ,

' h ' ,

' e ' ,

' n ' } ;

:
// CharDisplay

// MS DOS

#include <stdio.h>

#include <iostream.h>

//

void displayCharArray{char stringArray[],

int sizeOfloatArray);

int main(int nArg, char* pszArgs[])

char charMyName[] = { ' S ' , '


t', 'e', 'p', 'h', 'e 1 , 'n'};
displayCharArray(charMyName, 7 ) ;

cout << "\n";

return 0;

//displayCharArray ,

//
void displayCharArray(char stringArray[],

int sizeOfloatArray)

82

II.

for i int i = 0 ; i< sizeOfloat.Array;


{
cout << stringArray[i];

, ,
, ( ?).

displayCharArra y ()

displayCharArray () displayArra y ()
, .
; - :
.
, . ,
,
.
.
0, , .
0 0x30. - ,
0 (.. 0x00). 0,
\0.
\ , .
, :

// DispiayString
// MS DOS
#include <stdio.h>
#include <iostream.h>
//
void dLspIayString(char stringArray[]};
int maii(int nArg, char* pszArgs[j)
{
char charMyName[] =
{'S', 't', 'e', 'p', 'h', 'e', 'n',
displayString(charMyName);
cout << "\n";
retarn 0;
// displayString
void displayString(char stringArray[])
{
for (int i = 0; stringArray [ i ] != 0; i+-O
{
cout << stringArray[i];

charMyName
(\0) . d i s p l a y S t r i n g
, -.
d i s p l a y S t r i n g ()
- , , displayCharArray ().

7.

83

d i s p l a y S t r i n g () ,
. ,
( 6, " ").
, C++
. .
.

C++ .
, ,
. 8 9 :
char szMyName[] = "Stephen";


sz. .
"Stephen " ,
!

{/
. C++
,
. , ,
- .

,
.

. :
// Concatenate ,
// " "
#include <stdio.h>
#include <lostream.h>
// ,
//
#include <string.h>
//
void concatString(char szTarget[], char szSource[]);
int rnain(int nArg,
i

char* pszArgs[])

// ...
char szStringl[256];
cout << " #1: ";
cin.getline(szStringl, 128 ) ;
// ... ...
char szString2[128];

84

II.

cout << " #2: ";


cin.getline Is2String2, 128);
// ... " "
concatString(SzStringl, " " ) ;
// =treat(szStringl, " " ) ;
// ... ...
concatString(szStringl, szString2);
// =treat(szStringl, szString2);
// ...
cout << "\n" << szStringl << "\n";
return 0;
// concatString ~ szSource
//
szTarget
void concatString(char szTargetf], char szSource
(
//
int targetlndex = 0;
whiJ e (szTarget[targetlndex] )
(
targetlndex++;
//
int. sourcelndex = 0 ;
whil.e (szSource [sourcelndex] )
{
szTarget[targetlndex] =
szSource[sourcelndex];
targetlndex++;
sourcelndex+j-;
// -
szTarget[targetlndex] = ' \ 0 ' ;
}

main , g e t l i n e { ) . (
cir. >> s z S t r i n g
, .)
main () , c o n c a t S t r i n g {)
. c o n c a t S t r i n g ()
(szSource) (szTarget) . .
szTarget . concatString ; s z T a r g e t , t a r g e t l n d e x
.
t a r g e t l n d e x ssTarget .
while (value !=0) : while ( v a l u e ) ,
f a l s e , value 0,
t r u e .

7.

85

szSource,
, , szTarget. , sour
celndex szSource. c o n c a t s t r i n g ()
,
.
j_[e ,
.
""
, ,
.
,
. C++
:
char dash [ ] = " ",
c o n c a t S t r i n g ( d a s h , szMyName);
, dash
. c o n c a t s t r i n g (>
dash.

C++
C++
. , .
,
, . ,
. 7.1,
.
7.7.,

int strlen(string]

(
)

char*" strcat (target, source)

s o u r c e t a r g e t

char* strcpy(target, source)

s o u r c e t a r g e t

char* strstr(sourcel,

source2)

source!

int strcmp(sourcel, source2)

source2

int stricmp(sourcel, source2)



,
# i n c l u d e < s t r i n g . h > .
Concatenate c o n c a t S t r i n g ()
C++ s t r c a t ().
.

. , !
c o n c a t s t r i n g () ,
, C++.

86

II.

wchar_ t
+-1 char 8- ,
256 ( 0 255). 10 , 26
26 .
.
,
, . ,
, , .. , 8-
.
C++ wchar_t,
char. , char,
C++ . , w s t r s t r ( )
wchar_t.
, ,
. ,
, .

C++ .
p r i n t f ,
.
:
p r i n t f ( " ");
p r i n t f
, %. ,
:
i n t n l n t = 1;
double doubleVar = 3.5;
printf(" %i; "
" %f",
nlnt, doubleVar);
%i, %f,
:
1; 3.5

.

7.

87

C++

3 01 ...
/
s
/
^

*77
/ / C++ ,
* ^ * , ,
,
. ,
(.. ).
.
,
,
C++.

tncucoe aqftec
, C++ - .
, 0, 1, 2 ..
intRandy 0x100, a floatReader
0x180 ( ).
, ,
(
, ). ,
, . 8.1 (
Pentium GNU C++ Visual C++).

8.1.

( )

ir.t

long

float

double

,
(
& , ).
// Layout

//

88

//.

//

#include <sttiic.h>

#include <iostream.h>

int main(int intArgc, char* pszArgs[])

int

ml;
int
n;

long
1;

float f;

double d;

int
ir.2;
//

cout.setf(ios: :hex) ;
//

// ,
//

cout << "


= Ox" << (long)&ml
<< "\n
cout << "&n = Ox"
<< (long) &n < "\n
cout << "&1 = Ox" << (long)Si <<= "\n

cout << "sf - Ox"


<< (long) &f <<< "\n
cout << "&d = Ox"
<< (long) &d <i "\n
cout << "
= Ox"
<< (long)&m2 << "\n
return 0

He ,
.
. .
, ,
, 1 .. ( ).
GNU C++, Visual C++
.

/(
- , .
. 8.2 .
8.2.

& ()
* ()


,
( ).
( )

8. C++

89

void

fn()
in" irit.Var ;
iriL* pintVar;

pi ntVar - 6intVar; // pir.tVar intVar


*ntVar = 10;
// 10 int
// , pintVar

f n () intVar;
pir.tVar H;I i n t .
,
*,
.
i n t .
.
,

. , _ i n t , d double ..
.
& " ".
,
in-Var pintVar.
, fn <} 0x100,
i n t v a r 0x102, pintVa r 0x106 (
Layout;
).
s i n t v a r (0x102)
pintVar. 10 ,
, pintVa r ( 0x102,
.. intVar).


. ,
.
. , : 123 Main Street ( ,
! ,
). (, 123456).
123 Main Street,
0x123456.
123 Main Street. ,
, . :
, ,
, (
).
C++ , :
House myKouse;
House" houseAddress;
houseAddress = &nr.yHouse;
'houseAddress = couch;
: myHouse , a houseAddress
. myHouse houseAddress
, houseAddress.

90

i n t :
int myInt;
int* intAddress;
intAddress = sraylnt;
*intAddress = 10;

, : my i n t
i n t . raylnt intAddress 10
, , intAddress.


, , .
sintVar i n t , .. i n t * .
pintVar , :
i n t * pintVar = SintVar;
//
// * i n t
pintVar int*, a *pintVar int:

*pintVar = 1 0
//

// int

, pintVar, i n t . ,
bouseAddress , , , houseAddress
.
:
double dDubleVar

double* odoubleVar = sdoubleVar

*pdoubleVar =10.0

Pentium ,
, 14.
. ,
, :
int nl;
int* pintVar;

pintVar = &nl;

*pintVar = 100.0;

, ,
, , .
,
100.0 i n t , .
:
int iVar;

double dVar = 10.0;

iVar = (int)dVar;

:
int* piVar;
double dVar = 10.0;

double*
pdVar;
piVar (int*)pdVar;

14

, ,
,
. . .

8. C++

91

, ,
, . ,
, , ,
.
Layout Err :
// LayoutError

//

#include <stdio.h>

#include <iostream.h>

int main(int intArgc, char* pszArgs[])

rnt
rnt
int

upper = 0;

n
= 0;

lower = 0;

//

cout << "upper =


" <<: upper << " \n" ;

cout << "n


=
" n
"\n";

cout << "lower


= " << lower << "\n";
// double

// , int

cout << "\ double int\n";

double* pD = (double*
) .n;
*pD = 13.0;
//

cout << "upper = "


upper << "\n";
cout << "n
= " ' n
<< "\n";

cout << "lower = " << lower "\n";

main()
i n t . , .
.
, .
* = 13.0;, , double,
., i n t . .

upper "" - .
:
house* houseAddress = &"123 Main Street";

hotel * hotelAddress;

hotelAddress = (hotel*)houseAddress;

*hotelAddress = TheRitz;

houseAddress .
hotelAddress .
. "" . ,
"" , ,
, ( - !).
,
.

92

II.

* p i n t v a r = 100 . , C + + ,
p i n t V a r 1 0 0 . 0
.

Jtefteqana
.
, ,
.


, ,
. :
void fn(intArg)
{
int intArg = 10;
// intArg 10
}
void parent(void)
{
int nl = 0;
fn(nl);
// nl 0
}
p a r e n t () nl .
n l fn (). f n () intArg
10. fn()
. n l ,
, p a r e n t () 10.
nl 0.
, C++ , ,
.
,
.
, , - "

f n

O " ,
.


, ,
.
void

fn(int*

pintArg}

*pintPrg = 10;

void parent(void)
{

int r. 0;

8. C++

93

fn(Sn);
i

//
// 10

f n () .

, , , fn ().
, 0x102. f ()
, 0x102. fn () *pintArg = 10
10 i n t ,
0x102. , 10,
0x102 .


C++ ,
.
.
(, ,
).
void fnfints. intArg)
{
intArg = 10;
i
void parent(void)
i
int n - 0;
fn(ri)
// 10
}
frs () , ,
, 10 i n t , intArg.

/
(heap) ,
. ,
.
, ,
. , ,
:
double* f n ( v o i d ) ;
.
, ,
(.. ,
...).


, C++
, .. , .
:

94

//.

//

//

//

// ( )

int intGlobal;

// intChild child

//

// child()

// ( )

void child(void)

int intChild;

// intParent
void paremt (void)
{
i n t i n t P a r e n t = 0;

c h i l d () ;

i n t i n t L a t e r =
0 ;
i n t P a r e n t = i n t L a t e r ;

int main(int nArgs, char* pArgs[])

parent();

main (). main ()


p a r e n t ().
i n t P a r e n t , , .
.
p a r e n t c h i l d ().
i n t C h i l d ,
c h i l d O . i n t P a r e n t c h i l d () (
i n t P a r e n t c h i l d ),
.
c h i l d () i n t C h i l d
, .. ,
(, ,
).
c h i l d ()
p a r e n t (), i n t L a t e r ,
, p a r e n t ().
main () i n t L a t e r i n t P a r e n t
.
,
.
( ).
i n t G l o b a l ,
.

8. C++

95


,
.
double1 child(void)

double dLocalVariable;

return fidLocalVariable;

void parent(void)

<
double* pdLocal;

pdLocal = child ();

*pdLocal = 1.0;

, dLocalVariable
c h i l d ( ) . , dLocalVariable c h i l d ()
,
- .
5
* '
,
. ,

. ,
, d L o c a l V a r i a b l e ,
. ,
.


, C++
.
, .
,
"" C++. (heap).
, new;
, .
double.
double* child{void)

double* pdLocalVariable = new double;

return pdLocalVariable;

, pdLocalVariable
c h i l d ( ) , , ,
.
.
d e l e t e .
void p a r e n t ( v o i d )
{

// child()

//

double* pdMyDouble = child();

96

II.

//

*pdMyDouble = 1.1;

//

delete pdMyDouble;

pdMyCouble = 0;

, c h i l d O ,
double , .
, ,
pdMyDouble . ,
. ,
- , pdMyDouble,
.
, ,
, .

8. C++

97


/ ...
/
/

C++ ,
. ( 8, "
C++".)
; .


, 3, "
", .
(
7, " "). . 9.1
.
9.1.

pointer+offset

o f f s e t p o i n t e r
pointer-offset
pointer2-pointerl

,

pointer2

pointeri

offset i n t ( ,
, ++ +=, ).
, (
), ,
. ,
. , 123 Main Street, 124 Main Street
( 122 Main Street, ,
).
,
127 Main Street.
123 Main Street + 4 = 127 Main Street

, , 123
127, :
127 Main Street - 123 Main Street = 4

, :
123 Main Street - 123 Main Street =

98

II.


.
.
.
. .
0 , 1
..
32- ,
charArray. 0x110,
0xl2f. , cha
rArray[0; 0x110, c h a r A r r a y [ l ] 0x111, charAr
[2 ] 0x112 ..
p t r .

p t r = ScharArray[0];
p t r 0x110.
.
. 9.2.
,
charArray.

9.2.

0x110

+1

0x111

charArray[0]
charArray[1]

+2

0x112

charArray[2]

+n

0110+

charArray[n]

,
.
, char* p t r scharArray [0] ;, * ( p t r + )
charArray [n] .
* , ,
* p t r + ,
p t r .
, .
* ( p t r + n) , p t r
.

, C++ a r r a y f n ] * ( p t r + n), p t r
a r r a y . C++ a r r a y []
* ( s a r r a y [0] +). , char charArray [20], charArray
s c h a r A r r a y [ 0 ] .
, ,
( ). ,
, a r r a y [n] C++
* ( a r r a y + n) .

9.

99



( , ).
, d i s p l a y A r r a y (),
, :
// displayArray ,

// nSize

void displayArray(int intArray[], int nSize)

cout << " :\";

for (int n = 0; n < nSize; n++)


{

cout n << ": " << intArray[n]


M \n";
}

cout << "\n";

,
.
, :
// displayArray ,

// nSize

void displayArray(int intArray[], int nSize)

cout << " :\n";

int* pArray = intArray;

for(int n 0; n < nSize; n++, pArray++)

cout << n ": " << 'pArray "\n";

}
cout << "\n";

}
displayArray
intArray.
, "
15.
.
for
intArray. pArray,
.
,
, ,
, .
.
.

,
. . .

100


,
( C++ ).
(,
), 7, " ".
C++ .
,
.



,
. ,
, .
7, " ",
c o n c a t S t r i n g {), .
:
void c o n c a t S t r i n g ( c h a r s z T a r g e t [ ] , char s z S o u r c e [ ] ) ;
,
. ,
, .
szTarget c o n c a t S t r i n g ()
while:
void c o n c a t S t r i n g ( c h a r s z T a r g e t [ ] , char szSource[])
{

//
i n t intTargetlndex = 0;
while(szTarget[intTargetlndexj)
{
intTargetIndex++;

,
:
void c o n c a t S t r i n g ( c h a r * pszTarget,

char* p s z S o u r c e ) ;

:
void concatString(char* pszTarget, char* pszSource)

//

while(*pszTarget)

pszTarget++;

c o n c a t S t r i n g while ,
szTarget [ i n t T a r g e t l n d e x ] 0.

pszTarget.

9.

101

ptr+ + p t r
p t r + 1.

while pszTarge t
, . , pszTarge t ,
, .

c o n c a t s t r i n g ( )
:
// ConcatenatePtr
//
" ",
//

//

#include <stdio.h>
#include <lostream.h>
void concatString(char* pszTarget, char* pszSource);
int main(int nArg, char* pszArgs[])
{

// ...
char szStringl [256];
cout " #1: ";
cin.getline (szStringl, 128);
// ... ...
char szString2 [128];
cout << " #2: ";
cin.getline(szString2, 128 ) ;
// ... " " ...
concatString(szStringl, " " ) ;
// ... ...
concatString(szStringl, szString2);
// ...
cout << "\" szStringl "\";
return 0;

// concatString *pszSource
//
*pszTarget
void concatString(char* pszTarget, char* pszSource)
{
//
while(*pszTarget)
{
pszTarget++;
}
//
// (
// , ,

102

// )

while(*pszTarget++ = *pszSource++)

main f)
, c o n c a t s t r i n g ()
.
, c o n c a t s t r i n g
char*. , while
c o n c a t s t r i n g {) pszTarget.
c o n c a t s t r i n g ()
pszSource pszTarget.

while (), .
1. , pszSource.
2. pszSource
.
3. ,
pszTarget.
4. pszTarget
.
5. , .

while (). , pszTarget
.



: ? ,
char*
?
c o n c a t e n a t e ()
C++, .
,
C++. , C++, ,
, . ,
,

while (*pszTarget++ = *pszSource++) {} ,



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

9.

103

, , C++,
. C++
.
.
//
*pszArray++ = '\0';
/ /
*pszArray2 '\0';
pszArray2 = pszArray2 + 1;
//
//
, ,
.

.


, szTarge t + n s z T a r g e t [ ] ,
szTarge t . szTarge t
0x100, 0x105.
,
i n t , .
,
0x114 {0x100 + (5*4) = 0x114).
, , a r r a y + n
a r r a y [n] , , C++
.
. 123 Main Street
126 Main Street, Main Streel .


. -,
,
.
void arrayPointer()
{
// 128
char charArray[128];
// ,
// ,
char* pArray;
}
charArray 128 , ,
, .
.
void arrayVsPointer{)
{
//
char charArray[128];

104

charArrayflO] - '';
* (charArray + 10) = '';
// ,
chai* pArray;
pArrayflO] = "";
* {pArray + 10} = '0' ;
}
charArray[10] *(charArra y + )
. pArray
. C++ , pArray
, ,
. , [10] * (pArray + 10)
.

(segment violation).

( , ) .
, charArray , pArray . for,
, .
void arrayVsPointer()
{
char charArray[10] ;
for ;int i = 0; i < 10; i++)
{
"charArray = '\0' ;
// ...
charArray++;
// ...
charArray++ , 10++.
:
void arrayVsPointer{)
{
char charArray[10];
char* pArray = charArray;
for (int i 0; i < 10; i++)
{
""pArray = ' \0 ' ;
//
pArray++;
//



, ,
. .
,
. :
int*

pints[10];

9.

105

, p i n t s [0] i n t .
, :
void f ()
(
int nl;
i n t * p i n t s [3];
pints[0] = snl;
* p I n t s [ 0 ] = 1;
}

:
void fn ()
{

int nl,n2,n3;
i n t * p l n t s [ 3 ] { s n l , &n2,
f o r f i n t i 0 ; i < 3;
{
* p l n t s [ i ] = 0;

&n3,};

:
void

fn()

int nl,n2,n3;
int* pints[3] =

for

{(new i n t ) ,
(new i n t ) ,
(new i n t ) } ,
( i n t i = 0 ; i < 3;
p l n t s [i]

= 0;

,
.. .
.
, .


C++ , ,
. C++ ,
(
...).
. ( , .)
, ,
. , 1,
"". ,
1 12.
:
// int2month()
char* int2month{int nMonth]
{
char* pszReturnValue;
switch(nMonth)

106

II.

case 1: pszReturnValue = "";

break;

case 2: pszReturnValue = "";


break;

case 3: pszReturnValue = "";

break;

// ...

default: pszReturnValue = " "

return pszReturnValue;

switch {> , if.

,
, .
:
// int2ironth ()
char* i n t 2 m o n t h ( i n t nMonth)
{
//
if (r.Month < 1 I I nMonth > 12)
{

ieturn

"invalid";

}
// r.Month
//
char"* pszMonths [ ] = ( "" ,
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""};
return

pszMonths[nMonth];

nMonth, ..
1 12 (
, , d e f a u l t ) . nMonth ,
, .

main ()
main () .
, . ,
MS DOS:
MyProgram file.txt /w

9.

107

MS DOS , MyProgram.exe,
f i l e . t x t /w. , (/)
(-), , :
, . ,
<, >, >> | ! ( ),
.
main ().
pszArgs, main ,
, a nArgs .
.
// PrintArgs

//

,
#include <stdio.h>

#include <lostream.h>

int main(int nArg, char* pszArgs[])

//

cout << " " << pszArgs [0] " \" ;

//

for (int i = 1; i < nArg; i++)

cout << i << ": " pszArgs[ij "\n";

//

cout << " \";

return 0;

, main() .
i n t , nArgs.
. char*;
pszArgs.
.
PrintArgs
PrintArgs a r g l arg2 arg3 /w
MS DOS, nArgs 5 ( ).
. , pszArgs [0]
,
( "argl", "arg2'\ ...). MS DOS
/, "/w".

108

10


...
/
S
S

FunctionDemo

/ #includ e
J C++

tw r ,
* . -.
. ,
,
.

[ nftotftcuunf

,
.
, .

(linking).
,
. -,
. GNU C++ Visual C++ ""
.
.
, .
,
, . ,
(
).
-, , ,
,
.
,
. ,
.
(, , ) ,
.

10.

109

flfuuiefi nftozftoMMM
, , ...
, , (,
,
?). , FunctionDemo 6, " ",
.
, , , FunctionDemo. cpp :
// FunctionDemo
//

//
NestedDemo
//

#include <stdio.h>
#include <iostream.h>
// suraSequence
//
,
//
.
//

int sumSequence(void)
{
//
int nAccumulator = 0 ;
for (;;)
{
//
int nValue = 0;
cout << " : ";
cin
nValue;
// ...
if (nValue < 0)
{
// ...
break;
// ...
//
nAccumulator = nAccumulator + nValue;
//
return nAccumulator;
int main(int nArg, char* pszArgs[])
{
cout << " \"
<< ". ,\"
<< " .\"
<< " ,\"
<< " ";

110

II.

// ...

int nAccumulatedValue;
do

// ,

//

cout << " ^";

nAccumulatedValue = sumSequence();

//

cout << "\ "

<<nAccumulatedValue

"\";
// . . .
} w h i l e (nAccumulatedValue != 0 ) ;
c o u t << " \ " ;
r e t u r n 0;
, FunctionDemo
. ,
, main () sumSequence ().

nftozfuuutM

FunctionDemo
FunctionDemo. cpp ,
. main
,
. sumSequence ()
.
, ,
, ,
.
FunctionDemo
: sumSequence (), main ().
, . sumSequence ()
, FunctionDemo
, .
.

sumSequence ()
sumSequence .

SeparateModule. cpp

sumSequence():

// SeparateModule ,

//
,

//
;

//
,

10.

111

//

main()

#include <stdio.h>
#include <iostream.h>
// sumSequence
//'
,
//
.
//

int sumSequence(ostreams out, istreams in)
I
//
int nAccumulator = 0;
for(;;)
{
//
int nValue = 0;
out << " : ";
in >> nValue;
// ...
if (nValue < 0)
{
// ...
break;
// ...
//
nAccumulator = nAccumulator + nValue;
//
returnnAccumulator;
}
SeparateModuIe cpp , ,
main() .
, ()
.
,
.
sumSequence () ,
FunctionDemo 6, " ", .
cin ,
cout.
.
c i n i s t r e a m , ,
.
, sumSequence () ,
. 26, " -",
.
, -
, .
,
.

112

II.

MainModule.
sumSequence () , MainMod
ule. main():
// MainModuie- ,
//
,
//
;
//
main()
#include <stdio.h>
#include <iostream.h>
int surn3-aquer.ee (ostreams. out, istreams in);
int mainfir.t nArg, char* pszArgs[])
{
out << " \"
<< ". ,\"
<< " .\"
<< " ,\"
<< " \";
// ...
int nAccumulatedValue;
do
{
// ,
//
cout << "\ \";
nAccuirLulatedValue = sumSequence (cout, cin) ;
//
cout " "
<< nAccunulatedValue
<< "\";
// ...
} while (nAccumulatedValue != 0);
cou~ << " \";
return 0;
}
sumSequence () ,
:
i n t sumSequence(ostreams out , i s t r e a m s i n ) ;
6, " ".
,
, ,
.


SeparateModule.cpp MainModule.
r h i d e . Compile^Make
<F9>. r h i d e
a o u t . e x e ( , ).

10.

113

GNU C++
,
, : . ,
,
. , ,
, .
.
1. Project^Open Project.
2. , Separate (
).
<empty>.
3. ProjectsAdd Item. ,
.
4. MainModule.cpp.
5. Cancel, . Sepa
rate .
6. Compile^Make.
, r h i d e
, ,
Visual C++.

Visual C++
Visual C++, .
1. File^Close Workspace,
.
2. MainModule.cpp Com
pile ( Make).
Make, ,
.
3. Visual C++ .
, Visual C++
.-, .
, MainModule.cpp.
4. , ,
View^Workspace.
, :
(Class View) (File View).
.
. -.
5. ,
.
6. Mainmodule,
,
MainModule.



MainModule..

114

II,

7. Add Files,
, , , , Microsoft Word.
8. SeparateModule. cpp .
MainMcd
u l e . c p p SeparateModule.cpp.
9. Build .

#include
main () subse
quence (;. . ,
- .
,
, .
C++ ,
. ,
.
.
1. SeparateModule .h,
sumSequence {). . h ,
:
// SeparateModule.h

//
SeparateModule.cpp

int sumSequence
(ostreams out, istreams ir ) ;
2. MainModule. cpp,
SeparateModule . h .
// MainModule- ,
//
,
//
;
//
main()
#include <stdio.h>
#include <iostream.h>
//
#include "SeparateModule.h"
i n t main(int nArg,

char* pszArgs[])

ttinclude ,
SeparateModule.h. ,
, .
# i n c l u d e .

10.

115

,
. , ,
, , .
, .h-
#def ine . (
)
.
, #ifdef.
ifendif,
tiifdef (#ifndef
).
//Mylnclude.h
//, ;: #define MyModui e_h
// . , ,
//
#ifndef MyModuIe__h
// MyModuie_h ,
//;'^
//
#define MyModuie_h
//;.; , >:
/ / D O

// #ifndef
wendif
,
.

++
, ,
, # includ e < s t d i o . n > ^ i n c l u d e <io:;tream.h>.

.
, . h-
, .h-
. ,
C+ + , ,
( ), , ,
, .
,
.

116

II.

11

C++
/3 sfnou ...
S
S
/
/ BUDGET

t w ( ""),
** .
.
, .
, .
, ,
.

/tfauta
: , , ,
, (compile-time error).
, ,
. ,
( !), , ,
.
, ,
(run-time error).
, , , ,
(,
, "" ).
"" .
,
. ,
, .
.
.


,
, ( WRITE).
WRITE , FORTRAN,
WRITE.

11. C++

117

""
.
.
, ,
, .
// ErrorProgram

//
,

//

#include <srdio.h>

#include <iostream.h>

int mainfint argc, char* pszArgs[])


{

cout << " !\n";

// ,

// ,

//

ir.t nSum;
for (int r.Nums = 0; ; )

//

int nValue;

cout << "\ :";

cin >> nValue;

// ...

if {nValue < 0)
(

// ...

cout << "\ : "

<< nSum/nNums

<< "\";

break;

// ,

//

nSum += nValue;

return 0;

( <F9>).
1, 2 3, -1. ,
?
, . 11.1.

"" 1
, . 11.1, .
(
). :
Division by zero at . . . ( . . .). ,
-
( , ?).

118

::\wiNNT\System32\command.com

-jDlxl

|) : !
:2
^ :
[ :-1
[Exitin g Hue to s i g n a l SIGI-Pf
d i v i: s i o n by Zero at eip=0O00161a, x87 status-0000
|eox -000o81ba ebx-0O0901c5 ecx-000n81bci odx-00880800 esi-00000054 edi-0
Iebp"000a81a8 esp-000a817c progranHI A6CC\III\CRR0R.CXC
jcs: e1 "' base-01c58800 l i m i t O00bf
ids: sel=01af base=Olc5O000 limit-O80bffff
limit=BO0bffff
ies: sel-Olaf
}fs : sel=017f base=O0006d20 H m i t - 0 0 0 0 f f f f
sel=01bf
base=0800UOO0 linit=O010ffff
kjs:
sel=01af
limi1=QO0bffff
stack: I800n81cc. 08O281cc] bxceptn stack: 100028128.,080261e01
i l l frame traceb^ck CIPs:
0*0000161d
0x00010/46
|H:\GCC\J1J>
. .1.

. , ""
, (
).
, (
, : ,
- ).
, " ". ,
, ,
.
,
, Visual C++ GNU C++,
.
GNU C++ ( Visual C++
).
rhide , ,
<Ctrl+F9>. 1, 2 3, -1, .
f B

-r ^

,
. ,
, ,
.

r h i d e ,
"Program exiting code 255(0xfT)" ("
255(0xfT)"), . 11.2. , ,
0.
, , , - .
, Oxff.
, r h i d e
.

11. C++

119

f File Fdit Search Run Compile Debug-Project Options Windows HeJE


h:/gcc/ni/Error.cpp

15/

// ErrorPrograra
//
. .
//

ttincluif c <stciio.h>

intmaintintargc,
-.. cout "

Program exi t code: 255 (0x00ffj-

//
//
: //
int nStim;
1:1

F3 Open F5 ZOOM F6 Next

F18 Menu Hlt*K Quit

11.2. Oxff ,

^ 4 ^

,
. ,
<F6>.

,
, , . 11.3.
! " . , , * '

'

'

JLJ j

Kil e bdit~Search Hun Coetpile Debug t'roject Options Hindows'tielp


...
... h-/QCC/jj1 / j : r r o r c p p
// trrorProgram
//
,
//

^include <stc!io.h>
Kinclude <iostreara.h>
int main!int argc, char* pszfirgsH)
{ :
cout " !\';
// ,
// ,
//
.- int nSuM;

E r r o r , C P D 2 8 J in function Main
in function ,crtl,,staHup*178

ilnter JUMP to source l"b Zoom f-6 Next

HU Henu RH-X Quit

. 11.3. rhide ,

"Call frame tmccback" (" ") ,


.
, , .
120

II.

main {},
c r t l _ s t a r t u p ( , ?). ,
28 ErrorProgram. cpp. ...
28 :
cout << "\nAverage is : " // 2 6
<< nSum/nNums
// 27
<< "\";
// 28
, 28 . .
, C++
, .. 26, 27 28 . ,
2628 28- .
, 27.
, , nNums
. .
, , nNums ,
. nNums
for,
, for, :
for

( i n t nNums = ;

;nNums+-)

"" 2
, I, ,
, .
,
, .
- .
!

:
:
:
:
: 22 952 3

1
2
3
-1

C++
, MS DOS
Windows, ( Visual C++
r h i d e , , ).
?
C++ .
C++ .
,
,
, , , 200 16
.
C++
, .
.
,
.

11. C++

121

, - nNums nSum ( , )
. , ,
.
, nValue,
nSum.
.
nValue. nSurn nNums, fo r ,
:
for (int nNums = 0; ;nNums++)
{
//
int nValue;
cout << "\ :";
cin >> nValue;
// ...
if (nValue < 0)
{
// ...
cout << "\ : "
nSum/nNums
"\";
break;
//
cout << "nSura = " << nSum
<< "\n";
cout "nNums= " << nNums << "\n";
cout << "nValue= "<< nValue << "\n";
cout << "\n";

// ,
//
nSum += nValue;

,
nValue, nSum nNums .
1, 2, 3 -1 .
nSum - ,
( ).
!
:!
nSum = -858993460
nNums= 0
r.Value= I
:2
nSum = -858993459
nNums= 1
nValue= 2
:3
nSum = -858993457
nNums= 2
nValue= 3

122

II.

:
, , nSura
, . ,
:
i n t nSum = 0;
. ,
.
, , ,
:
// ErrorProgram
//

//

#include <stdio.h>
#include <iostream.h>
int main(int argc, char* pszArgs[])
{
cout << " !\n";
// ,
// ,
// .
int nSum = 0;
for (int nNums = 0; ;nNums++)
{
//
int nValue;
cout << "\ :";
cin >> nValue;
// ...
if (nValue < 0)
{
// ...
cout << "\ :"
<< Sum/nNums
<< "\";
break;

// ,
//
nSum += nValue;
return 0;
}

.
!
:1
:2

11. C++

123

:3
:-1
: 2
, ,
.

.

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


, , , r h i d e Microsoft Visual C++
( ,
).
, , ,
.
.

.
, .
, .
(
r h i d e , Visual C++ ).


C++ , ,
, . ,
.
, . GNU
C++ Visual C++
. . 11.1
.

124

11.1. MicrosoftVisualC++ GNUrhide


VISUAL C++

GNU C++
(RHIDE:)

(Build)

<Shift+F8>

<F9>

(Step In)

<F11 >

<F7>

<F10>

<F8>

(Step Over)
(View Variable)

<Ctrl-rF4>

(Set Breakpoint)

<F9>

<CtrkF8>

; (Add watch)

<Ctrl+F7>

(Go)

<F5>

(View
Screen)
(Program Reset)

User


<Shift+F5>

<CtrkF9>
<Alt+F5>
<Ctrl+F2>


.
, .
//
Concatenate
//

" "

bill .

//
( )
#include <sndio.h>
#include <ios~rearr.. h>
void concatString(char szTarget[], char szSource[]};
int main.int nArg, char* pszArgs[])
cout << " \" ;
cout << "( .)\\";
// ...
char szStrir.gl [256] ;
cout << " #1:";
cin.getline(szStringl, 128);
// ... ...
char szString?. [128];
cout << " #2:";
cin.getline(szString2, 12 8; ;
// ... " " ...
concatString ( szSr_ringl , " " ) ;
// ... ...
concatString(szStringl, szString2);
// ...
cout << "\n" << szStringl << "\";
return 0;
// concatString - * pszSource

11. C++

125

//
* pszTarget
void concatString (char szTarget[], char szSourcel])
int nTargetIndex;
int nSourcelndex;
//
while(szTarget[++nTargetIndex])

//
while(szSource[nSourcelndex])

szTarget[nTargetlndex] =
szSource[nSourcelndex];
n7argetlndex++;
nSourceIndex++;

. ,
" ", " "
(, , ).
Oxff.
( ). ,
, :
Call frame traceback
Concatenate.epp(4 9) in function concatString FPcTO
Concatenate.epp(28} in function main
in function
crtl__startup+174
, 49 Concatenate , epp,
c o n c a t S t r i n g (), 28 main () .
49
while(szTarget[++nTatIndex])
28
concatString(szStringl, " " ) ;
49,
28. , .
. ,
.


, ,
. rhid e Runoprogram Reset.
. . 11.1 , rhid e
<Ctrl+F2> { <Shift+F5> Visual C++).
, . 11.1. ,
, , .
Program Reset {
, ).
.
Step Over ( ).
r h i d e MS DOS, ,
,

126

II.

. ( ,
.
.)

main (), .
.
Step Over r h i d e
.
. (User Screen),

.
.
,
. , ,
.
Step Over
c i n . g e t l i n e () , , .
, , :
, , g e t l i n e ()
, .
<Enter>.

cout << " #2 : ". .



, , -
. ,
.
c o n c a t s t r i n g {)
Step Over , (. 11.4).
SelNH- Run Conpile

15/16

l-tll-l

h:/sss.cpp

// . . . n<"'iriWTh .. . .

concatStringtszStringl, s7String2);

// ...

cout << " << szStringl << "\n";

returnf>:
// concatString - n
//

v o i d conccitStringlc
t
i n t nTargetlnde
34:1

Program has r e c e i v e d s i g n a l :
SIGftBRT. flborted

Open Fb ZOOM 1-6 Next

9 L

10 Menu i l l l ~ K Quit

. 11.4. concatstring

11. C++

127



.
.
.
Program Reset.
Step Over,
c o n c a t s t r i n g () . Step In (
),
c o n c a t s t r i n g (>, . 11.5.
r

cFC/rhkte

File'Edit"Setfrch^-Buh'-Conpiie Debug Project Options Windows Help


(*I======-_======= h:/sss.cpp
//
" pszTarget
void concatStringichar szTargetll, char szSourceN)
{ int nTargetlndcx;
int nSourcelndex;

15/16"

'\

//
while(szSourcclnSourceIndexl }
U.I

F2

Save

F3

">"-~

~~

Open F5

Zoon

F6

Next flli*F9 C o m p i l e

F10

Menu fill* Quit

. 11.5. Step In

Step In Step Over ,
.

Step In,
,
. , - .
Cancel
(
).
, ,
, , Go.
Step Over,
. r h i d e ,
.

128

II.

, while
. ,
,
while .


.
, ,
, .
, ,
Program Reset. while
, ,
.
. while
Set breakpoint. , . 11.6.
lFC/W:rWde
file Edit Search BtiH 1041 Options Hindows Helt
h:/sss-cpp
intnlargetlndex;

intnSourcelndex;

15N/17H
1-IM-j

//
hile(szTargeU**nTargetIndex])
[
//
whilefszSourcelnSourcelnclexlJ

szTargetlnlargetlndex] szSourcetnSourcelntlex]
;
J :
' 44:1
is.

'
1-2 Save h3 Open Fb ZOOM 16 Next R1M9 Conpile I-IB Hcnu 11 Quit
. 11.6.
,
.
. , ,
,
.
Go.
, while.
.
, .


while, ,
. ,
, . ,
, nTarget Index
while.

11. C++

129

>tFC/W:rMde
Fdit Search Run &

h:/sss.cpp

15/
1-fT1-3

intnTargetlndex;
intnSourcelndex;
iiTarfletIndex

Help

. 11.7.

nTarget Index,
View Variable.
. Eval,
. . 11.7.
, ,
n T a r g e t l n dex nSourcelndex. ,
(New Value) 0 (
nScurcelndex).
Step Over.
, ,
. Step Over Step In
while. ,
while. n T a r g e t l n dex 1.
nTargetlnde x
, nTargetlnde x
Add Watch. .
Step In .
nTargetlnde x 1,
.
,
c o n c a t s t r i n g , Go.
.
,
szTarget View Variable. . 11.8.
0x68298 .
.
.

130

II.

! _cSfc/*it*le
E File Edit
t
1
/ / non e
"

SearchRun

COM

ilfi
JiJe I)

*-h
buQP
i nsW
sH
:/sssro
.cie
pcptOoto
nidow

elt
tieoDOrt

cir

111

_jni_xj
15H/14H

xpr6ssion

I
H i

esult
ew

value

I szTarget:

iJB
BBS1
Bflft

. 11.8.

(
)
.
, , ,
( , ). Pro
gram Reset c o n c a t s t r i n g , :
void
{

corcatString(char

szTarget[],

char

szSource[])

i n t nTargetIndex = 0;
int nSourcelndex = 0 ;
//
while (szTarget [+J-nTargetIr.dex] )

//
whij e(szSource[nSourcelndex])
{
szTarget{nTargetlndex; =
szSource[nSourcelndex];
nTargetIndexi-+;
r.SourceIndex + + ;

//
szTarget[nTargetlndex] = ' \ G ' ;
)

To, , ,
"". .
r.Targetlndex r.Sourcelndex
.

11. C++

131

.
, , ,
, .
, ,
Go.
.

( .)

#1:

#2:

, ,
( ) .
BUDGET.
.
, . ,
, , .
"" (.. ) C++.
BUDGET (
). :
| / ;
fi

. / ;
| / ;
I ^ ,
.

,
( , !).
.
BUDGET .
// BUDGET1.CPP ""

#include <iostream.h>

#include <stdio.h>

//

const int maxAccounts = 10;


//

unsigned accountNumber[maxAccounts];

double
balance[maxAccounts];

//

void process (unsigned^ accountNurnber,

doubles
balance);

132

II.

void init(unsigneds accountNumber,


doubles
balance);
// main
/ /
int main(int nArg, char* pszArgs[])
{
//
int noAccounts = 0; //
//
while (riOAccounts < maxAccounts)
{
char transactionType;
cout << " , X :'
c;.n >> transactionType;
/'/ , X
;.: (transactionType == ' ' | |
transactionType == 'X')
1
break;
...
(transactionType == '1 ||
transactionType == '')
// ... ...
init(accountNumber[noAccounts],
balance[noAccounts]);
// ...
process(accountNumber[ncAccounts],
balance[noAccounts]>;
//
noAccounts++;

//
//
double total = 0;
cout << " :\n";
for (int i = 0; i < noAccounts; i++)
i
ccut << " "
<< accountNumber[i]
<< " = "
<< balance[i]
<< "\n";
//
total += balance[i];

//

11. C++

133

cout << " = "


<< total
<< "\";
return 0;
1
// init ,
//

void init(unsigned& countNumber,
doubles
balance)
{
ccuc << " :";
cin >> accountNumber;
balance = .0;
// process
//

void process (unsigneds accountNurober,
doubles
balance)
{
cout << " \"
<< " \'
double transaction;
do
(
cout << ":";
cin >> transaction;
// ?
if (transaction > 0)
{
balance += transaction;
// ?
if (transaction < 0)
{
//
transaction = -transaction;
if (balance < transaction)
{
cout << " :
<< balance
<< ", "
<< transaction
<< "\";
}
else
{
balance -- transaction;
} while (transaction != 0) ;

134

II.

,
( , ):
X :

-: : 1234


:200
:-100

200
: 100, 200

:0

X :

:2345

:200

:-50

:-50

:-50

:0

X :X

1234 = 100

2345 = 50

= 150

, ,
: ,
.
"".
,
:
, - .
, ,
'' ,
. , ,
. , ,
! .
, ,
,
; . ,
.
< , , ,
? , , ,
= , .
,
.
, , ,
. , ,
= , -.
, BUDGET.
, , .
, b a l a n c e [n]
accountNumber [n], .

11. C++

135

, ,
MAXACCOUNTS

: ;
, ,
. , ,
, .
X
. X,
main ().
,
MAXACCOUNTS, , .
,
'X', ''
.
'' , i n i t o ,
.
process (> .
i n i t () proces s {) ,
.
.
,
.
. .
i n i t ()
.
.
(, ).
p r o c e s s ! ) .
, .
.
0 .
, .
, .
BUDGET.
C++. ,
-
C++.

136

//.

III

""

^^

/...

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

12



& ...
S
S
/

- ?
, , ,
: .
, , .

tf/
,
, .
, , , .
, ,
.
( , ).
, , .
S .
,
:
; , .
'i s He ,
.
S .
*/
,
, .

.
. ,
.
,
, . ,
,
.

12. -


. ,
, "" , , .

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

. ,
.

"-"

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

.
, .
C++.

/<11

.
: " ?"
: " , ...". : " ?"
- : ", , ...". (
, , ,
.)
,
, . ,
, .

. ,
, , .

140

///. ""

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


? .
, . - ?
,

. ,
. ,

. , ,
. , ?

!
,
" ". .
: "
. .
3000 . .
, ". !
.
%
|
I
I
S
fV
|
|

. ,
.

, ,
.
. -
. ,
.
,

.
.
. , ,
, . ,
.

12. -

13

C++
S

: , ,
^ ^ ..
, . ,
, ,
. ,
.
, ,
. .
( , ). ,
, , .

/&

.
, .
C+ + ,
, .


, ,
, ;
// dataset
class NameDataSet
{
public:
char firstName[128];
char lastName [128];
int creditCard;
}
// dataset
NameDataSet nds;
c l a s s ,
, .
s t r u c t ,
c l a s s , p u b l i c .
public . (He
, , .
, p u b l i c p r i v a t e .
p r i v a t e , p u b i i c :-).)

142

///. ""

p u b l i c .
, NaneDaraSet ,
. , i n t
( , ).
,
.
nds,
NameDataSet. , nds ,
.
, nds KameDataSet
, NameDataSet. , firstName
, .
.

:
NameDataSet nds;
nds.creditCard = 1;
cin >> nds.firstName;
cin >> nds . lastNairve;

nds NameDataSet ( NameDa


t a S e t ) ; n d s . c r e d i t C a r d nds;
nds . c r e c i t C a r d i n t , ,
nds . firstName, char [ j .
,
: nds,
. -
10 (,
!).
.
nds ,
, .
i n t getlJata (NameDataSet& nds)
{

coir: << " : " ;

cin >> nds.firstName;

if fstricmp(nds.firstName, "exit") == 0)
{

return 0;

cout << " :";


cin. >> nds . lastName;

cout << " :

cir >> nds.creditCard;

return 1;

13. C++

743

// displayData
void displayData(NameDataSetfi nds)
{
cout << nds.firstName
<< " "
<< nds.lastName
"/"
<< nds.creditCard
"\n";
}
int main(int nArg, char* pszArgs[])
{
const int MAX = 25;
// 25 NameDataSet
NameDataSet nds[MAX];
// ,
//
coat << " / :\"
<< " 'exit'\"
" \";
int index - 0;
while (getData(nds[index]) & index < MAX)
{
index++;
}
cout "\:\n";
for (int i = 0; i < index; i++)
{
displayData(nds[i]) ;
}
return 0;
}


NameDataSet,
// DataSet
//

#include <stdio.h>
#include <iostream.h>
#include <string.h>
// NameDataSet
/ /
class NaineDataSet
{
public:
char firstName[128] ;
char lastName [128];
int creditCard;
};
int getData(NameDataSetS nds)
{
cout << " : ";

144

HI. ""

cin

>> nds.firstName;

if (stricmp(nds.firstName, "exit") == 0)
{
return 0;
out << " : ";
cin >> nds.lastName;
out << " :
cin
nds.creditCard;
return 1;
// displayData
// NameDataSet
void displayData(NameDataSet& nds)
{
cout << nds.firstName
<< " "
<< nds.lastName
"/"
nds.creditCard
<< "\n";
}
int mainfint nArg, char* pszArgs[])
{
const int MAX = 25;
// 25 NameDataSet
NameDataSet nds[MAX];
// ,
//
cout << " / \"
<< " 'exit' ";
int index = 0;
while (getData(nds[index]) & index < MAX)
{
index++;
}
cout << ":\n";
for (int i = 0; i < index; i++)
i
displayData(nds[i]);
)
return 0;
}
main () 25 NameDataSet,
.
whil e getDat a ,
. , getDat a ()
0
( 25).
displayData , .

13. C++

145

g e t D a t a O NameDataSet,
rids. "&"
15, " ".
g e t D a t a O
firstName. s t r i c m p O
, " e x i t " , getData O
main (), , . (
s t r i c m p O , . "EXIT",
" e x i t " .) " e x i - " ,

nds.
dispIayDat a () nds.
:
/
'exit'
: Stephen
: Davis
: 123456
: Marshall
: Smith
: 5678 90
: exit
:
Stephen Davis/123456
Marshall Smith/567890
, .
(, !). e x i t ,
.
. Marshal l Smith
. e x i t
. , ,
.

146

III, ""

14


6 ...
/
-/ -
S -
/ -
S
S -
S -
S -

*77

^ *' . Savings
.
class Savings

public:

unsigned accountNuraber;

float balance;

};

Savings :
void fn(void)

Savings a;

Savings b;

a.accountNumber = 1; // , ...

b.accountNumber = 2; // ...

a.accountNuniber b. accountNumber.
,
( ).

.
C++ ,
. ,
Savings , -
.

14.

147


-,
. , :
, ,
..

. C++ strcmp ()
getLine () . 26,
" -", ,
- (in >> cout <<) ,
.
Savings
:
class Savings

public:

unsigned accountNumbere

float balance;

unsigned deposit(unsigned amount)

balance += amount;

return balance;

Savings
d e p o s i t (). Savings
. , MicrowaveGven ( )
cook () (), Savings d e p o s i t ().
, , -.

-
-?
:
class Savings

public:

unsigned accountNumber;

float balance;

I;
unsigned deposit(Savings & s, unsigned amount)
s.balance += amount;

return s.balance;

i
: "&"
.
deposit() " ".
,
Savings. ,
, -
.

148

III. ""

, "",
. -
, -
.
,
. , ,
.. ,
" ". , - Savings, de
p o s i t (), .
, , .
,
, , ( )
. Savings,
, , "".

2)<5 cfti/-
: -
( , ?).

-
-,
Student :
class Student

public:

int
semesterHours;

float gpa;

//

float addCourse(int hours, float grade)

//

//

float weightedGPA;

weightedGPA = semesterHours * gpa;

//

semesterHours += hours;

weightedGPA +=
grade * hours;
gpa = weightedGPA / semesterHours;

//

return gpa;

addCourse ( i n t , f l o a t ) - Student.
, Student, semesterHours gpa.
,
, ,
,
- .

14.

149

-
. -
, C++. ,

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


- .
a d d C o u r s e ( i n t , f l o a t ) s t u d e n t : : a d d C o u r s e ( i n t , f l o a t ) ,
.
addCourse ( i n t , f l o a t ) , - .
, Student (: :
-).
, .
.
addCourse ( i n t , f l o a t ) ,
Student; ,
, (
: ,
).
Teacher: : addCourse (ir.t, f l o a t )
Golf: : addCourse (int., f l o a t ) . addCourse ( i n t , f l o a t )
, ,
.

-
- , ,
- :
class Student

public:

int
semesterHours;

float gpa;

};

Student s;

void fn(void)

// - s

s . seinesterHours = 10;

s.gpa = 3.0;

,
. , .

150

III. ""

Student s;

void fn(void)

//

semesterHours = 10;

/ / ?
Student::semesterHours = 10;

// , ,

// ,

//

-
- -
, .
Student f;
void fn(void)

// s

s.semesterHours = 10;
s.gpa = 3.0;

s . a d d C o u r s e ( 3 , 4 . 0 ) ; / / -
}

, - ,
- . ,
, .
, .
: "s ,
addCourse () "; , , s
, .
, , .
- ,
- .

2) -
, : " -
! -
! ..." , - s t u
den t : : addCourse () ... ? addCourse () ,
!
: - ,
? , . ,
Student addCourse (), ,
addCourse (). ? :
#include "student.h"

float Student: :addCourse (int hours, float grade)

float weightedGPA;

weightedGPA =
semesterHours * gpa;
//

14.

151

semesterHours += hours;
weightedGPA += hours * grade;
gpa = weightedGPA / semesterHours;
return gpa;
int main(int argcs, char* pArgs[])
Students;
Student t;
s.addCourse{3, 4.0); // 4
t. addCourse (3, 2.5); /'/ 2 ...
return 0;
addCourse () s,
s. , s e mesterHours s . addCourse ()
s. semesterHours, gpa s.gpa.
main (), addCourse () t
Student, t t . semesterHours t . gpa.


- , ?
-
. , -
:
s . a d d C o u r s e ( 3 , 2 . 5 ) S t u d e n t : : a d d C o u r s e f&s, 3, 2.5)
(, , ;
,
).
, ,
, .
.
, - ,
, ,
(this) .
, t h i s . S t u d e n t : :addCourse ()
:
float Student::addCourse(int hours, float grade)
{
float weightedGPA;
weightedGPA - this->semesterHours * this->gpa;
//
this->semesterHours += hours;
weightedGPA += hours * grade;
this->gpa = weightedGPA / this->semesterHours;
return this->gpa;
}
, t h i s - >
, .

152

III. ""

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

^ &
: :
, ,
. ,
.
: : ,
. addCourse ()
: : a d d C o u r s e ( i n t , f l o a t ) .
: : , .
:

// addCotrse

f l o a t adcCourse(in hours, f l o a t grade)

return

h o u r s * g r a d e ;

c l a s s S t u d e n t

p u b l i c :

int
s e m e s t e r H o u r s ;

f l o a t g p a ;

//

float addCourse(int hours, float grade)

<

//

v^eightedGPA = addCourse (semesterHours, gpa);

//

//

weightedGPA += addCourse(hours, grade);

gpa = weightedGPA / semesterHours;

//

return gpa;

, - S t u d e n t : : addCourse ()
: : addCourse () . : : addCourse
Student S t u d e n t : : addCourse () .
-
,
.
,
. : :
( ):

14.

153

// addCourse
float addCourse(in hours, float grade)
{
return hours*grade;
}
class Student
[
public:
int
semesterHours;
float gpa;
//
float addCourse{int hours, float grade)
{
//
weightedGPA = ::addCourse(semesterHours, gpa)
//
//
weightedGPA += ::addCourse(hours, grade);
gpa = weightedGPA / semesterHcurs;
//
return gpa;

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

-
- , .
, , s t u den t . h:
class Student
(
public:
int semesterHours ;
float gpa;
//
float addCourse(int hours, float grade)
{
//
//
float weightedGPA;
weightedGPA = semesterHours * gpa;

154

III. ""

//

sernesterHours += hours;

weightedGPA += grade * hours;


pa = weightedGPA / semesterKours.

//

return gpa;

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

. - .
, .

, , ,
. C++. -,
, inline-,
.
^
;' -, ; ,




. , C++
- .
s t u d e n t , , :
class Student

(
public:

int
semesterKours;
float; gpa;

14.

155

//

float addCourse(int hours, float grade)

//

// Student

float Student::addCourse(int hours, float grade)

float weightedGPA;

weightedGPA = semesterHours * gpa;

//

semesterHours += hours;

weightedGPA += grade * hours;

gpa = weightedGPA / semesterHours;

return gpa;

addCourse ().
.
-
, , ,
.
Student S t u d e n t : : addCourse ()
. , .
Student , s t u d e n t . h ,
,
Student..
S t u d e n t ,
. S t u d e n t .
.obj-,
.
6, " ".

-
- , (
6, " ", , ). ,
,
.
class Student

public:

//grade

float grade();

//grade

//

float grade(float newGPA);

II... - ...
};

class Slope

public:

//grade

156

///. ""

float grade () ;
//... -...
};
//grade
char grade{float value);
int main(int argcs, char* pArgs[])
{
Student s;
s.grade(3.5};
//Student::grade(float)
float v = s . grade ();//Student::: grade ()
char = grade(v);

//::grade (float)

Slope o;
float m = .grade();//Slope::grade()
return 0;
}

main () .
,
, ,
( ).
.
-
S t u d e n t : : grade ( f l o a t ) S t u d e n t : : grade () .
.
s . grade ()

S t u d e n t : : grade () , s Student.
: : grade ( f l o a t ) ,
.
Slope, - Slope : :grad e ( f l o a t ) .

14.

157

15


...
S
S
S

/
S

Linked List Data

*77

C++ -.
* * , ;
? (
, ). Student,
, , .

OnfteqeAettue

. ,
.
, , 0.
C++ ,
.
int array[10];

// 10

,
:
a r r a y [ 0 ] = 10;
[ 9 ] = 20;

// 10
// 20

( 0)
10, 2 0.
, C++ 0
, , 1.

, ,
, .
.
.

158

///. ""

innvariable;

//
int" pVariable = &.variable;
// pVariable
*pVariable = 10; // 10 ,
// pVariable
pVariabl e ,
v a r i a b l e . ,
pVariable , 1.
( , !),
:
S v a r i a b l e ;
^ pVariabl e ;
,
10, , .
, ( ,
).
7, " ",
() , 8, "
C++ 1 ', 9, " ",
.

, .
:
c l a s s Student
{

public:
int
semesterHours;
float gpa;
float addCourse(int hours, float g r a d e ) ;
};
void someFn()
{
// 10
Students[10;;
// 5.0
s [ 4] .gp a = 5 . 0 ;

(!]

// ,
// ...
s [ 4 ] . a d d C o u r s e ( 3 , 0.0 ) ;
}

s s t u d e n t . s [ 4 ]
, , s [ 4 ] -gpa
. s [ 4 ] . a d d C o u r s e ()
.

15.

159


, .
class Student
{
public:
int
sernesterHours;
float gpa;
float addCourse fint hours, float grade) {return 0.0};
};
int main(int argc, char* pArgs[])
{
// Student
Student s;
// Student
Student* pS;
// pS s
pS - &s;
return 0;
}
pS " s t u d e n t " ; ,
s t u d e n t * .


,
s.
int main(int argc, char* pArgs[])
{
//
Student s;
Student* pS= &s; // s
// gpa ,
// pS ( )
*pS.gpa = 3.5;
return 0;
}
, . ,
". " " * " .
C++ . ,
, .
i n t i = 2* (1+3);

//

.
i n t m a i n ( i n t a r g c , char* pArgs[})
{

Student

s;

Student* pS= Ss; // s


// gpa ,
// pS ( )
(*pS).gpa = 3.5;

160

III. ""

return 0;
"ps , pS, , .gpa
.


*
.
, .
C++ ->,
*;
, pS->gpa (*pS) .gpa.
,
( ).

/

.


, C++
. ( 6, " ", .)
, :
.
#include "Student.h"

// Student no
void soneFn(Student valS)
cout << "GPA = "
valS.GPA "\n";
int main(int argc, char* pArgs[])

i
Student s;

s.semesterHours = 10;

.gpa = 3.0;

// s

s ome Fn(s);

return 0;

main () s,
s ome Fn ().
5^^

, .

15.

161

v a i s soraeFnO
s main ().
valS s main {).


,
.
#include <stdio.h>
#include <iostreara.h>
class Student
(
public:
int
semesterHours;
float gpa;
float addCourse(int hours, float grade){return 0.0};
};
void someFn(Student* pS)
f
pS->semesterHours = 10;
p5->gpa = 3.0;
pS->addCourse (3, 4.0); // -
}
int main(int argc, char* pArgs[])
{
Student s;
// s someFn(>
someFn fSs);
// pS
Student* pS;
pS = Ss;
someFn(pS);
return 0;
}
, someFn () ,
Student . s someFn ()
s.
: ps
-.

someFn ().


, :
? , ,
,
.
-, ,
.
, :

162

III. ""

#include <stdio.h>
#include <iostream.h>
class Student
{
public:
int
semesterHours;
float gpa;
float addCourse(int hours, float grade)(return 0.0;};
};
void someFn(Student copyS)
{
copyS.semesterHours = 10;
copyS.gpa
= 3.0;
copyS.addCourse(3, 4.0); // -
}
int mainfint argc, char* pArgs[])
{
Student s;
s . gps. = 0.0;
// s.gpa someFn
cout " s.gpa = " s.gpa << "\n";
//
cout << " someFn(Student)\n";
someFn(s);
// s.gpa someFn 0
cout << " .gpa = " << s.gpa << "\n";
return 0;
}
someFn ()
s. someFn () ,
. , copyS
s, soraeFn ()
s, , main .
.
s.gpa = 0
someFn(Student)
s.gpa = 0
someFn () ,
, :
#include <:stdio.h>
#include <iostream.h>
class Student
{
public:
int
semesterHours;
float gpa;
float addCourse(int hours, float grade){return 0.0;};

15.

163

void someFn(Student * pS)


{
pS->sernesterHours = 10;
pS->gpa
= 3.0;
pS->addCourse(3, 4.0);

// -

int main(int argc, char* pArgs[])


{
Student s;
s.gpa = 0.0;
// s.gpa someFn
cout << " s.gpa = " << s.gpa << "\n";
//
cout << " someFn(Student)\n";
someFn(&s);
// s.gpa someFn 3.0
cout << " s.gpa = " << s.gpa << "\n";
return 0;
)
someFn ()
s ( ,
).
:
s.gpa = 0
someFn(Student)
s.gpa = 3 . 0


9, " ",
, .
#include "Student.h"
// ,
void someFn(Students refS)
I
refS.semesterHours = 10;
refS.gpa = 3.0;
refS.addCourse(3, 4.0); // -
}
Student s;
int mainfint argc, char* pArgs[])
{
someFn(s);
return 0;
}
someFn () ,
. , s ome Fn () s, ma in () .
s;
C+ + .

164

III. ""

[tct/
, ,
. , ,
. ,
, :
MyClass* myFunct)
{
//
MyClass me;
MyClass* pMC = &mc;
return pMC;
}
myFunc () , ,
, myFunc (), .
:
MyClass* myFunc(}
{"
MyClc.ss* pMC = new MyClass;
return pMC;
}


.
:
.
,
, .

,
:
MyClass :[100];
[ 3 ;

// 100
/ / (+1) -

.
.
,
, , , .
void
(

fnfint nSize)
//
//MyClass
MyClass* pMC = new MyClass[n];

//

15.

165

//

, .
MyClass YourClass .
, .
,
. (
, , .)
,
. , , .


, ,
, .
.
, .
.
, .
class LinkableClass

public:

LinkableClass* pNext;

//

};

LinkableClass.
,
. ,
.
pNext ,
. , ,
, "".
.
LinkableClass*,
, ,
"" ( ,
LinkableClass).
0. ,
, "".
0 .
L i n k a b l e C l a s s t
. C++ 0 ( ,
" ").
,
, .
void addHead(LinkableClass* pLC)

pLC->pNext = pHead

pHead = pLC;

166

III, ""



. :
void addlail(LinkableClass* pLC)

//

LinkebleClass* pCurxent = pHead;

// ,

//

// pNext
while- (pCurrent->pNext != f LinkabieClass* ) 0]

pCurrent = pCurrent->pNext;

// LC
pCurrent->pNext = pLC;
// 0
// ,
//
pLC->pNext = (LinkableClass&*}0;
}

addTail ()
, pNext .
. addTail () pLC* .
( .
,
pHead. pHead , .)
remove () .
1, , 0 .
void remove(LinkableClass* pLC)

LinkableClass* pCurrent = pHead;

// , *pLC

if {^Current == (LinkableClass*) 0)

return 0;

//

//

while (pCurrent->pNext);

//

if(pLC==pCurrent->pNext)

//..-

//

pCurrent->pNext = pLC->pNext;

// ,

//

pLC->pNext = (LinkableClasss*)0;

return 1;

return 0;
}

15.

167

removed , . ,
0 ( *pLC ).
, remove ,
. ,
pNext pNext . , ,
, ,
.


, .

.
. ,
, .
,
.
,
.
.

JlfiOiftcuuta fjnked&stData
LinkedListDat a
, .
// LinkedListData
//

#include <stdio.h>
#include <iostream.h>
#include <string.h>
// NameDataSet
//

class NameDataSet
{
public:
char szFirstName[128];
char szLastName [128];
int nSocialSecurity;
//
NameDataSet* pNext;
};
//
NameDataSet* pHead = 0;
// addTail
void addTail(NameDataSet* pNDS)
{
// ,
//
pNDS->pNext = 0;
// ,
//

168

///. ""

//

if {pHead == 0)

pHead = pNDS;

return;

// ,

NameDataSet* pCurrent = pHead;

while(pCurrent->pNext)

pCurrent = pCurrent->pNext;

//

pCurrent->pNext = pNDS;

// getData

//
; ,

//

NameDataSet* getData()

//

NameDataSet* pNDS = new NameDataSet;

//

cout << " : ";

cin >> pNDS->szFirstName;

// 'exit'...

if ((stricmp(pNDS->szFirstName, "exit") == 0))

// ... ...

delete pNDS;

// ... ,

return 0;

//

cout << " : ";

cin >> pNDS->szLastName;

cout " : ";

cin >> pNDS->nSocialSecurity;

//

pND->pNext - 0;
//

return pNDS;

// displayData NameDataSet

void displayData{NameDataSet* pKDS)


{

cout; pNDS->szFirstName

<< " "


pNDS->S2LastName

15.

169

<< pNDS->nSocialSecurity
"\n";
int main(int argc, char* pArgs[])
cout << " / \"
" 'exit' \";
// NameDataSet
NameDataSet* pNDS;
while (pNDS = getDataO)
f
//
// NameDataSet
addTail(pNDS);
//
// ( ,
// )
cout ":\";
pNDS = pHead;
while(pNDS)
//
displayData(pNDS);
//
pNDS = pNDS->pNext;

I
return 0;

, LinkedListData
. main () getData (),
NameDataSet . " e x i t "
, getData () . main (} addTail (),
, getData (), .
NameDataSet,
main () , displayDat a ().
g e t D a t a O NameDataSet.
getDat a ()
. " e x i t " ,
0. g e t D a t a O
, pNext
.

! : " ".
addTai l ()
addTai l , ,
. pHead , addTai l
.
d i s p l a y D a t a () .

170

III. ""

16


!
/3 1 ...
S
/
S

13, " C++" , .


p u b l i c , .. ,
. , p u b l i c .

,
. , (public )
.


, ,
- .
i / .
1
( - ),
?

)
. ,
|
.
1
.
' S , I
.
t

I
( ,
\

I
).
1
S .
i
,
|
( ).
, (
;,
|
).
I
, 8, " I
C + + " .

16. : !

171

I ^ I
. ,
|
- .
, : "
! ,
".
, .
, ,

.


p u b l i c
, , .
p r o t e c t e d ,
, .. , .
.
, s t u d e n t .
, , (
spendKoney ( ) drinkBee r (
), ):
addCourse ( i n t h o u r s , f l o a t grade) ;
grade f) ;
hour s () .
s t u d e n t ,
"" s t u d e n t .
class Student
{
public:
// grade
float graded
{
return gpa;
}
// hours
int hours f)
{
return semesterHours;
}
// addCourse
//
float addCourse(int hours, float grade)
//
protected:
int semesterHours; //
float gpa;
//
};
semesterHours gpa
Student, .
Student s ;
int mainfint argc, char* pArgs[])

172

III. ""

// ( ,

// )

s.gpa = 3.5;
//

float gpa = s.grade(); //

// ,

//

return 0;

gpa
.

,
p u b l i c p r i v a t e .
, .
.

p r i v a t e . ,
p r i v a t e . p r o
t e c t e d p r i v a t e 21,
" ".

xoftouiu
, ,
, .


p r o t e c t e d gpa
.
, .
gpa,
, , :
class Student
{
public:

// grade ,

float grade()

return gpa;

//

float grade(float newGPA)

float oldGPA = gpa;

//

if (newGPA > 0 && newGPA <= 5.0)


{

gpa = newGPA;

return oldGPA;

16. : !

173

II...
protected:
int semesterHours; //
float gpa;
};
grade ( f l o a t )
gpa. ,
.
gpa , ,
0 5.0.
s t u d e n t ,
.


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


,
.
f r i e n d ().

" .,."
-.
""
, ,
.
,
, .
, , .
,
. ,
, ,
.
Student .
class Student;
{
friend void initialize(Student*);
public:
// ,
protected:
int semesterHours; //
float gpa;

774

III. ""

// Student

//

void initialize(Student *pS)

pS->cpa=Q;
//

pS->=emesterHours = 0;


. , , . ,
,
. , ,
.
class Student;

class Teacher

i
friend void registration();

protected:

int noStudents;

Student *pList[100];

public:

void assignGrades();

};
class Stjdent

friend void rp^i.t:". .' '

public:

// ,

protected:

Teacher *;
int semesterHours; //

float gpa;

};

r e g i s t r a t i o n
s t u d e n t Teacher, ,
.
, s t u d e n t ,
. :
.
, , , , Teacher,
s t u d e n t . ,
. -
:
class Teacher

// ,

public:

void assignGrades();

);
class Student

friend void Teacher::assignGrades();

public:

// ,

protected:

int semesterHours; //

16. : !

175

float gpa;

};

void Teacher::assignGrades
{);
{

//

// Student

, -
, s t u d e n t .

. , -
, :
class Student;

class Teacher

protected:

int noStudents;

Student *pList[100];

public:

void assignGrades();

};

class Student

friend class Teacher;

public:

- -

// ,

protected:

Teacher *pT;

int semesterHours; //

float gpa;

);
- Teacher
s t u d e n t .
.

176

///. ""

17

:

& ...


,
\S . ,
. C++
, C++ (,
, , ).
, , " ".

.
? ?
Dog,
. , . ,
Dog (, :
...).
, .
Dog , .
,
Dog, , .
, .
, Dog ( ) .
.
, . ,
.
, -
. ,
, .
,
pickOne ().
int me;
void pickOne (}
<
int noMe;

17. : 177


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

-,
.
, .


,
:
struct Student

int
semesterHours;

float gpa;

};
void fnf)
{

Student s = {0,0};

// ... ...

C++,
.
.
class Student

public:

// ... ...

protected:

int
semesterHours;

float gpa;

};

void fn()
{

Student s = {0,0);
// ,

// -

// ... ...

f ()
semesterHours gpa, (
).

178

///. ""

, ,
, .
, .
.
class Student

public:

void init()

semesterHours = 0;

gpa = 0.0;

// ... ...

protected:

int semesterHours;

float gpa;

};

void fn ()
{

Student s;
// ...

s.init{);
// ...

// ... ...


. , ,
- i n i t ().
.
,
.

!


~ -,
. ,
. ( ,
- . , C++
, , : "
i n i t { ) ' \ , ;
.)
, ,
( ,
).
:
#include <iostream.h>

class Student

public:

Student()

out << " Student\n";


semesterHours = 0;

gpa = 0.0;

17. : 179

// ... ...
protected:
int
semescerHours;
float gpa;
void fn()
{
Student s;
//
// ... ...
}
s t u d e n t : : s t u d e n t (]
, s.
(inline) .
,
:
#include <iostream.h>
class Student
{
public:
Student();
// ... ...
protected:
int
semesterHours;
float gpa;
i;
Student: :Student
(
cout << " Student\n";
semesterHours = 0;
gpa = 0.0;
I
voidf.n(]
{
Student s;
//
// ... ...
int main(int arges, char* pArgs[])
{
fn();
return 0;
I
main() ,
.
, .
GNU C++ 29, "
".
,
s. (Step into),
s t u d e n t : : s t u d e n t () (
inline- , , Outline
inline functions, ;

).
. ,
.

180

///. ""

. , ,
:
v o i d f. n ;!
Student s [5];
//
//... ...
}

.

, , ,
( ) .
, .
!
-, ,
s t a d e n t :
void n i)
{
Stucent s;
//
// ... ...
s . St udenz () ; // ,
//
}


void.
-, ,
.
, ,
, :
# i n d u c e <iostream . h>
c l a s s Student
{
public :
SU.dent ()
{
coui << " Student\n";
semes torHours = ;
gpa = 0.0;
)
// ... ...
protected:
int
semesterHours;
float gpa;
};
class Teacher
(
public:
Tea-her()
cout << " Teacher\n";
class T jitorPair

17. : 181

public:

TutorPair()

cout << " TutorPair\n";

noMeetings = 0;
)

protected:

Student student;

Teacher teacher;

int noMeetings;

};
int main(int argc, char* pArgs[])

TutorPair tp;

cout << " rnain()\n";


return 0;


:
Student

Teacher

TutorPair

main()

tp main;) T u t o r P a i r .
T u t o r P a i r ,
- s t u d e n t t e a c h e r .
s t u d e n t ,
. Teacher.
T u t o r P a i r ,
.
, T u t o r P a i r
s t u d e n t t e a c h e r .
, .

qeanftiffctnofi
, .
,
. -
.



; .
, ,
. :
, ,
. ,
- .

182

III. ""


, ,
(-) (C++ : ,
"", .. ).
, .
Student :
class Student

public:

Stucent()

sernesterHours = 0;

gpa = 0.0;

-Student()

//

// ... ...
protected:
int
semesterHours;
fleet gpa;
};

, ,
C++, .
(" "),
. "
". , ,
, r e t u r n .
, .
, ,
. ,
-. T u t o r P a i r ,
.
#incluae <stdio.h>

#includes<lostream.h>
class Student

public:

Student()

cout << " Student\n";

semesterHours =
0;
gpa = 0.0;

-Studentf)

cout << " Student\n";

// ... ...

protected:

int: semesterHours ;

float gpa;

17. : 183

class Teacher

I
public:

Teacher()

cout " Teacher\n";

-Teacher()

cout << " Teacher\n";

class TutorPair

pub-1i :
TutorPair()

cout << " Tutor?air\n";

noMeeHings = 0;
-TutorPair(>

cout << " T u t o r P a i r W ;

protected:

Student s;
Teacher t;
i.nt noMeetings ;

};

i n t rnain ( i n t a r g c s , c h a r * pArgs [ ] )
TutorPair

tp;

cout << " main()\n";

return 0;

, :
Student

Teacher

TutorPai r

main()

TutorPair

Teacher

Student

, T u t o r P a i r t p ,
main .
.

184

III. ""

18


1 ...
S
S
S
/
^

. ,
\* Student .
, , .
, , ""
. ,
s t u dent. ,
.
C++ -,
.
.
, 17, " :
", , .. ,
. .

faucCHcufyutHbsccHaflfUfKMo/
//
C++ , :
#include <iostream.h>
#include <string.h>
class Student
{
public:
Student(char *pName)
{
out << " " << pName << "\n";
strncpy(name, pName, sizeof(name));
name[sizeof(name) 1] = '\0';
}
// ... ...
protected:
char name(40];
int

sernesterHours;

float gpa;

};

18.

185


,
, ,
. -, .

, ,
, . ,
, : .
,
,
. ,
( ) . -
, ,
.
, (C++ ,
, - ). BankAc
count ,
.
, ,
.


. , -
, , -, .
,
. ,
s s t u d e n t ,
Student (char* ). s main ().
#include <iostream.h>

#include <string.h>

class Student

public:

Student(char "pName)

cout << " " << pName << "\n";


s t r n c p y (name, pNarrLe, s i z e o f (name) ) ;
name[sizeof(name) 1]
= ' \ 0 ' ;

semesterHours = 0;

gpa = 0.0;

-Student()

cout << " " << name << "\n";

// ,

name[0] '\0';

// ... ...

protected:

char name[40];

int
semesterHours;

float gpa;

186

///. ""

int main(int argcs,

char* pArgs[])

Studen t s ( " D a n n y " ) ; //


r e t u r n 0;
)
/ /
,
17, " : ",
, pName, char*.
- ,
name, .
s main (). , ,
s .
s Danny. main()
Danny.
:
Danny
Danny
"
inline-".
( !).
:
#include <iostream.h>
#include <string.h>
class Student
{
public:
Student(char *pName)
-Student()
//... ...
protected:
char name[40];
int
semesterHours ;
floa'i
II (
// )
Student::Student(char *pName)
{
cout << " " pName << "\n";
strncpy(name, pName, sizeof(name)I;
name[sizeof(name) 1] = '\0';
semesterHours = 0;
gpa = 0 . 0 ;
}
// ,
Student::-Student()
cout << " " << name << "\n";
}
C++,
.

18.

187


-, :
.
" " ,
, .
, ,
6, " ".
C++ , ,
. , Student
, :
#include <lostream.h>

#include <string.h>

class Student

public:

Student()

out << " ";

semesterHours = 0 ;

gpa = . ;

name[0]
= '\0';
}

Student (char ^pNair.e)

out << " " < <" pNane << "\n";


strncpy (narr.e, pName, sizeo: (name) ) ;

name [ sizeof (name ) 1'. = ' \0 '


;
semesterKours = 0 ;

gpa = 0;
}

Student (char "*pName, int xfiEcurs, t'loat xfrGPA)

cout << "


" << p.Mame << "\n";
strncpy (name,
pName , sizeof (narr.e) ) ;
name [ si zeof (name )
1 ] = ' \Q ' ;

semesterHours
= xfurs;
gpa = xfrGPA;

-Student()

j
cout << " \";

// ... ...

protected:

char name[40];

int
semesterKours;

float gpa;

};

//
//
7
int main (int arges, char* pArqs[])

Student noName;

188

HI. ""

Student freshMan("Smell E. fish");


Student xfer("Upp R. Classman", 80, 2.5);
return 0;
}
noName ,
s t u d e n t : : s t u d e n t ,
. ( , ,
, .)
freshMan ,
char* ; xfe r .
, ( ) .
,
, .
#include <io5treairi. h>
#include <string.h>
class Student
{

public:
S t u d e n t ( c h a r *pName ="no name",
i n t xfrHours = 0,
f l o a t xfrGPA = 0.0)
{

cout << " " << pName << " \ n " ;


strncpy{name, pName, s i z e o f ( n a m e ) ) ;
name [sizeof(name) 1] = ' \ 0 ' ;
semesterHours = xfrHours;
qpa = xfrGPA;
}

-Student()
{

cout << " \";


}
// ... ...
protected:
char name[40];
int semesterHours ;
float gpa;
int main(int argcs, char* pArgs[])
f
Student noName;
Student freshMan("Smell E. Fish");
Student xfer{"Upp R. Classman", 80, 2.5);
return 0;
}
,
, f r e s h Man noName.
C++
, .
.
,
.

18.

189

fcofiaitftifkJH0fto6

no
, C++ .
, C++ ,
, . ,
, , C++
( ,
), - .
, C++ (
C++ , ,
).
:
, ,
.
.
.
class Student
{
//... , ,
};
int main(int arges, char* pArgs[])
{
Student noName;
return 0;
}
noName , C++
.
s t u d e n t C++ ,
- .
.
class Student
{
public:
Student(char *pName);
}
};

int main(int arges, char* pArgs


{
Student noName;
return 0;

To, s t u d e n t ( c h a r * ) , ,
C++
. GNU C++
( , :
):
Error: no matching function for call Student::Studentf)
(: ,
Student::Student())

190

III. ""

, s t u d e n t : : Student ().
, :
class Student

public:

Student(char *pName|;

Studentf);

};
int main(int argcs, char* pArgs[])

Student noName;

return 0;

, , C++
.

/&>&///#
- , ,
f l o a t i n t . ,
. ,
, ?
.
#include <iostream.h>

#include <string.h>

int nextStudentld
= 0;
class Studentld

public:

Studentld()

value = +rnextStudent!d;

coat << "


"

<< value << "\n";


}

protected:

int value;

class Student

public:

Student(char "pName =
"no name")
{

out << " " << pName << "\n


:;trncpy(name, pName, sizeof(name) ) ;

name [sizeof(name) - 1] =
'\0';

protected:

char
narr.e [40] ;
Studentld id;

int main!int argcs, char* pArgs[])

18.

191

Student s("Randy");
return 0;
}
s t u d e n t
. "" ,
nextStuden t 13.
Studen t id. Student ID . Student id,
Student St.uder.tlD.
s t u d e n t SLudentlo,
- , ,
.
Studenti D Student.
C++ , id
StudentiD : : StudentiD ().
Student, ,
. ( , ,
. Outline inline func
tions.) :
1
Randy
: Studenti D ,
Student.
( ,
, .
"".)
,
, C++, -
. .
- (, , ).
, . ,
? ?
: ,
, s t u d e n t , ,
, stuaen_iD.
, .
#include <iostream.h>
#include <string.h>
class Student Id
{
public:
Studentld(int id = 0)
{
value = id;
out << " id, "
<< value << "\n";
}
-Studentld(}
{
cout << " " << value << "\n";

192

III. ""

protected:

int value
class Student

public:

Student(char *pName
= "no name", int ssld = 0)
f

cout " " << pName << "\n";


s-rncpy(name, pName, sizeof(name));

name[sizeof(name} - 1] =
'\0';
/'
/ / -

S-udentld id(ssld);

protected:

char name[40] ;

StudentId id;

};

int main(int argcs, char* pArgs[])

Student s("Randy", 1234);

cout " main\n";

return 0;

Studer.tID ,
( ,
,
; ). s t u d e n t
(.. ) id
S t u i e n t l D .
,
, , ,
id, 0

Randy

id, 1234

1234

main
0

, s t u d e n t l D
: 1234.
, 1234
main{). , s t u d e n t l D
Student.
. id
Student.
id
s t u d e n t .
.
, ,
.
. C++ :

18.

193

Student(char *pName = "no name", int ssld = O):id(ssld)


{
cout " " << pNarae "\n";
strncpy{name, pName, sizeof(name));
name[sizeof{name] 1] = '\0';
}
protected:
char name[40];
Studentld id;
);
.
, .
. C++ :
" id s s l d . -,
, ".
, ,
.
id, 1234
Randy
main
1234
" : "
. , .
class SillyClass
{
public:
SillyClass(int& i) : ten(10), refl(i)
{
}
protected:
const int ten;
intS refl;
};
int main(int argcs, char* pArgs[])
{
int i;
SillyClass sc (i);
return 0;
}
S i l l y C l a s s - ten r e f l
.
. ,
.


s t u d e n t
:
Student noName;
Student freshMan("Smell E. Fish");
Student xfer("Upp R. Classman", 80, 2.5);
Student , noName, ,
. noName
?

194

, ""

,
:
Student noName();
, , .
noName,
Student, ,
s t u d e n t . !
,
; C++ . (- ,
, , ?..)
, ,
.
Student thislsAFunc(int);

Student thisIsAnObject(10);

, ,
; . C+ + ,
(
void
; ,
...).

/&^^
, ,
,
. ,
,
.
.
S ,
.
S .
S main().
S - .
/ ,
.
^ ,
.
,
,
. ,
- .
.

18.

195


,
- . (,
, , "'1
. ,
.)



, . ,
.
,
, C+ + ,
, .
.
#include <iostream.h>

#include <string.h>

class DoNothing

public:

DoNothing(int initial)

out << " DoNothing "

<< initial

<< "\n";

void fn(int i)

static DoNothing dn(i);

cout << " - fn i = " << i << "\n";

int main(int argcs, char* pArgs[]}

fn(10);
fn(20);
return 0;

:
DoNothing 10

- fn i = 10

- fn i = 2 0
, fn (} ,
DoNothing f n ().

1 ()
.
, ,
main {) .

196

III. ""

.
, main ,
.
, main()
, . C++
,
main() .
"",
, !
.
, ,
. ,
, ,
, . ,
, , .


.
. ,
. :

? ( , ,
.) , ,
, .
,
,
. ,
.

. ,
( , ).
.
// Student.H:

class Student

public:

Student (unsigned id) : studentld(id)

const unsigned Studentld;

I;
class Tutor

public:

Tutor (Students s)

tutoredld = s.studentld;

protected:

unsigned tutoredld;

};

// FILE1.CPP

//,

18.

197

Student randy(1234);

// FILE2.CPP

//

Tutor jennyfrandy);

s t u d e n t ,
Tutor ,
. randy, jenny.
, randy , jenny; -
. ,
. jenny ,
s t u d e n t , ,
randy .
.
, ,
. ,
.


,
. , .
.
class Student

public:

Student (unsigned id, unsigned age) : sAge(age), sld(id)

const unsigned sld;


const unsigned sAge;

};
s l d sAge,
. , ,
- ,
, - .

,
, . ( ,
C++ "", " " " " )

198

III. ""

19


& / ...
S
S
/ "" ""
</

Mt^ ,
V"* C++ ,
. 17, " :
", ,
18, " ",
. ,
( ).

, C++ ,
. ::(&) ( x : : X ( c o n s t X&>),
X . , ,
X. ,
, ,
"" C++.


, ,
:
void fn(Student fs)
{
//
}
int main(int arges, char* pArgs[])
{
Student ms;
fn(ms);
return 0;
}
f n ()
, .
, .
, (
). C++

19.

199

, , , ? .
? ( , ""
. !) ,
.
C++ .

,
, ms f n ().
S t u d e n t : : Student: (Students)
(- ...).


, ,
. Student.
#include <iostream.h>

#include <string.h>

class Student

public:

//

Student(char *pName = "no name", int ssld = 0)

cout << " "

<< pName
"\n";

strncpy (name, pNarne, sizeof (name) ) ;

name[sizeof(name) 1] = '\C';

id
= ssld;
//

Student(Students s)

cout "

s.name

<< "\n";

strcpy(name, " " ) ;

streat(name, s.name);

id = s.id;

Student{)

cout << ": " << name << "\n

protected:

charname[40];

int id;

// fn
void fn(Student s)

200

III. ""

cout << " fn(}\n";


}
int main 'int argcs, char* pArgs [ ] )
{
Student rar.dyC'Randy", 1234);
cout << " f n ( ) \ n " ;
fn (i cindy) ;

cout << " fn()\n";


return 0;
}
:
Randy
fn()
Randy
fn()
: Randy
fn()
: Randy

, .
; main{) ". . . ".
C++ randy
( f n () ).
f n (); randy
main () .
,
. (
, ,
, - , "
. . .'".
. ,

, , .)

*
.
, C++
. , C++
. ( ,
,
.)
, C++,
-. , C++,
. ,

( ),
. ,
.

19.

201

#include <iostream.h>
#include <string.h>
classStudent
public:
Student(char *pName = "no name")
cout << " "
pName "\n";
s t r n c p y (name, pNarne, s i z e o f (name) ) ;
n a m e [ s i z e o f ( n a m e ) 1] = T \ 0 ' ;
Student(Students s)
{
cout << " " << s.name << "\n'
strcpyfname, " ") ;
streat(name, s.name);
-Student( )
cout << " " << name << " W
protected:
char name[4 0 ] ;
class Tutor
{
public:
Tutor(Students s) : student(s)
//
// s tudent
(
cout << " \";
)
protected:
Student student;
};
void fn(Tutor tutor)
(
cout << " fn()\n";
}
int mainfint arges, char* pArgs[])
i
Student randy("Randy") ;
Tutor tutor(randy);
cout << " fn{}\n";
fn(tutor);
cout << " fn( ) \n" ;
return 0;
}
:

202

III. ""

Randy

Randy

fn()
Randy

fn()
Randy

fn(}
Randy

Randy

randy s t u d e n t ,
.
t u t o r Tutor ( S t u d e n t s ) .
T u t o r : : s t u d e n t ,
s t u d e n t . .
fn() t u t o r .
Tutor , t u t o r
, C++ . ,
, t u t o r . s t u d e n t
Student.

"" ""

. ?
- ,
-
?
,
, .
,
, ,
.
, .
.
#include <iostream.h>

#include <string.h>

class Pe rson

public:

Person(char *pN)
{

cout << " " << pN << "\n";

pMame = new char[strlen(pN) + 1];


if (pName != 0)

strcpy(pName, pN) ;

-Person()
{

cout << " " << pName << "\n";


II

19.

203

pName[O] '\0';
delete pName;
protected:
char *pName;

int main(int argcs, char* pArgs[])


I
Person pi("Randy");
Person p2 = pi;
return 0;

//

}
Person
, .
. p i ,
, 2.

. , 2
C++ ,
. , p i 2 ,
, .
, -
, . ?

. , C++,
, .
, 2
. .
p i , , .
, .
. 19.1. p i 2,
. , p i 2 (
). "" (shallow)
, .
1

pName

pName

pName

. 19.1. pi 2

204

III.""

,
.
, .
class Person

public:

//

//

Perscn{Persons p)

cout << " " .pName


<< " \";

pName = new char[strlen(p.pName) + 1];


if {pName != 0)

strcpy(pName, p.pName);

//...

...

,
(. 19,2).
"" (deep),
, (, , ,
, ).
1
pName

pName

2
pName

. 19.2. pi 2

:
Randy

Randy

Randy

Randy

19.

205

, ,
, , ,
(, ) .. ,
. , :
,
.

/&
,
. ,
. .
Student fn();
//

int main (int argcs, char* pArgs[])

Student s;

s = fn(};
// fn()
//

return 0;

f ) .
s, ?
C++ (
). ", , C++
, , ?" (
!) ,
,
s. , s ?
int main (int argcs, char* pArgs[])

Students refs - fn();


//.. . ?. . .

return 0;
}

,
r e f s !
,
.
Student f n l ;

int fn2(Students);

int main (int argcs, char* pArgs[])


{

int x;

// Student, fnl(),

// fn2().

// fn2() ,
//

// .

// ,

// fnl (),

3*fn2 (fnl () ) + ;

206

lit. ""

// , f n l ( ) ,
//
//... . . .
retur n 0;
}

, ,
, a re f S
.
,
- . , ?
.
.
, C++ ?
, , .
'.'
,
, .
, ,
c l a s s Studen t
{

protected:
Student(Students s){}
public:
//... ...
i;

, C++, ,
s t u d e n t ( -).

. .
class Student
{
public:
Student()
{
//..., ...
}
Student(Student s)
{
11. -, . . .
void fn(Student fs)
{
|
int main (int argcs, char" pArgs[])
{
Studentrns;
fn(ms};
return 0;
)
, student
s t u d e n t : : student (Student) ? !

19.

207


.
,
? ,
. , main() f n {), C++
s t u d e n t .
, ,
s t u d e n t .
, ,
s t u d e n t .
, ,
student...
.

208

III. ""

20


#...
S
^ -
S BUDGET2 .

*77

- .
* * , .
, , -,
, .
-, -,
. .

On/teqeAeHue
- ,
(static). - (
, -...).


.
(, ) , ,
, , ,
. ,
, ,
.
( )
. ,
s t u d e n t .
, , "" .
, ,
. , , , ,
,
,
(, , - ).
, ,
, , ,
. .
, ,
.
,
, .

20.

209


-
s t a t i c , .
class Student

public:

Student fchar *pName = "no name")

strcpyfname, pName);

noOfStudents++;

-Student()

noOfStudents;

int number()

return noOfStudents;

protected:

static int noOfStudents;

char name[40];

};
Student si;
Student s2;

noOfStudents s t u d e n t ,
s i s2. , s t u d e n t
name noOfStudents,
s t u d e n t .
", , noOfStudents
s t u d e n t , ?" :
. , .
int Student::noOfStudents - 0;


. - (
).
, ,
, .

-
- ,
. ,
. ,
, .
class Student

public:

Student()

210

///. ""

noOfStudents++; //
/'/. . . . . .
static int noOfStudents;
//... , ...
void fn(Student&

si,

Student s2)

//
cout " "
<< si.noOfStudents
//
<< " \ " ;
//
}

fn () noOfStudents
s i . , s i s2 noOfStudents,
: s i ? s2?
. ,
, , :
//... , ...
void fn:Student& si, Student s2)
1
//
cout << " "
<< si.noOfStudents "\n";
cout << " "
<< s2.noOfStudents << "\n";
!
, :
/ / . . . , ...
void fn;student& s i , Student s2)
//
cout << " "
<< Student::noOfStudents
<< " \ n " ;

, , C++
.
, ,
,
. , ,
.
class Student
static int noOfStudents;
Students nextStudent ();
//... , ...
void fn(Student& s)
cout s.nextStudent().noOfStudents << "\n";

20.

211

- n e x t s t u d e n t (> . ,
C++ noof s t u d e n t s , ,
. "
16
, .

-

-, . -,
,
. , s t u d e n t
,
.

S t u d e n t . ,

, ( ),
17
.

. , Radio

tune, .
, tun e . ,

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

&
-
- .
-, , -
. , -,
-, . ,
.
, - number
.
#include <iostream.h>
#include <string.h>

, . , GNU C++
, Borland C++ Watcom C++ .
. .

17

: ,
, .
. .

212

III. ""

class Student

public:

static int number{)

return noOfStudents;

//. .. , ...

protected:

static int noOfStudents;

char name[40];
};

int Student::noOfStudents = 0;
int main(int argcs, char* pArgs[])

Student s;

cou- << s.number!) << "\n";

coun << Student::number() << "\n";


return 0;

, -
-. - -
, . ,
.
class Student

public:

//

static char *sName()

return name;
// ?

//... , ...

protected:

char name[40];

static int noOfStudents;


};
, -
-. :
#include <iostream.h>

#include <string.h>

class Student

public:

// , ,
//

lit ( ,

// ...

Student (char *pName);

-Student () ;

//findName
static Student* findName(char *pName);

protected:

20.

213

static Student *pFirst;

Student *pNext;

char name[40];

};
Student* Student::pFirst = 0;

//findName

//

//
,

//
.

Student* Student::findName(char *pName)

// ...

for (Student *pS = pFirst; pS; pS = pS->pNext)

// ...

if (strcmp(pS->name, pName)
== 0)
{

//,..

return pS;

// { )

return (Student*)0;

int main(int argcs, char* pArgs[])

Student si("Randy");

Student s2("Jenny");

Student s3("Kinsey");

Student *pS = Student::findName("Jenny");

return 0;

findName () p F i r s t ,
. findName s t u d e n t ,
name , ,
. -
, .
int main(int argcs, char* pArgs[]}

Student si("Randy"};

Student s2("Jenny");

Student s3("Kinsey");

Student *pS = si.findName("Jenny");

return 0;

s i findName ;
, , findName ()
.
- ,
, . , -
Duck: : f l y () ( : : ( ) ) ,
"" Duck: : g o E x t i n c t (> ( { ) : : ( ) )
(: ).

214

III. ""

this
, t h i s ,
, t h i s
-. , .
- t h i s ,
.
class SC
{
public:
void nFn(int a ) ;
// , SC::nFn(SC * t h i s , int a)
s t a t i c void sFnfint a ) ;
// , SC::sFn(int a)
};
v o i d fn(SC s)
f
s.nFn(lO);
// S C : : n F n ( s s , 10);
s.sFn(lO);
// S C : : s F n ( 1 0 ) ;
}

, nFn () ,

void SC::nFn(SC * t h i s , i n t ) . nFn f)

s ( ,

).

, SC: :sFn

t h i s .

SC: : sFn () t h i s ,

. , -

- BUDGET2 .
BUDGET,
. ,
"" , -
, .
,
.
( ) ( ). (
, ,
.) ,
, .
:
% S 20 ,
500 ;
:~
f S 20 , 500 .

20.

215

:
".. / ;
S 5.00 .
, ,
Checking Savings. -
, ,
.
, checking Savings ,
( ,
). , d e p o s i t ()
()
withdrawal () ().
, -,
d i s p l a y (); .
, ,
, (

, , , ).
.
// BUDGET2.CPP , .

#include <iostream.h>

#include <stdio.h>

//

const int maxAccounts = 10;


// Checking

class Checking

public:

Checking(int initializeAN = 0)

accountNumber = initializeAN;

balance = 0.0;

//

int accountNo()

return accountNumber;

double acntBalance()

return balance;

//

void deposit(double amount)

balance += amount;

void withdrawal(double amount);

// cout

void display()
{

cout << " " << accountNumber

216

III. ""

<< " = "


"\n";

<< balance

protected:
unsigned accountNumber;
double balance;
};
// withdrawal -
//
inline-
void Checking::withdrawal(double amount)
{
if (balance < amount)
{
out << " :
<< balance
<< "( " << amount
<< "\";
}
else
{
balance -= amount;
// ...
if (balance < 500.00)
{
II ...
balance -= 0.20;

// Savings
class Savings
{
public:
Savings(int initialAN = 0)
1
accountNumber = initialAN;
balance = 0.0;
noWithdrawals = 0;
//
int accountNo()
return accountNumber;
double acntBalance()
return balance;
//
void deposit(double amount)
balance += amount;
void withdrawal(double amount);

20.

217

// display 'cout
void display()
{
cout << " " << countNumber
<< = " << balance
<< " ( = "
noWithdrawals
<< ")\n";
}
protected:
unsigned accountNumber;
double
balance;
int
noWithdrawals;
};
void Savings::withdrawal(double amount)
{
if (balance < amount}
{
cout << " : "
<< " " << balance
<< ", " << amount
<< "\";

// ...
if (++noWithdrawals > 1)
{
// ... S5
balance -= 5.00;
//
balance -= amount;

//
void process(Checking* pChecking);
void process(Savings* pSavings);
//
Checking* chkAcnts[maxAccounts];
Savings* svgAcnts[maxAccounts];
// main
int main(int argcs, char* pArgs[])
{
1
// 'X ''
int nChkAccounts = 0; //
int noSvgAccounts = 0;
char accountTyoe;
// 'S' ''
while (1)
{
cout << " S , "
<< " , "
<< "X :";
cin >> accountType;
// , X

218

III. ""

if (accountType == 'x' II accountType == 'X')


i
break;;
}
// E
switch (accountType)
{
//
case '':
case ' :
if {noChkAccounts < maxAccounts)
{
int acnt;
cout << " :";
cin >> acnt;
chkAcnts[noChkAccounts] = new Checking(acnt);
process(chkAcnts[noChkAccounts]);
noChkAccounts++;
}
else
{
cout << " \";
}
break;
//
case 's':
case 'S':
if (noSvgAccounts < maxAccounts)
{
int acnt;
cout << " :";
cin >> acnt;
svgAcnts[noSvgAccounts] = new Savings(acnt);
process(svgAcnts[noSvgAccounts]);
noSvgAccounts++;
}
else
{
cout << " \
}
break;
default:
cout << " ...\";

/ /
double chkTotal = 0;
cout << " :\";
for (int i = 0; i < noChkAccounts; i++)
{
chkAcnts[i]->display();
chkTotal += chkAcnts[i]->acntBalance
double svgTotal = 0;
cout << " :\n";

20.

219

for (int j = 0 ; j < noSvgAccounts; j-t-r)


i
svgAcnts[j]->display{);
svaTotal += svaAcnts [~j ->acr.tBalance() ;
double total = chkTotal + svgTotal;
out << " = "
chkTotal
<< "\n";
out << " = "
<< svgTotal
<< "\n";
out << "
<< total
<< "\n";
return 0;

= "

// (Checking)
void process (Checking* pCheckir.g)
out << " ,\"
<< " , 0 \'
double transaction;
do
{
cout << " : ";
in >> transaction;
//
if (transaction > 0}
{
pChecking->deposit(transaction);
//
if (transaction < 0)
{
pChecking->withdrawal(-transaction);
) while (transaction J = 0);
// (Savings)
void process(Savings* pSavings)
{
cout << " ,\"
<< " , 0 \";
double transact!on;
do
{
cout << ":";
cin >> transaction;
//
if (transaction > 0)

220

III. ""

pSavings->deposit(transaction);
/ /
if (transaction < 0)
pSavings->withdrawai(-transaction) ;
} while (transaction != 0) ;
}

,
, ( - ).
,
.
8 ,
, X :S
:123
,
, 0
:200
:-20
:0
;= ,
, X :S
:234
,
, 0
:200

:0
3 ,
, X :
:345
,
, 0
:200
:-20
:0
3 ,
, X :
:456
,
, 0
:600
:-20
:0
S ,
, X :
:
345 = 9 . 8
456 = 580
:
123 = 180 ( = 1)
234 = 175 ( = 2)
= 7 59.8

20.

221

= 355

= 1114.8
-, checking.
. "= 0"
, .
Checking cl = new Checking(124};
Checking c2 = new Checking();
c l checking , 123,
2 , .
accountNof) acntBalanc e ()
ccountNurr.be balance .
,
. , , ,

.
d e p o s i t () withdrawa l ()
. d e p o s i t () ,
inline-. withdrawal (), ,
, .
d i s p l a y (} .
Savings, , checking,
noWithdrawals,
.

svgAcnts chkAcnts .
maxAccounts.
main () Budget!,
.
"" main () switch,
: S . switc h
: -, ,
; -, d e f a u l t (
) .
, main
, .
, Checking Savings
main () . , , main () ,
, ,
.
process (), ,
- deposi t () withdrawal (),
. , , , process ()
. .
.
, .
, , Budget2
, Budget 1. Savings,
, (
Checking). main (), .
: Savings
Checking ,
. 4, "",
Budget3.

222

III. ""

IV

* ...
-
3 ,
,
-.
.

.
( ), ,
.
, ,
( ).
,
,
:
.

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

21


* ...
/
S
S

* (inheritance), ..
^ ' .
. (
...). ,
( ), (,
), , , .
. ,
,
, , , .
,
, .
. , : " ?",
: " , ". ,
( , )
. , , ,
"".
-
. ,
,
( ), , ,
.
C++ :
class Student

class GraduateStudent : public Student

I
};

GraduateStudent Student.
, GraduateStudent . , GraduateStu
dent , .


C++ . ,

( ).

21.

225

. , Student
GraduateStudent.
, . ,
GraduateStudent, ,
.

. - ,
,
.

. ,
?
, ,
. ,
, ,
.
,
. ,
,
. , ,
,
.

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

, .
,
.
. ,
. ,
, .
, ,
.
,
.


, ,
. ,
, , ..
.
, ,
(, ). ,
* ( , ). ,
,
. , .

226

IV.

C++ , s t u d e n t Person. ,
, Person s t u d e n t . ,
, s t u d e n t Person (
; ).
C++ -
.
, studen t Person, . Person
Student ( ,
, , ).
, Person student .
, studen t , a Person .
. ,
GraduateStuden t s t u d e n t ,
Person. , : GraduateStuden t
Student Studen t Person, GraduateStuden t
Person.

ftcuc

GraduateStudent ,
.
#include <string.h>
class Acvisor

classStudent
{
public:
Student{char *pNarr.e = "no name")
{
strncpy(name, pName,
average = 0 . 0 ;
semesterHour = 0;

sizeof(name));

v
ddCourse(inthours,floatgrade)
{oida
a
v
e
r
a
g
e=(
seme=h
ster
H
orus
r;
s*average+grade};
s
e
m
e
s
t
e
r
H
o
u
r
s+
o
u
} average=average/semesterHours;
i
our
fn
lnh
oatg
ps
a()
{){r
{re
et
tu
ur
rns
nae
vr
ea
re
as
gtee;riHours;}
protc
eh
ca
trn
ed:ame[40];
i
n
ts
eme
s
t
e
r
H
o
urs;
f
l
o
a
ta
v
e
r
a
g
e
;
);
classGraduateStudent:publicStudent
21.

227

public:
int qualifier( > { return qualifierGrade;}
protected:
Advisor advisor;
int qualifierGrade;
int main{ )
{
Student llu("Lo Lee Undergrad");
Graduatestudent gs;
llu.addCourse(3, 2.5);
gs.addCourse (3, 3.0);
return 0;
}
Studen t , . l i u
Student. GraduateStuden t
Student ; p u b l i c s t u d e n t
GraduateStuden t Student.
p u b l i c ,
, (
?).
gs S m a e n t , .
-
name,
senesterHcurs,
average
\-
addCourse (). , GraduateStuder.t q u a l i f i e r (),
a d v i s o r q u a l i f ierGrade- , gs
Student - .
.
void fn(Students s)
{
// ..., ...

intmain()
{
GraduateStudent gs;
fn(gs);
return0;
}
, f n i )
s t u d e n t . main ()
G r a d u a t e S t u d e n t . ,
( - , !) "GraduateStudsn t Student" .
- Student
GraduateStudent , .
i n t main( )
[
GraduateStudent

gs;

// Student::addCourse()
gs.addCourse (3, 2.5) ;
return 0;

228

IV.


, ,
, ,
. .
,
GraduateStudent ,
Studen t ( ). s t u d e n t
, Person,
s t u d e n t . ,
, ""
, .
-,
. ,
- ( ).
class GraduateStudent : public Student
{
public:
GraduateStudent(char *,
Advisors adv) : Student(pName),
advisor(adv)
{
qualifier-Grade = 0;
}
// ,
};
void fn!Advisors advisor)
{
GradaateStudent gsC'Yen Kay Doodle", a d v i s o r ) ;
// . . . , ...
}

GraduateStuden t Stu dent, pName.


-, , Studen t
Advisor. Advisor ( ad
v i s o r ) GraduateStudent .
, ,
, GraduateStudent .
! , Advisor,
Student . s t u d e n t Person,
Student .
. s t u d e n t ,
GraduateStuden t .
.

, GraduateStuden t
Student Advisor, -. -
Advisor, , Studen t -
Advisor, , GraduateStuden t Advi

21.

229

sor. , GraduateStuden t Advisor.


?
.
, , ,
. , .
, (, ,
, ).

, ( ),
.
, , ,
, ,
, .
.
:
class Vehicle
class Motor
f
};
class Car : public Vehicle
{
public:
Motor motor;
};
void VehicleFn(Vehicles v);
void raotorFn(Motors m);
in t main()
!
Car c;
VehicleFn();
//
motorFn(c);
//
motorFn(.motor);
//
return 0;
}
VehicleFn () , Vehicle.
motorFn () , Motor, Motor.
,
, : motorFn ( .motor) .

230

IV.

22


-:

& ...
S
/
S
</
S

^ ,
\* , .
( ):
void son'5Fn{int)
void ' (char*)
void som- (char* ,

..

double)

someFnO .
: someFn(int) someFn (char*)
+- , ,
.
,

, .

, - .
- .
. ,
, - ?
:
c l a s s S t utient
(
public:
// . . . , . . .
floc.t c a l c T u i t i o n ( ) ;

class G::aduateStudent

public Student

public:
float calcTuition ( ) ;

};

22. -:

int main (int argcs, char* pArgs[])


{
Student s;
GraduateStudent gs;
s.calcTuition();
// Student::calcTuition()
gs.calcTuition (); //. . .
//GraduateStudent::calcTuition()
return 0;
}
,
calcTuitio n (), C++ , c a l c T u i t i o - ()
. , .
, , ,
, , .

s.calcTuition()

Student::calcTuition;),

s S t u d e n t , g s . c a l c T u i t i o n ()
G r a d u a t e S t u d e n t : : c a l c T u i t i o n { ).
, ?
,
:
class Student
!
public:
// ,
float calcTuition()
{
return 0;

classGraduateStudent:publicStudent
{
public:
floatcalcTuition()
(
return0;
void fn(Students x)
{
x.calcTuition();

// calcTuition()
// ?

}
int main{int arges, char* pArgs[]}
{
Student s;
GraduateStudentgs;
fn{s);
fn(gs);
return 0;
)
c a l c T u i t i o n ()
f (). ,
fn () , Student, Graduate-Student.
GraduateSudent Student!

232

IV.

, ,
. , 21, " ".
, fn () .
Student. ,
C++ fn () Student.
Studer.t fn ()
, fn (Students) f n ( s t u d e n t * )
. , , 15,
" ".

. c a l c T u i t i o n {>

Student: : c a l c T u i t i o n (),

Student,
GraduateSudent::calcTuition(),

G r a d u a t e S t u d e n t . C++ "",
! ? .
,
. ,
C++ ( GNU C++ , , Visual C++)
,
, .
fn()
. Student,
GraduateStudent. ,
( " "). fn()
, C++ ,
: Student GraduateStudent.
, ,
.
Student,
f (). , ,
. f n ()
Student, fn ()
s, GraduateStudent, f n () gs. ?
,
,
.
(.. ) (),
- . C++ . (
. ,
C++!) ,
.

.
,
, C++
. .
(overriding).
,
.

22. -:


( ),
- . ,
, ,
.
, ,
, -. ,
, Ada.
.
, , . ,
, ,
s t u d e n t .
, ,
. (
"",
Microsoft... .)
,
, , -
(, , !).
, -
someFunction () - c a l c T u i t i o n () (
...).
void someFunction(Student s s)
//..., ...
s.calcTuition{);
//... ..
C++ ,
someFunction {) , ,
GradusteStudent .
define STUDENT 1
#define GRADUATESTUDENT 2
void someFunction{Students s)
i
//..., ...
// ,
//
switch (s.type)
case STUDENT:
s.Student::calcTuition();
break;
case GRADUATESTUDENT:
s.Graduatestudent::calcTuition{);
break;
//... .. .

234

IV.

type.
type = STUDENT type = GRADUATESTUDENT
GraduateStudent. type
s. ,
, ,
.
, . -,
. ,
c a l c T u i t i o n ()
, . ,
, .
-, ( ) ,
, .
, .
.
?..
, ,
, (, ,
).

( ?), .
, ,
( : ...).
, !
, ,
.
, .
.

. , ? ,
. :
. : , ,
. ( ) , ( ,
) , (
) , (
Oven) (), ,
(Microvawe). , :
, ,
.

/
C++ ; , ,
, , C++ .
, .
( ) . - C++
, , C++
,
.
.
- , C++
v i r t u a l , .

22. -:

#include <iostream.h>
class Base
public:
virtual void fn()
f
cout << " Base\n";
}
};
class Subclass : public Base
public:
virtual void fn()
{
cout << " Subclass\n";

void test(Bases b)
b.fn ();

//

)
int mainfint argcs, char* pArgs[])
Base be;
Subclass sc;
cout << " test(be)\n";
test(be);
cout << " test(sc)\n";
test(sc);
return 0;
v i r t u a l C++ , f n ()
-. f n (} ,
, -
, f n () .
fr, ()
t e s t () . t e s t () , b . f n O
Base: : f n (). t e s t ()
Subclass , Subclas s : : () .
:
test(be)
Base
test(sc)
Subclass
C++,
.

.
.
,
.

236

IV,

/
-
, ,
. .
#include <dos.h>
c l a s s Stuff !) {};

// sleep{}

class Nachos : public Stuff {};

//

//
class Oven
{
public:
virtual void cook(Nachos& nachos);
//
void turnOn();
//
void turnOff();
//
void insert(Stuff& s) ; //
void remove(Stuff& s ) ; //
protected:
float temp;
};
void Oven::cook(Nachos& nachos)
{
//pa ( )
// 3 50)
turnDn();
while (temp<350) {}
// 15
insert(nachos);
sleep(15*60);
//
remove(nachos);
turrOff();
}
class Microwave : public Oven
{
public:
virtual void cook(Nachosk nachos);
void rotateStuff(Stuff& s);
};
void Microwave::cook(Nachos& nachos)
{
//
// ,
insert(nachos);
turnOn(;;
/ /~

// ( ^ )
sleepOO);
// 30
rotateStuff(nachos);
sleep(30);
// 30

22. -:

// ,
turnOff();
remove(nachos);
Nachos makeNachos(Oven& oven)
f
//
Nachos n;
//
// ,
oven.cook{n);
// ...
return n;
}
Nachcs, s t u f f ().
Oven turnOn ( ) , t u r n o f f () , i n s e r t ()
remove () (
). , Oven - cook (Nachos& ),
.
cook (Nachos&) ,
Microwave, Oven, -.
Oven: : cook (Nachos&) 350,
15 .
. Oven ,
Microwave: : cook (Nachoss) , 30
, 30 ,
.
, , .
makeNachos () Oven . oven (),
, oven, cook ().
Oven : : cook () Microwave : : cook () ,
. makeNachos (}
, .
? -,
, , .
,
.
-, . ,
, -
makeNachos!) ( ,
,
!). , ConvectionOver.
- ConvectionOven: : cook (Nachos&),
makeNachos {), .
.
, ,
, !

238

IV.


tfufuni/
,
, , .
C++ ,
.
,
- ,
. -
, , ,
. ,
, ,
.
#include <iostream.h>
class Base
{
public:
virtual void fn(int x)
{
cout << " Base, int x = " << x << "\n";

c
lassSubclass:publicBase
{
publv
ii
cr
:tualvoidfn(floatx)
{

cout " float x = " x "\n";

void test(Bases b)
(
int i = 1;
b. fn (i);
//
float f = 2.OF;
b.fn(f);
//
int main(int argcs, char* pArgs[])
{
Ease be;
Subclass sc;
cout << " test(be)\n";
test;(be);
cout << " test(sc)\n";
test(sc);
return 0;

22. -:

,
f n () Base f n ( i n t ) , Subclass
fn ( f l o a t ) . ,
.
:
test(be)

Base, int = 2

Base, int = 2
test(sc)

Base, int x = 1

3ase,
int = 2
i n t , ,
f n ( i n t ) be, sc. ,
f l o a t i n t t e s t (}
Base: : fn () . , ,
t e s t (), .
.() B t e s t { ) Base: : fn ( i n t ) .
,
.
,
, -
, -
. ,
.
class Base

public:

//

Base* makeACopy()

//... ,

class Subclass : public Base


1
public:

//

Subclass* makeACopy{)

//... ,

};

void fn (BaseClassS be)


{

BaseClass* pCopy = be.makeACopy(};


// . . .

:
Subclass,
Base: imakeCopy{) .

240

IV.

.
-, - .
- ,
, , .
-,
,
.
, B a s e : : f n ( ] ,
, , fn () .
void test (Bases b)

b.base::fn f);

//

, .
, .
, -
.
, ,
-
. , ,
.
.
, .
, ,
, , , :
class Base

public:

-Base();

);
class Subclass : public Base

(
public:

-Subclass();

void finishWithObject{Base* pHeapObject)

delete pHeapObject; // -Basef)

//

// pHeapObject

, f inishWithObject () ,
Subclass, Subclass :
, . ,
, .
?
: .
, C++

22. -:

!
,
, .
.
,
( !): ,
(, ),

. ,
!

242

IV.

23


- ...
J
/
/ : BUDGET3.CPP

.
; ,
.
, .
, .

. :
. .
. ,
.
, , ,
. , ,
,
.
, ,
(factoring)
( , ;
, : 12 2,
2 3...).

, ,
Checking Savings, BUDGET,
. , ,
, -
. Checking Savings . 23.1.
, .
; S . .
| S -.
% S -.
'- / , ,
\
, ; ,
1
. , I
, .
" S .
/ .

23.

243

Savings

Checking
Iwithdrawal!)
1
1

deposit!)

. . - " ^ pRrst
accountNoQ
p N e x t
| first)
j count
accountNumber

withdrawal!):
deposit!)
accountNo{)J pNext
~]count
i ,
-jaccountNumber
next(}
noAccountsfJ DoWtihdrawasl

. 23.1. Checking Saving



.

. 23.1 , checking Savings


wit-hdrawal (}
.
,

-
d e p o s i t (). , , ,
(
). .
, . Savings
, Checking, Savings
Checking. . 23.2. Savings
Checking. , noWithdrawal
w i t h d r a w a l ! ) . ,

(
,
,
Checking
).

Savings

|wfttidrawal!)
=
Checking ,
deposit;)

|accountNo() pi-ret
,
pNext
count
Ifirctf)
.
accountNumber 1

next()
, s a v i n g s
noAccountsO
checking.
" ? .

". , , ,
Savings

.
| withdrawal!)]noWittidrawals
,
.
,
Puc. 23.2. Savings
"",
Checking
, ,

244

IV.

.
.
, . ,
, .
,
, .
checking. ,
, checking,
. rninimumBalance.
. Savings Checking, ,
Savings . ,
.
. ,
miniraumBalance. ,
.
. ,
-. Savings
,
Checking. , Checking
Savings .
? Checking Savings,
. ( Account),
, Checking Savings. . 23.3.

Account
[withdrawal)
jdeposit!)
pfirst
1accountNoQ" pNext
count
accountNumber
balance
|noAccountsj)"

Checking
withdrawal!) j minimumBalance

Savings
withdrawal!) noWrthdrawals

. 23.3. Checking Savings, Account


Account ?
, (
). ( , )
-, .
.
, Savings Checking
( ).
, Account,

23.

245

.
, Checking,
Savings.

. - ,
, ,
. : ,
...
, ,
, .
Mouse J o y s t i c k ""
.
. Mouse Display
.
( )
. , , "" ,
, . 23.4.
, Checking Savings
Account . Conventional
. ,
, .

( , , .
,
).
,
.
. , . 23.4,
, (. 23.5).
,
, .

, ,
. ,
Account. ,
Account.
- Account ,
. Account .withdrawal ()
.
. Savings : :withdrawal () ,
Checking: : withdrawal ( ) . Account: : withdrawa l () ?
.
:
" ?" .
" , ?"
.
" , , !"
...

246

IV.

Account

Conventional
Savings

Timed

Market

CD

Checking

K501

Stock

Mutual Funds

SpecialChecking
Puc. 23.4.

Account

RemotefyAccessible
Stock

LocallyAccesable

Checking
SpecialChecking

Market

CD

MutualFunds

Savings
K501

Puc. 23.5.
, . ,
" ". ( )
. ,
. ,
, w i t h d r a w a l ()
( , ,
" ").
, .
, .
, ,
,
, "".
, , .
- .
, "".
, ,
- .
,
. .
, ,
; , -
.
, C++
.

23.

247


.
, ...
, -
( . , , , ).
, w i t h
drawal () Account. , ,
. C++ w i t h d r a w a l ( ) ,
,
, , .
18 ( ,
).

Account.
//Account
class

Account

protected:

Account(Accounts ) ;

public:

Account(unsigned accNo, float initialBalance = 0.0F);

unsigned int accountNo{);

float acntBalance{};

static Account *first(};

Account 'next();

static int noAccounts();

//

void deposit();

//

virtual void withdrawal(float amount) =


0;
protected:

// ,

//
static Account *pFirst;

Account *pNext;

static int count;


//

unsigned accountNumber;

float balance;

I;
withdrawal (} = 0 ,
.
,
. Account ,
.

, ,
. . .

248

IV.

,
, .
, .
,
. :
.
; ,
. ,
.
void fn()

Account acnt(1234,100.00);
acnt.withdrawal(50);

//

//, -,

// ?

,
, . ,
? ,
Account: :withdrawal () .
. Account
.
, Account,
.


,
. Savings ,
withdrawal ()
. Savings ,
withdrawal () . checking:
, withdrawal ()
, .
, , .
.
class Display

public:

virtual void initialize () =


0;
virtual void writefchar *pString) =
0;

classSVGA:publicDisplay

// - "

virtual void initialize!);

virtual void write(char *pString);

class H W G A : public Display

//

23.

249

virtual void write(char *pString);


classThreedVGA:publicHWVGA
virtual void initialized;
};
voidfn()
SVGAme;
VGAvga;
//
S
Display, ,
: i n i t i a l i z e () w r i t e f ] .
.
-.
SVGA . ,
, . , SVGA
i n i t i a l i z e d w r i t e f ) ,
.
HWVGA . ,
VGA-. Displa y
, ThreedVGA, 3-D,
.
,
VGA ( , ,
). , HWVGA,
w r i t e () ( HWVGA ).
i n i t i a l i z e () ,
HWVGA -.
, w r i t e () HWVGA,
, i n i t i a l i z e ()
.
ThreedVGA HWVGA,
, i n i t i a l i z e {),
. , f n ()
ThreedVGA.

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

250

IV.


,
.
, , ,
. :
void fn(Account *pAccount);
//

void otherFn ()

Savings s;

Checking c;

//Savings Account

fn(&);
//Checking

fn(sc);

pAccount Account. ,
- ,
Checking Savings.
, fn ( ) , Checking,
Savings ( Account).
, Account,
.


w i t h d r a w a l ; ) ,
? Savings Checking,
, Account? -
. C++
, .
,
, .
,
.
,
Account:
class Account

// , ,

// withdrawal()

class Savings : public Account

public:

virtual void withdrawal(float amnt);

void fn(Account *pAcc;

//

23.

251

pAcc->withdrawal(100.OOf);
// ,
// withdrawal()
// Account
int main f)
!
Savings s;
//
fnf&s);
//
}
, s.
f n (},
withdrawal (). , withdrawal () Account,
.
, ,
.
: raain() f n () s. fnf) ,
, withdrawal () , ,
w i t h d r a w a l ! ) .
,
.
. , - , ,
withdrawal ,
, , . ,
.
, .
Account:
class Account
{
// , ,
// withdrawal()
virtual void withdrawal(float amnt) = 0;
};
class Savings : public Account
{
public:
virtual void withdrawal(float amnt);
};
void fn(Account *pAcc)
i
//
//
pAcc->withdrawal(100.OOf);
int main()
{
Savings s;
//
fn(&s);
//

252

IV.

, Account - withdrawal {).


, , pAcc->withdrawal (),
Account::withdrawa l () . .
. , . ( ,
, .)

, , ,
. ,
.

.??


Budget 1, 2,
- Budget2,
3. -
Budget3.
Budget
.
.
,
. Budget2 Budget3 Checking ()
Savings () .
, 500 ,

, .
Budget2 3udgetl :
, , ,
. , Budget2
Savings Checking, ,
.
Budget3 :
,
, ;
-
;
,
;

, .
"" -
,
, Account
.
.

23.

,

,

253

// BUDGET3.CPP
//
.
//
,
//
Budget2,
//

//
.
//

//
( ,
//
).
//
//
, ,
//
,
//
.
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
class LinkedListObj ect
{
public:
LinkedListObject()
f
//
//
addToEnd();
//
static LinkedListObject* first()
{
return pFirst;
}
LinkedListObject* next()
{
return pNext;
i
void addToEnd() ;
protected:
// ,
//
//
static LinkedListObj ect J pFirst,:
LinkedListObject* pNext;
//
//
LinkedListObject* LinkedListObject::pFirst = 0;
//
void LinkedListObject::addToEnd()
// this
// count

254

IV.

if (pFirst == 0)
{
// ;
pFirst = this;
}
else
{
//
LinkedListObject* pA;
for (pA = pFirst; pA->pNext; pA = pA->pNext) {}
,' /
pA->pNext = this;
)
pNext = 0;
//

// Account
//
: ,
//
.
//
withdrawal() - ,
//

class Account : public LinkedListObject
{
public:
Account::Account(unsigned accNo,
double initialBalance = 0.0)
{
// -
;CcountNumber = accNo;
balance = initialBalance;
//
count++;
//
int accountNo()
{
return accountNumber;
}
double acntBalance()
{
return balance;
}
stati int noAccounts{)
{
return count;
// ,
// .
//
static Account* first(]
{
return (Account*)LinkedListObject::first

23.

255

Account* next()
return (Account*)LinkedListObject::next()
}
//
void deposit(double amount)
balance += amount;
virtual void withdrawal(double amount) = 0;
//
void display()
cout << type()
<< " " << accountNumber
<< " = "
<< balance
"\n";
virtual char* type() = 0;
protected:
static int count;
//
unsigned accountNumber;
double
balance;
//
int Account::count = 0;
// Checking
//
. ?
class Checking : public Account
{
public:
Checking::Checking(unsigned sccNo,
double initialBalance = 0.0) :
Account(accNo, initialBalance)

//
virtual void withdrawal(double amount);
char* type()
f
return "";

// withdrawal -
//
Account::withdrawal() ,
//
20
//
$500
void Checking::withdrawal(double amount)
{
if (balance < amount )

256

IV.

:out << "


" " << balance
<< ", " << amount
<< "\";

balance -= amount;
.'/ ,
.'/
..1 (balance < 500.00)
balance -= 0.20;

// Savinqs , Checking,
//
class Savings : public Account
{
public:
Savings::Savings(unsigned accNo,
double initialBalance = 0.0)
Account(accNo, initialBalance}
{
noWithdrawais = 0;
i
//
virtual void withdrawal(double amount);
char' type()
{
return "";
protected:
int noWithdrawals;
// withdrawal - -
//
Ac:ount::withdrawal() ,
//

//
$5
void Sav Lr.gs : : withdrawal (double amount)
{
if (balance < amount)
{
;out << "
" " << balance
<< ", " << amount
<< "\";
}
else

23.

257

if (++noWithdrawals > 1)
{
balance -= 5.00;
}
balance -= amount;

//
unsigned getAccntNo(};
void
process(Account* pAccount);
void
getAccounts();
void
displayResults(>;
// main
int main(int argcs, char* pArgs[])
{
// ,
getAccounts();
//
displayResults(>;
return 0;
// getAccounts
void getAccounts(>
{
Account* pA;
// , "" "X"
char
accountType;
// S
while (1)
{
cout " S ,\"
<< " , X : ";
cin >> accountType;
switch (accountType)
{
case '':
case ' :
pA = new Checking(getAccntNo(In
break;
case 's':
case 'S':
pA = new Savings(getAccntNo()) ;
break;
case 'x':
case 'X 1 :
return;
default:
cout << " .\";
}
//
process(pA);

258

IV.

// displayResuits ,
//

void displayResults()
(
//
double total = 0 . 0 ;
coir: << " :\";
for (Account* pA = Account::first();
; pA = pA->next())
{
pA->display();
total += pA->acntBalance();
}
cou~ << " = " << total << "\n";
// getAecntNo
unsigned getAecntNo()
{
uns igned acentNo;
cou~ << " : ";
cin >> accntNo;
return accntNo;
// process(Account)
void process(Account* pAccount)
{
coin << " ' , \"
<< " , "
"0 ";
double transaction;
do {
cout << ":";
cin >> transaction;
//
if (transaction > 0)
{
pAccount->deposit(transaction);
}
//
if (transaction < 0) {
pAccount->withdrawal(-transaction);
}
} while (transaction != 0) ;
}
,
, {, ,
). ,
.
S ,
, X : S

23.

259

: 123
,
, 0
:200
:-20
:0
S ,
, X : s
: 234
,
, 0
:200
:0
S ,
, X :
: 345
,
, 0
:200
:-20
:0
S ,
, X :
: 456
,
, 0
:600
:-20
:0
S ,
, X :
:
123 180
234 = 175
345 = 179.8
456 = 580
- 1114.8
- Budget3
LinkedListObject . ,
.
, - f i r s t () next () . ,
LinkedListObject , ,
-.
, Budget3, Account.
, , :
? S ;
5 S ;
/ .
, ,
d e p o s i t {) Account. ,

260

IV.

,
. Account: w i t h d r a w a l ()
( " " ).
Account
LinkedListObject,
Account. Account
, (
, ).
count,
Account.

. .
accountNoO accountBalar.ee (} ,

, .
- Account: : f i r s t (; n e x t ()
L i n k e d L i s t O b j e c t .

L i n k e d L i s t O b j e c t , f i r s t () n e x t ( ) ,
Account.
d i s p l a y () type()
.
checking Account .
Checking , Account.
- withdrawal ,
.
Savings Checking: , ,
withdrawa l (J.
Account,
withdrawal (), ,
.
, , .
ge~ Account () checking Savings (
, ). ,
Account.
d i s p l a y R e s u l t () ,
,
, ( )
.
p r o c e s s ! ) .
( Account: : d e p o s i t ( ) ) (
S a v i n g s : : w i t h d r a w a l { ) C h e c k i n g : : w i t h d r a w a l ( } ,
, pAccount).
,
p r o c e s s ( ) . -, ,
. ,
p r o c e s s ! ) .

23.

261

,
.
, Budget3, .
Budget
(Budgetl) -
(Budget2) -
(Budget3).

262

IV.

& ...

C++,
C++
- .


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

( ).
:

24


/ &...
:

f
, C++ (+, -, & ..)
^ * . (, )
, i n t , double char (
). , ,
, ( ).
, : C++ ,
, . ,
, .
"". ,
, C++.
, C++
. ,
, ,
.
, ; - ,
, " " ">>"... ,
, (
). ,
, .
(

- , - -

,
):
, .

, , , - .

///oneftcufiofto6:
qaautiie * /
C++ , ,
, int char.
, ?

24.

265

, ,
. , USDollar,
.
. ,
( -) ?
? ? ,
. ,
USDollar USDollar. ,
USDollar . .
USDollar USDollar...
USDollar
.
:
//expense
//
( )
USDollar expense{USDollar p r i n c i p l e , double rate)
{

//

USDollar interest = principle.interest(rate);

//

//

returnprinciple,add(interest),-

, :

//expense

//
( )

USDollar expense(USDollar principle, double rate)

USDollar interest = principle*rate;


return principle + interest;

, ?
, ,
.

/
, ?
. , +
+(,)? , , a d d ( a , b ) ? . ,
.
C++ ,
o p e r a t o r ,
. , +,
i n t i n t , ,
: i n t o p e r a t o r + ( i n t , i n t ) .
, , ,
double ( double opera
t o r (double , double)). , ,
i n t double.

. ,

266

V.

. ,
(, ,
).
,
USDollar (
,
).
// USDoliar ,

//
.

//

class USDDllar

friend USDollar operator+ (USDollar&, USDollarS) ;

friend USDoilarS operator++(USDollarS);

public:

USDollar(unsigned int d, unsigned int c ) ;

protected:

unsigned int dollars;

unsigned int cents;

//

USDollar::USDollar(unsigned int d, unsigned int c)

dollars = d;

cents = c;

while (cents >= 100)

dollars++;

cents -= 100;

// operator+ si s2
//

USDollar operator+(USDollars si, USDollarS s2)

unsigned int cents = si.cents + s2.cents;

unsigned int dollars = si.doliars + s2.doliars

USDcJlar d(dollars, cents);

return d;

// operator++ ,

//

USDollarS cperator4-f (USDollar& s)

s.cents++;

if {:;.cents >= 100)

3.cents -= 100;

.dollars++;

i
return s;

24.

267

int main(int argcs, char* pArgs[


USDollar dl{1, 60)
USDollar d2(2. 50)
USDollar d3(0. 0);
d3 dl +d2;
++d3;
return 0;
USDolla r ,
. 100.
,
, ,
.
o p e r a t o r + operator+ +
, USDollar.

, ...
f r i e n d 16, " :
!". ,
.
. ,
. o p e r a t o r + () ,
USDollar.
,
. ,
( , ).
, , ,
.
. , , ,
.
, ;
, . :
. , f r i e n d
.
. ,
. I
, ,
. - . , ;
:
,
.
;
o p e r a t o r + ( ) (.. ),
( s i s2). o p e r a t o r + ( )
s i s2.
U Dollar.

, o p e r a t o r + + ( ) , .
o p e r a t o r + + ( ) , .
100, , ,
1, , , ,

268

V.

, operator + (USDollars, USDollaru)


. operator+( )
; -,
, - . ,
. ,
.
+= , + =.
, .
o p e r a t o r + + ( ) opera t o r () , ,
. , C++
, C++ .
C++
++ ++.
, .
operator+ + (ClassName)
, a operator+ + (ClassName, i n t ) .
0.
--.
.
, d3 = d l + d2 ++d3?

noq/^?
o p e r a t o r ^ () , a operator+ + ()
? ,
!

, .

operator+()
.
, + , , , o p e r a t o r + ()
- .
// ,
// ,
//
USDollarA. operator+ (USDollarS s i , USDollar& s2)
{
si.cents += s2 .cents;
if (si.cents >=100)
{
si.cents = 100;
si.dollars++;
}
si.dollars

+=

s2.dollars;

24.

269

return

si;

, , u l = u2
u 3 ; , ul 2.
, o p e r a t o r + ( ) ,
. o p e r a t o r + ()
, .
-! ,
.
USDollarS operator+(USDollarS si, USDollarS s2)
{
unsigned int cents = si.cents + s2.cents;
unsigned int dollars = si.doliars 4 s2.dollars;
USDollar result(dollars, cents);
return result
1.
,
. , r e s u l t ,
. ,
,
, r e s u l t .
,
?
USDollars operator+fUSDollars s i , USDollarS s2)
f
unsigned i n t cents = s i . c e n t s + s 2 . c e n t s ;
unsigned i n t d o l l a r s = s i . d o l l a r s i- s2. d o l l a r s ;
r e t u r n *new USDollar(dollars, c e n t s ) ;

2, , ,
, ,
: .
, .
, ""
.

.
.
: ,
o p e r a t o r + () ? ,
,
.
, .
, , :
SomeClass f ( ) ;
LotsClass g 0 ;
fn()

270

V.

int

i;

i = f !)

12 - g {) ) ;

, f ( ) , ,
( ) . , ,
.
operator++()
o p e r a t o r * () , o p e r a t o r * * (} .
,
. s.
.
//
USDollarfi operator**(USDollarb s)
{
s.cents++;
if (E.cents >= 100)
{
s.cents -=100;
.dollars**;
}
retuin s;
}
,
.
//
USDollar operator**(USDollar& s)
{
s.cents**;
if {<;:.cents >= 100)
{
'..cents- =100;
.dollars**;
}
return s;
3. s ,
.
= ++, ++(++)?
, 2.
, 1, 1
, .
+* (*+) , .
,
.
:
, .
,
. .

24.

271

Oneficwiofibi -

-. USDol l a r :
class USDollar
{
public:
USDollar (unsigned i:it d, unsigned inc. c} ;
L'SDoilar& operatcr+-i () ;
USDollar operator*(USDollarS s);
protected:
unsigned int dollars;
unsigned int cents;
'JSDollar: : USDol lar {unsigned int d, unsigned int )
{
dollars = d;
cents = ;
whi]e (cents >= 100;
(
cents -- IOC;

// operator* - this s2
//
D HCJ
USDollar USDollar::operator^USDollarS s2)
{
// "this->"
ur.signed int = this->cent s t- :-/' . cents ;
unsigned int d = t.hi s->dol . ars + s2 . del .1 ars ;
USDollar t (d, c) ;
return t;
// operator++ ,
//

USDollar& USDoliar::operator++()
{
this->cents++;
if" (this->cents >= ICO)
{
ihis->cents -- 100;
return ^this;
int main(int arges, char* pArgs[])
{
USDollar dl{1, 60);
USDoliar d2(2, 50);
272

V.

USDoIlar d3 (0, 0) ;
d3 dl + d2;
++d3;
return 0;
}
" t h i s - > " ,
-
. , , t h i s
.

USDoIlar: : o p e r a t o r s fUSDollars)
: : operator*
(uSDollarb, U S D o l l a r s ) .
, . .
//operator !
USDoIlar operators fUSDollars s i , USDollars s2)
(
unsigned i n t = s i . c e n t s + s 2 . c e n t s ;
unsigned i n t d = s i . d o l i a r s + s 2 . d o l i a r s ;
USDoIlar d ( d o l l a r s , c e n t s ) ;
retur n d;
}

//operator+ -
USDoIlar operator! (USDollars si, USDollars s2)
{
unsigned int = this->cents + s2.cents;
unsigned int d = this->dollars + s2.doliars;
USDoIlar t(dollars,cents);
return t;
}
, . , s i
s2, - " " (, t h i s ) s2.
, ,
, ; ,
.

oqna /
, , ,
C++ o p e r a t o r * (double, USDollars)
o p e r a t o r * ( U S D o l l a r S , double) .
4.
.
,
. -,
. o p e r a t o r * () ,
, :
USDoIlar o p e r a t o r * (double f, USDollar& s)

24.

273

//... ...
}
inline USDollar operator* (USDollars s, double f)
{
//
return f*s;
}
,
.
inline-
.


: -,
?
-.

()
[]
->

- , . ,
-.
USDollar operator*(double factor, USDollarS s);
void fn(USDollarfi principle)
i
USDollar interestExpense = interest^principle;
: -, o p e r a t o r * {) double.
.
, .
, "" ,
; . , , , ,
i .

onefuundfwg
//*
,
. .
i n t i = 1;
double d = 1.0;

// 1
d = i + d;

274

V.

// 2

i = i t ;
i n t double. C++ op
er a tor + ( i n t , double) , o p e r a t o r + (double, double).
( i n t , double) C++ i n t i double (,
"i double"), (double, double).
,
, double
i, .. i n t .



, C++ ,
. ,
double JSDollar.
class

USDollar

{
f r i e n d U S D o l l a r o p e r a t o r ^ ( U S D o l l a r b s i , USDollarfi
s 2 ) ;
p u b l i c :

U S D o l l a r { i n t d, int
c) ;
U S D o l l a r ( d o u b l e v a l u e )

d o l l a r s = ( i n t ) v a l u e ;

c e n t s = (int) ( ( v a l u e - d o l l a r s ) * 1 Q O + 0.5) ;

//. . . , ...

C++ double
USDollar. (, C++ ,
"" double.)
:
void fn (\JSDollar& s)
{

//

// operator-f (USDollar&, USDollars)

s = USDollar(1.5) + s; // ...

s = 1.5 + s;
// ... ..,

s = s + 1.5;
// ... ...

s = s + I;
//

// int double,

//

o p e r a t o r + (double, USDollars), opera


t o r * (USDollars,, double). C++ double USDollar
operator--)- (USDollars, USDoilar&).
,
. ,
.

,
.

24.

275

5. C+ +
.
, ,
.


.
:
class USDollar
{
public:
USDollar(double value = 0.0);
operator double()
{
return dollars + cents/100.0;
}
protected:
unsigned int dollars;
unsigned int cents;
};
USDollar:rUSDollar(double value)
{
dollars = (int)value
cents = (int)((value dollars) * 100 + 0.5);
}
o p e r a t o r doubl e ()
USDollar double,
, , 100.
:
int mainfint argcs, char* pArgs[])
{
USDollar dl(2.0), d2(1.5), d3;
//
d3 = USDollar((double)dl + (double)d2);
//
d3 dl + d2;
return 0;
}
o p e r a t o r
, . - us Dol
l a r : : o p e r a t o r double () USDollar
double. , ,
. ( C++ : "
, ".
, C++ .)
USDollar double ,
oper a t o r + (double ,
d o u b l e ) , USDollar
.

276

V.

, . C++
d3 = dl + d2, d l d2 dou
ble, USDollar. -$,
, , ,
C++.
,
. C + + USDollar
double,
. USDollar ,
double.
,
, .
double, USDollar
double , . ,

. ,

, ..


, , C++
USDollar dl double d2.
.
1 C++ o p e r a t o r + (USDollar, double).
2. , C++ ,
USDollar double.
3 , C++ , -
.
, ;
.
6.
, .
,
.
7.
. ,
.
class A
{

public:
(& ) ;
};

class
{

public:
operator

A{);

24.

277


, ,
: : o p e r a t o r A() : :(&) ,
.
, ,
. ,
. ,
,
.

278

V.

25


* ...
S , , ...
S
/

24, " ",


. ,
,
.
, C++ . ,
,
c p e r a t o r = () .

fiaxfotfia,
/ ftaJotfia...
,
: .
, source d e s t i n a t i o n :
v o i d fn()
{

struct mistrust source,


destination = source;

destination;

C++
operator={) .
. ,
, o p e r a t o r - ()
.
,
19, " ".
:
void fn(MyClass& me )
{
MyClass newMC = me; / /
newKC = me;
/ /
}
, ,
newMC, newMC .
. newMC MyClass.

25.

279

,
. , ,
, ,
. ,
, .

nefizfUj3K6U
oneftoj&ofia/
. ,

Name ( ,
).
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
class Name
{
public:
Name()
{
pName = (char*)0;
)
Name(char *pN )
{
copyName(pN);
}
Name(Names s)
{
copyName(s.pName);
}
-Name()
{
deleteName();
//
Namei operator^(Names s)
{
// ...
deleteName();
//... ,
copyName(s.pName);
//
return *this;

protected:

// copyName pN
//

void copyName(char *pN)

280

V.

int length = strlen(pN) + 1;


pName = new char[length];
strncpy(pName, pN, length);
i
// deleteName pName
void deleteName()
(
// ...
if (pName)
i
// ... ...
delete pName;
// .,. ,
//
= 0;

// pName ,
// ASCIIZ
char *pName;

int main(int argcs, char* pArgs[])


Name s("Claudette") ;
Name t("temporary") ;
t = 3;
//
return 0;
Name ,
. Name ,
.
o p e r a t o r = ( ) . ,
.
. . t
(temporary). t = s,
deleteName () , ,
, ,
, copyName () .
deleteName ( ) ,
, ,
.
.
,
.
.


, ?
, , ,
, Name .

25.

281

Name ,
, , pName. ,
. (
) copyName ().
// copyName pN
//

voici copyName (char JpN)
{
int length = strlen(pN) + 1 ;
pName = new char[lengthj;
strncpy(pName, pN, l e n g t h ) ;
}

, ,
, pN 19.
Name,
,
(,
?).
Name
, ( de
leteName{)), copyName ().
.


, " ".
"",
.
, ,
,
class MyClass
{
public:
Name n a m e ;
int age;
MyClass(char* pName,
(
age = newAge;

int newAge)

: name(pName )

void fn()
{
MyClass a f"Kinsey", 16) ;
MyClass
a b;

bfChrista",

1);

( : name fpName),
18, " ".)
19

, ,
,
(
). . .

282

V.

.
, name Name: : o p e r a t o r ^ {).
: ... !.,
( , ),
.


: o p e r a t o r = ( )
Names. void C++ ,
.
void otherFn(Names);
void fn(Kamefi oldN)
{

Name newN;
//:, . . .

otherFn{newN = oldN);
//... , ...
Name newerN;
newerN = newN = oldN;
}
newN = oldN void, ,
.
,
. ,
(i = I) 1.
i = j = 1; C++. i
j = 1, 1.
o p e r a t o r = ( )
* t h i s C++.
, : o p e r a t o r = ( )
-. , ,
.
-
. ,
+= *=;
.

,
, .
, ,
, , :
class Name
{

/ / . . . , ...
protected:

25.

283

//
Names operator^(Name& s)
{
return * this;

.
void fn(Name& n)
{
Name newN;
newN = n;
// : operator^()!
}
fn () .
,
.

284

V.

26

-
/3 & ...
/
S
S
S
S
S

...
fstream
strstream


" "

11, " C + + " ,


, -.
24, "
", , - -
<< >>, ,
, . (
, 24, "
".)
- .
: ,
; . ,

-.

, -,
i o s t r e a m . h , operator>>()
operator<< (). ,
.
// :

istream-i operator>> (istreams source, char*


pDest ) ;
istreani operator (istreams source,
int s dest) ;
istream.i operator {istream& source,

char s dest) ;
II. .

. ...
// :

istreamS operator<<{ostreams dest, char* pSource);

istreamS operator(ostreams dest, ints source);

istrearas operator(ostreams dest, chars source);

//... ...

26. -

285

|? )%

: -
operator>> {)

,
a operator<< (} .

, , :
#include <iostream.h>
void fn()
cout << " \";
C++ , ostream,
char*. ,
o p e r a t o r < < ( o s t r e a m s , c h a r * ) i o s t r e a m . h. C++
c h a r * , "
\" cout . ,
o p e r a t o r < < ( c o u t , " \").
c h a r ' , C + + ,
.
, cout ostream?
i o s t r e a r c . h (
. 26.1).
, main () .
26.1. -

cm
ccut

istream
ostream

cerr

ostream

clog

ostream

\ : " ?
? ,
?". ?
, .
C++ - ,
o u t p u t ( ) ,
: .
:
i void displayName(char* pName, i n t age)
1
{
:
o u t p u t ( c o u t , " " ) ;
:
output(cout, pName);
output(cout, "; ") ;
output(cout, age);
'
output(cout , " \ n " ) ;
; }
. -,
, .. ostream ,

286

V.

. -,
. , .
#include <iostream.h>

void f n f i n t a, i n t b)
{

cout "a + b" a + b "\n";

// cperator+
// operator
// ,
//
//cout " + " ( +
//
//cout (" + " )

) << "\";

+ ( "\");
}
-, ,
. , - ',
:

#include <iostream.h>
void fn(int a, int b) f
((cout "a + b") a + b) "\n";
}
.
;
, , ,
. "<<" , - , [
" " - . , " "?..
ostream? ostream ,
-. A i s t r e a m
.
FILE, s t d i o . h .
f open ()
FILE, ,
-. f... () ,
f p r i n t f ; , fscanf() f g e t s ( ) .
-
ostream istream,
.


ofstream, i f s t r e a m fstream
fstream.n - .
,
ostream istream.
, ,
- .
ofstream, ,
; :
ofstream::ofstream(char* pFileName,
int mode = ios::out,
int prot = filebuff::openprot) ;

26. -

287

.
, .
mode . 26.2, a p r o t . 26.3.
, OR (
i o s f i l e b u f f ostream).
26.2. mode ofstream

1OS
ios

ate
in

ios

out

( o s t r e a m )

ios

trunc

, (
)

ios

nocreate

( i s t r e a m }

ios

noreplace

ios

binary

( )

26.3. prot of stream

f i l e b u f : : openpro t

f i l e b u f : : sh_none
f i l e b u f : : sh_rea d

f i l e b u f : :sh_write

MYNAME,
.
#include <fstream.h>
void fn()
{
// MYNAME ,
//
ofstream myn("MYNAME");
//
<< " \";
}
ofstream ; : of strea m (char *) ,
. MYNAME
, ; MYNAME.
, .
, of stream : : o f s t r e a m ( c h a r * , i n t ) ,
-. ,
(
), ofstrea m ,
(,
\ \\,
).

288

V.

#include <fstream.h>
v o i d fn(>
{

// BINFILE ;
//,
ofstrea m bfile("BINFILE" , i o s : : b i n a r y I i o s : r a t e ) ;
II...
...

- bad () 1,
. ,
, :
#include <stream.h>
void fn )

{
ofstream myn("MYNAME");
if(nyn.bad())
// ...
{
err << " MYNAME\n";
return;
}
//
<< " \" ;
}

ofstream,
, ,
c l e a r ().
ofstrea m .
.
i f s t r e a m , ofstrea m ,
.
#include <fstream.h>
void fr. ()
(
// ;
// ,
ifstream bankstatement("STATEMNT", ios:mocreate);
if ;bankStatement.bad())
{
cerr "He STATEMNT\n";
return;
}
// ,
// (eof)
while (Ibankstatement.eof())
{
bankStatement accountNumber >> amount;
II... ...

26. -

289

STATEMNT, bankstatement.
, (
- , ).
(, ),
.
account Number withdrawal,
(bankstatement .eof () == t r u e ) .
i f s t r e a m

-.
c l e a r () .
fstream i f s t r e a m ofstream (,
). fstream
, .

Jloqfocaccbt strstream
i s t r s t r e a m , o s t r s t r e a m s t r s t r e a m
s t r s t r e a . h s t r s t r e a m . h .
MS DOS
; ,
8.3 DOS. ,
, Visual C++,
8.3 s t r s t r e a . h . GNU C++
s t r s t r e a m . h .
s t r s t r e a m . h ,
fstream, .
s p r i n t f O H s s c a n f ( ) .

.
// Visual C++
#ifdef WIN32
//... 8.3...
#include <strstrea.h>
//... ...
#else
//
#include <strstream.h>
#endif
//parseString ,
//
,
//

char* parseString(char* pString)
i
istrstream inp(pString, 0 ) ;
int accountNumber;
float balance;
inp >> accountNumber >> balance;
char' pBuffer = new char[12B];

290

V.

ostrstream out(pBuffer, 128);


out << " = " << accountWumber
<< ", = $" << balance;
return pBuffer;
}
p s t r i n g "1234 100 . 0".
inp i s t r s t r e a m .
. 0,
", ".
out , pBuffer.
.
, i c s : : out.
, i o s : : a t e ,
, .
, , 1234 countNumoer 100 balance . ,
, " = " *pBuffer,
1234 accountNumber, ..


" = 1234, $100.00"
#ifde f ,
. __WIN32 ,
Visual C++, ,
GNU C+ + . , Visual C++,
s t r s t r e a . h ; s t r s t r e a m . h .
. , Inp out
, .
. , ,
.

-
, .
,
EUDGET 249.600006 249.6 (
249.60). -
. ; , C++
.
,
249.6. ,
249.60.
-, -
. , ,
p r e c i s i o n ( ) :
#include <iostrearn.h>
void fn(float interest, float dollarAmount)

26. -

291

cout << " = ";


cout.precision(2);
cout << dollarArriount;
cout.precision(4);
cout << interest
<< "\n";
i
p r e c i s i o n (> dollarAmount
.
249.60 , .
.
. (
, ?) ,
iomanip.h, ,
- ( ,
iomanip.h ).
, ,
.
,
, :
#include <iostream.h>
#include <iomanip.h>
void fn(float interest, float dollarAmount)
{
cout << " " ;
<< setprecision(2) << dollarAmount
<< setprecision(4) interest
<< "\n";
}
. 26.4.
26.4.

hex

flags (10)
flags(16)

oct

flags (8)

setiill()

fill(c)

setprecision( )

precision(c)

width(n)

.
( width (n)
setw(c)) .
, ,
.
, .
, ,
8 .

292

V.

#include <lostrearn.h>
# i n c l u d e <iomanip.h>
v o i d fn{)
cout. <<
<<
<<

setw ( 8 )
10
20
"\";

/ / 8 . . .
/ / . . . 10, . . .
// 2 0


, .
:
#include <iostream.h>
#include <iomanip.h>
void fn;)
cout; <<

<<

setw (8) // . . .
10
setw(8)
20
//...
"\n";

, ,
,
s e t w ( ) .

? >-
, . , -
,
. ,
, ,
.
.
#include <iostream.h>
void fn(float value)
int previousPrecision;
// :
previousPrecision = cout.precision();
// ,
//
previousPrecision = cout.precision(2);
cout << value;
//
cout.precision{previousPrecision);
"" ,
; , , "" .
, ,
.

26. -

293

&
C++
. , ,
.
-,
,
. , , USDollar 24,
" ", d i s p l a y ! ) .
#ifdef _WIN32
#include <strstrea.h>
#else
#include <strstream.h>
#endif
#include <iomanip.h>
class USDollar
I
public:
USDollar(double v = 0.0)
{
//
dollars = (int)v;
// ,
// 0.5
cents int(( v dollars ) *" 100.0 + 0.5) ;
}
operator double()
{
return dollars + cents / 100.0;
}
void display(ostreams out)
{
out << '$' << dollars '.'
//
<< setfill('O') << setw(2) cents
//
<< setfill( ' ' ) ;
protected:
unsigned int dollars;
unsigned int cents;
// operator
ostreams operator << (ostreams , USDollarfi d)
{
d.display (o);
return o;

294

V.

int main(int argcs, char* pArgs[])


{
USDollar usd(i.50);
our << " = " << usd "\n";
usd = 2.0 * usd;
cout << " " << usd << " \n" ;
return G;
}
d i s p l a y () $,
. ,
ostream, cout.
d i s p l a y (} f strea m s t r s t r e a m ,
ostream.
, d i s p l a y ()
0.
, 10 .
, d i s p l a y {} USDollar
operator< < (ostreams , u S D o l l a r s ) .
uSDollar ,
, main ().
:
= $1.50
S3.00
, c p e r a t o r < < ( )
ostream, . ,
.
operator< < () ,
void fn(USDollars usd, float i)
{
cout << " " << usd << ", = " << i;
}

void fn'USDollar& usd, float i)
I
(((cout << " ") << usd) << ", = ") << i;
}
"" cout.
cout,
operator< < (oatreams, uSDollarS) .
ostream,
.

void, ,
, void .
, .
ostream o p e r a t o r < < ( o s t r e a m s os , USDollars usd)
I
usd.display(os);
return cout;

26. -

295

, ostream ,
, cout , ostream .
, cout
c s t r e a n . ,
.
void storeAccount(int account,
USDollar balance,
char* pName)
{
ofstream outFile("ACCOUNTS", ios::ate);
outFile << account << balance << pNarae;
account o u t F i l e
oporator< < !ostream&, int&) , o u t F i l e . o u t F i l e
operator<<(ostream& , USDollarS) USDollar.
cout o u t F i l e .
pName cout , .

" "
, ,
: cout << b a s e c l a s s o b j e c t , C++
,
. ,
-, .
, .
#include <iostream.h>
#include <iomanip.h>
class Currency
i
public:
Currency(double v = 0.0)
{
//
unit = (int)v;
//
cents = int((v unit) * 100.0 + 0.5);
}
virtual void display(ostreamk out) = 0 ;
protected:
unsigned int unit;
unsigned int cents;
};
class USDollar : public Currency
{
public:
USDoilar(double v = 0.0) : Currency(v)

296

V.

/ / : $ 1 2 3 . 4 5
v i r t u a l void d i s p l a y ( o s t r e a m s out)
{

c u t << ' $ ' u n i t << ' . '


<< s e t f i l l ( ' O ' ) << setw(2) << c e n t s
<< s e t f i l l ( ' ' ) ;

class UAH : public Currency


(
public:
UAH(couble v 0.0 ) : Currency(v)

// : 123.00 .
virtual void display(ostreams out)
{
out. << unit << ' . '
// 0
<< setfill('O') << setw(2) << cents
//
<< setfill{' ')
" .";
ostreams operator<< (ostreams o, Currency^ c)
{
.display(o);
return o;
}
void fn (Ourrencys c)
{
// ,
// operator (ostreams, Currency;*)
// -
out << " " <<
"\";

int mainiint argcs, char* pArgs[])


{
// USDollar ,
//
USDollar usd(1.50);
//
UAH d ( 3 . 0 0 ) ;
fn(d ;
r e t u r n 0;
}

Currency USDollar UAH. Cvrrc-.ic-;


d i s p l a y (> 1 .
, . d i s p l a y () .
operator<<( ) , , operator<<( )

26. -

297

uSDollar, .
UAH, operat.or<< () .
, operator<<( ) , ,
, :
$1.50
3.00 .
, ,
-, (
-) d i s p l a y () .

298

V.

27


/3 1 ...
-

S ?

, , :
. .
.
C++ ,
, (exceptions).
, .. ,
. :
(, , ) ,
.
t r y
(), throw () c a t c h ().
: .
, () ,
() .
.
#include <iostream.h>
//factorial
int factDrial(int n)
{
//
// ,
//
if ( < 0)
(
Chrow " ";
}
//
int accum
while(n >
{


=1 ;
0]
*= ;

}
return accum;
i n t main(int argcs,
{

char* pArgs[])

27.

299

try{//
out << "Factoria l of -1 is "
<< factorial(-1) endl;
//
cout << "Factorial of 10 is "
<< factorial{10) << endl;
//
catch (char* pError) {
cout << " : " << pErrcr << endl;
)
return 0;
}
main() , t r y .
.
main () .
f a c t o r i a l (} ,
, .
throw. ,
t r y
.

/
, FORTRAN?
, - :
" f a c t o r i a l () ,
. ".
? .
, . -,
, .
, ,
, ,
.
-, .
, , " " -1,
" " -2 .. ,
, , .
.
-, .
, , , -
f a c t o r i a l (), ,
. , , ,
, . ,
"
, ...", , , ,
.

300

V.

f a c t o r i a l ()
, ? ,
( ) ,
, , , , .
, .
errRtn = someFunc{);
if (errRtn)
{
errorOut{" someFn()"};
return MY_ERROR_1
}
errRtn = someOtherFunc( ) ;
if (errRtn)
{
errorOut(" someOtherFn()");
return MY__ERROR_1
}
.
| V .
%-f I
.

iy , , ,

...
,
, .
, 90% .
,
. ,
.
, ,
C++ , .
, .

&
,
. (throw) C++
.
t r y .
t r y ,
, . t r y ,
, .
.
,
, , .
r e t u r n .
.
t r y , c a t c h
( t r y ) .
, catch,

27.

301

; , catch.
,
, catch.
, .
.
#include <iostream.h>
#include <stdio.h>
class Cb j
{
public:
Cbj (char c}
label c ;
cout << " " << label << endl ;
}
~Obj
{
out << " " << label << endl ;

protected:
char label;
void fl () ,void f2() ;
int main(int, char*[j)

Obj a('a') ;
try
{
Obj b( 'b') ;
f l () ;
}
catch(float f)
{
cout << " float" << endl;
}
catch(int i)
{
cout << " int" << endl ;
}
catch (...)
{
cout << "..." << endl;
)
return 0;

void fl ()
{
try
i
Obj c('c');
f2f );
}
ca^ch(char* pMsg )

302

V.

cout << " char*" << endl;


void f2 ()
{

Obj d ( ' d ' ) ;


t h r o w 10 ;

:



d
d

b
i n t

, f 2 () i n t ,
, , d. f2 () t r y , C++
, d
f 2 (). f l ( ) t r y , catc h
char* , i n t .
C++ , f i () (
).
main{) C++ t r y .
. t r y c a t c h
f l o a t , i n t ,
. catc h - i n t ,
. c a t c h , ,
, c a t c h .

Ji/qeM /?
throw ,
. i n t ,
throw . ,
"" .
.
#include <iostream.h>
#include <string.h>
//Exception
//
class Exception
{
public:
Exception(char* pMsg, char* pFile, int nLine)
{
strncpy(msg, pMsg, sizeof msg);
msg[sizeof msg 1] = '\0 ' ;
;3trncpy (f ile, pFile, sizeof file};

27.

303

filetsizeof file 1 ] = 'NO';


lineNum = nLine;
}
virtual void display(ostreams out)
I
out << " < " << msg << ">\n";
out << " #" << lineNum
<< ", " << file << endl;
}
protected:
//
char msg[80];
// ,
char file[80];
int lineNum;
};

:
throw Exception(" ",
LINE ,
FILE ) ;
,
FILE

LINE
.

ostream, d i s p l a y (),
(. 26, "
").
catc h :
void myFunc()
try
//...
// Exception
catch(Exceptions x)
// -
.display(cerr);

c a t c h Exception,
- d i s p l a y .
c e r r
, .. , cout. cout c e r r
.
Exceptio n . ,
, . ,
invalidArgumentException
.

304

V.

class InvalidArgumentException : public Exception


{
public:
InvalidArgumentException f ir.t arg, char * pFile,
int nLine )
: Exception(" ", pFile, nLine )
{
invArg = arg;
}
virtual void display(ostreams out )
{
Exception::display(out);
cut << " " << invArg << endl ;
}
protected:
int invArg;
J;
c a t c h Exception, I n v a l i
dArgumentException , InvalidArgumentEx
c e p t i o n Exception, - d i s p l a y ! ) .

27.

305

28


8 / ...
S
S
S
J
S

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

,
-. . 28.1 - (
Sleepersofa). , Bed
(), Sofa (), .. .

Sofa

Bed
| sleep!)

| weight

wateMVO

weight

SleeperSofa
1foldOutO

. 28.1. -

306

V.

sieeperSofa :
class Bed
{
public:
Bed(>>}
void sleep(){}
int weight;
);
class Sofa
{
public:
SofaO (}
void watchTV(){}
int weight;
// SleeperSofa
class SieeperSofa : public Bed, public Sofa
{
public:
SieeperSofa(){}
void foldOut{){}
i n t main(int argcs, char* pArgs[])
1
SieeperSofa ss ;
// , .. .
ss.wai.chTV () ;
// Sofa: :watchTV()
/ / . . .. . . .
ss.foJ.dOut () ;
// SieeperSofa: : foldOut ()
// . . ..
ss.sleep();
// Bed::sleep( )
retur n 0;
}

SieeperSofa Bed Sofa.


SieeperSofa,
. , ss . s l e e p (),
ss . watchTV(). SieeperSof a Bed, Sofa.
, SieeperSofa , foldOut ().

,
.
. , Bed Sofa
weight (). ,
. : weight SieeperSofa?

28.

307

: . sleeperSof a 3 e d : : w e i g h t
Sofa: :weight. .
weight , ,
weight .
:
#include <iostream.h>
void fn()
{
SleeperSofa ss;
cout "3ec = "
<< ss. weight
<< "\";

//. ?

, weight
, .
.
#include <iostream.h>
void fn()
{
SleeperSofa ss;
cout << " - "

<< ss.Sofa::weight
<< "\n";

//,

}
,
:
.
f n () , s i e e p e r S c f a
Sofa. ,
.

Bufuntfcubttoe
SleeperSofa weight , ,
. -
, . ,
. ,
.
, ,

(, ,
). ,
. 28.2.
F u r n i t u r e () . ,
,
:
#include <lostream.h>
//Furniture ;
"weight" ""
class Furniture
{
public:

308

V.

};

Furniture() {}
intweight;

class Bed : public Furniture


public:
Bed() {}
void sleep{) {}
class Sofa : public Furniture
public:
SofaOf }
voidwatchTVO{)
class SleeperSofa : public Bed, public Sofa
public:
SleeperSofa() {}

void foldOut {}

void fn()
SleeperSofa ss;
cout << " = "
<< ss.weight
"\n";

Furniture
weg
i ht

Bed
sieepO

Sofa

wae
thTVO
N

Se
l eperSofa

I
i

fod
l OutO
Puc. 28.2.

28.

309

-... " "", "


, weight . ss
F u r n i t u r e ?
#include <iostream.h>
void fn()
{
SleeperSofa ss;
Furniture* pF;
pF = (Furniture*)Sss;
cout "Bee = "
<< pF -> weight
<<"\n";
};
ss F u r n i t u r e . ,
- , SleeperSofa*
F u r n i t u r e * . , , ?
. SleeperSofa
F u r n i t u r e . F u r n i t u r e Bed Sofa,
SleeperSofa . SleeperSofa
, . 28.3.

Furniture
Bed
( Furniture)

_ ,
Bed

Furniture
Sofa
( Furniture)

__,
Sofa

-
SleeperSofa


SleeperSofa

. 28.3. SleeperSofa
, SleeperSofa
Sofa,
SleeperSofa
.
Sofa F u r n i t u r e .

Bed,
SleeperSofa.
F u r n i t u r e ,
Sleeper -

, , ,
. 28.2. ,
, . 28.4.
S l e e p e r S o f a F u r n i t u r e ,
! , S l e e p e r S o f a
F u r n i t u r e Bed Sofa . C++
,
virtual.

310

V.

"urniture

Furniture

weight

weight

Bed
s!eep()

Sofa
watchTV()

j
1

\
SleeperSofa
foldOut()
*
. 28.4.
( )
Ifj^x-J \ |
/ XL !
, SleeperSof a
, .
#include <lostream.h>
class Furr.iture
f
public:
Furnit ure f) {}
int weight;
class Bed : virtual public Furniture
public:
Bed() {}
void leep(){}
class Sofa : virtual public Furniture
public:
Sofa() {}
void watchTVO { \
};
class SleeperSofa : public Bed, public Sofa
{
public:
SleeperSofat) : Sofa(), Bed() (}
void foldOutf)()

28.

311

void fn()
SleeperSofa ss;
cout "Bee = "
<< ss.weight
"\n";
v i r t u a l ,
Bed Sofa F u r n i t u r e . :
"- F u r n i t u r e , ,
". SleeperSof a , . 28.5.

Furniture
Bed
( Furniture)

Sofa
( Furniture)

SleeperSofa


SleeperSofa

. 28.5. SleeperSofa

, SleeperSofa F u r n i t u r e ,
Bed Sofa, F u r n i t u r e .
SleeperSof a (
, ).
weight fn (} ,
SleeperSofa F u r n i t u r e .
, (. . 28.2).
,
? -, ,
, , , ,
. -,
( ).
, Teacher-Assistant (
) Teacher () Student
(), , , Academician.
,
TeacherAssistant Academician.

312

V.

1.
.
2.
.
3. -
, - .
4. , .
, ,
.

^/

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

.
,
, -
:
Sinclude i i o s t r e a m . h >
class ! (int mem;};
class Base2 (int mem;);

class Subclass : public Basel, public Base2 {};

void fn(Subclass' pSC)

Basel* pBX = (Basel*)pSC;

T
Base2
* pB2 - (Base2 )pSC;
if<{void*)pBl -= (void*)pB2)

out <<' " " ;

int main(int args, char* pArgs

Subclass sc;

fn(Ssc);

return 0;

28.

313

1 2 , !, ,
*psc.
, ,
C++. .
Microsoft Foundation Classes (MFC),
.
.
! .
, Microsoft
, , .
, . ,
, .

314

V.

VI

S & ,..
... ""
"".
29, " ",
,
.
, ,
.

29


sfnou ...









nfteq

C++ .
, , ,
.
, .

, .
.
, . Microsoft Borland "
" (Enable All Messages),
. .
, C++
. :
#include "student.h"

#include "class.h"

Student* addNewStudent (Class class, char *pName,

SSNumber ss)

Student pS;

if(pName != 0)

pS = new Student(pName, ss);

class.addStudentipS);

return pS;

29.

317

( Visual C++) , pName 0,


pS ,
, .

2)<5
, (
).
, , .
, . ,
- .

141/

,
. , , ,
.
:
| S , ;
I / ;
I S C++ ;
| S C++ .
, .
,
, ( ),
,
.
, , ,
. ,
,
.

/

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

318

VI.

, . ,
, , .
class Array
{
public:
Array{int s)
{
E; ize = 0;
pData = new int[s];
if (pData)
size = s;
-Array{>
{
delete pData;
size - 0;
pData = 0;
}
//
int data fint index)
{
return pData[index];
}
int data (int index, int newValue)
{
int oldValue = pData[index];
pData[index] = newValue;
return oldValue;
}
protected:
int Tsize;
int pData;
};

d a t a ( i n t ) Array.
: , index
. , ?
d a t a ( i n t , i n t ) ,
.
, .
d a t a ( i n t ) .
i n t d a t a ( u n s i g n e d i n t index)
{

if(index >= size)


{

cout << " "


" ("
index "}\n";
return 0;

}
return pData[index];
}
, index
, ( index
).

29.

319

nfwzficuuuf
, ,
, , ,
.
,
, , ,
. ,
,
"" .
,
. , ,
, ,
,
.

cf&t oquH /
//taMMtf
, , , .
,
( ,
).
,
,
. ,
.

////
,
- o p e r a t o r ()
operator> > ()
o p e r a t o r H ) , C++.

,
.
C++
, .


, , ,
.
MyClass: : c r e a t e ()
, MyClass : : r e l e a s e ( ) ,
, . MyClass : : c r e a t e
. , ,

320

VI.

(,
"" MyClass : : r e l e a s e ()),
.

/oSfwJotntcu&
C++
. , ,
.

, ,
, . ,

( ,
, ).

, Microsoft MFC.
, C++,
.
,
.

29.

321


fni.

this

. t h i s
-,
" ".

v__table

,
, , , .

Abstract class

,
, .

Abstraction

,
. .
.

Base class

, .

Virtual
function

Outline function

, , ,
. . .
.

(inline)

Inline function

, ;
.

Expression

.
-+ .

Global variable

, - .

Deep copy

, , ,
, -
.

Friend

,
, .

Private

,
.

Protected

,
. .

Classification

. , ,
, , .

Constructor

-, .

322

table

(outline)

member

-, . . .

Default constructor

, ,

Signature field

,
.
-
. .

(
)

Copy constructor

,
. ,
z z : : z (z i ) .

Heap

,
m a l l o c ( ) .
f r e e ( ) .

Method

.-.

Inheritance

C++
.

Public

, .

Object-oriented
programming

,
, .

Function prototype
declaration

, (
).

Function
declaration

, ,
, ( ),
,
,

Callback function

,
.

Paradigm

.
, : -
.

Overloading

.

.

Operator
overloading

Overriding


, . .
-.

Shallow copy

Subclass

, .
U n d e r g r a d u a t e S t u d e n t , ,
U n d e r g r a d u a t e S t u d e n t .

Latebinding

, C++
.

323

Polymorphism

, -

.

Derived class

, .

Stream i/o

- C++,
o p e r a t o r << o p e r a t o r >>.
l o s t r e a m . .

Early binding

( ) .
.

Extensibility


, ,

Data segment

, C++
, . .

Code segment

, .

Stack segment

,
.

Function signature

, (
).

Short-circuit
evaluation

,
, .
: ; !
&s, ,
( f a l s e ) ,
, .

Reference variable

,
.

Static
function

-, t h i s .

Static data member

, ,

, ,
.

Variable type

.
i n t . c h a r , d o u b l e
float.

Pointer variable

, .

Disambiguation

,

.

Analysis phase

,
.

Coding phase

,
.

Design phase

,
.
.

324

member

Member function

, ; ,
-.

virtual function

, ,

Class member

. .

Instance member

( ) .

Instance of a class

. , i
i n t i n t i.

1S_A

. ,
M a l l a r d Duck; ,
M a l l a r d Duck.

325


--, 36; 42
!, 45
!=, 45
%, 40
%= , 40
&, 49; 88; i
&&. 45
*. 40; 89
*= . 40
/, 40
-, 49
|,49

private, 173
protected, 172

struct, 142; I

this. 152; 322

1,49

~, 49
+, 39
++ , 39; 42
+= , 40
<, 45
<=, 45
--,4 0
= 45
>, 45
>=, 45

V
virtual, 235

w
wchar t. 87

A
, 322
, 139; 171; 322
, 88

class, 142 1

D
delete, 96

. 227

Free Software Foundation, 23


friend, 174; 268

, 236; 240
, 248
, 310
, 206
, 29; 40; 322
, 37

GNU, 23

N
new, 96

operator, 266

. 205
, 22

-, 149
,

327

, 42

, 182; 192

, 241

#dcfine, 116
#ifdef, 116
#include, 115
, 268

, 171

, 42; 43

, 53

, 28

, 63

, 299; 1

, 299

, 142; 177

, 247; 248

, 227; 322

, 174

, 171

, 247

, 209

, 140

, 28

, 22

, 109

, 36

,
178; 185; 322
, 185

, 199

, 201

, 199

, 188

, 109; 192

, 195

, 191

, 279

, 94

, 77; 99; 158; 165

, 78; 80; 158

328

, 80

, 159

, 78

, 82

, 204

, 150

, 30614

, 312

, 109

, 225-30; 323

, 274

, 94

, 153

, 142; 177

, 147

, 206

, 177

, 177

, 153

-
, 139; 234

, 28

, 30; 265

!.45

!=, 45

%, 40
% = , 40

&, 49

&&, 45

*. 40

/, 40

, 49
|, 49
|, 45
~, 49

+ , 39

++, 39

+=, 40

<,45

<=, 45

-= , 40

=. 40

= = , 45

>. 45

>=, 45

, 265

, 279

, 266

, 285

. 39

. 39

, 44

, 98

, 41

42

, 53

, 117-36
, 12432

, 229

, 227; 243

,
117
, 117

, 323

, 265

, 73

, 93

, 94

, 28; 31

, 76

, 36

, 75

, 76; 195

, 233

, 233

, 233; 324

-, 285-98

, 292

, 294

, 286

, 72

, 38

, 38

, 41; 99
, 30; 42

, 22

, 143

, 166

, 109

, 22

, 22

, 98

, 37

, 94

, 209

, 84; 86; 101

char, 35; 87

double, 35;

float, 35

int, 35

long, 35

string, 35

, 129

, 88; 89; 159; 324

, 139

, 270

, 23

, 67; 161

, 69; 71

, 69

, 73

, 93

, 94

, 75

, 69

-, 148

, 155

, 301

, 243; 246

, 160

, 233; 235

, 143

, 55

do..while, 56

for, 57

while, 55

, 59

, 62

329

break, 60
continue. 61

, 143; 209; 325


, 209

, 248; 325

330

, 140; 143


"",
2-

www.dialektika.com


,
.

,



. ,
,


.


BASIC,

,
Internet.


.



.

V I S U A L

C + + . N E T

""
,

Visual

C++

KMW

w w w . d i a l e k t i k a . c o m

,
Visual C++ .NET.
,
:

,
.
C++
.
, Excel
Access.


,



. , ,

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

,
,
.

.

^^^^^^( /

|||-Depth

C++.
C++ In-Depth, . 1
.
C++.
,

C++.
! i.iiv- - 1 .
. v.ie . i - * .
C++, i i ..,.iaiLi<i:
-, OUCVA;I<ICV i
* .noGoii Jj.-i.t4ii.
. * s.! .
-ci t

) C++.
( C++ In-Depth, . 2
.)-,, . '1.
'>. J , . [ ^.uiarui. ) * *. \
^ <" . ^...
1;) ! iru.iK i-1:. ; i.
yiuari,, 1<]; c i u i
. . - ^ ^
: -1, . h *,
: , , C++
i:. i . , .

C++.
C++ Tn-Depth, . 3
i':dpeu.'
"-1: (1 ,
. .-'-OH ,
. - C++.
I; 11 , ,
afiw , C++
, .
t- .
.11 Loki, Web-
4:. C++.

C++.
( C++ In-Depth, . 4
! .
U
:> - C++
;
eptional C++ More Exceptional C + + ,
'
+ In-Depth, ,
:. .: C++.
*:> Guru of the Week,
.: C++, ,
. ! .

www.williamspubiishing.

com






,
,



www.dialektika.com

C++ "", 4-

7*. //.
..
. .
.
.. , . . ,
. . &

"".
101509, , . , . 43, . 1.
. . 090230 23.06.99
.
28.02.2003. 70x100/16.
Times. .
. . . 27,3 .-. . 15,83.
. 4000 . 2507.
" "
.
.
197110, -, .. 15.