C++ 21
Microsoft Visual C++. . , Microsoft Visual Studio. 6.0 - , MFC , . - . .
, .
21 1. 23 1. 24 2. C++ 40 3. 51 4. 71 5. 100 6. 135 7. 167 2. 201 8. 202 9. 233 10. 264 11. 301 12. 333 13. 374 1 4. 41 3 3. 451 15. 452 16. 505 17. 544 18. - 561 19. 596 20. 644 21. 673 . 725
. C++ 727 . 728 . 736 807
21 21 21 1 23 23 23 1. 24 24 C++ 24 25 25
, - 26
C++ - 28 C++ 29 29C++ Java 29 ANSI 30 30 31 31 32 33 C++ HELLO. 33 Visual C++ 6 36
36 37 37 38 38
38 39
2. ++ 40 C++ 40 cout 42 44
44
45 : ! 45
46 47
49 49 49
49 50
3. 51 51
52 52 53 54
55 56 57
57 58 typedef 59
short long 60 61
61 62
63 63
64 64 64 #deiine 65 const 65
66 68 68 69
70 70
4. 71 71
72 72
72
74 74 74 75
77 77
78 80 80 81
82 if 83
86 else 86 if 88
if 90 92
93 93 93
93 94 95 95 97 97 98
98 98
5. 100 1 00 , 1 01 101
1 02 1 02 1 04
1 06 1 06 108 ; 1 09 11 0, 111
11 2 112
11 3 11 4 , 116 11 9 1 21
inline- 122 1 23
1 28 1 28 1 29 131
132 132 133
133 134
6. 135 135
136 136
136 137 138 138
138 , 138 , 139
139 1 42 , 1 43
1 45 1 47
, 1 48 , 1 48
- const 151 152 155 156, - 159
162 163
163 164 165
165 165
7. 167 167
goto 167 goto 168
while 169 while 170 break continue 171 while(true) 1 73 do...while 1 74
do...while 1 75 for 1 76 for 1 78 for 181 182
- for 183 184
switch 186 switch 189 1 92 1 92 1 92
1 93 1 93
2 201 201 8. 202 202
204 206 206, 207 208 , 209
211 211
new 212 delete 213
215 215 216 217 218 this 221, 222 const 225
const 226 -
const this 227 228 230 231 231
231 231
9. 233 233 (&) 234
236 237 239 239
swap() 241 swap() 242
243 244
246 247
250 253
, 255 256 , 257 ! 259 ? 261 261 262 262
262 263
10. 264 - 264 , 266 269, 269 269 271- 272 276
277 278 280 281 this 283 284 284 286 288 290 290 290 291
293 296
297 298 299
299 299
11. 301 301
302 303 303
305 307
309 313
315 317
319
323 , 324 324 326 - 327 330
330 331 331
331 332
12. 333 333 333 335 338 338 340 341 343 344 345 346 347 348 348 350 351 strcpy() stmcpy() 353 354 360 361
361 362 371 371 372 372 373
373 373
13. 374 374
377 377 380
380 , 383
, 384
386 387 391 394- 395
396 399 401 404 408
408 , 409
Java 41 0 41 0 411
411 411
14. 413 - 41 3 - 41 8 420
423 426 429 typedef 431
- 433 - 436
438 438 439
439 439
3 451
451 15. 452 452
458 458 459 462
465 466
475 483 492 492 497 501 502 502
502 503
16. 505 505
505 506
509 - 509
509 510
511, 512 >> istream 514
515 515 517
ut 522 522
- ut 522, 524
ut. width() 524 525 526
rintf() 528 531
ofstream 531
532 - 532 ofstream 533
536 - 538 541 541 542
542 543
17. 544 544 545 548
549 550 550 550
551 using 553
using 553 using 555
557 557 std 558 559 560
560 560
18. - 561 C++ - 561 562 : 563 565 566 567
567 568 569 570 573 574
576 577 577 578 578
579 579 581 581
582 CRC 583 585
589 592
594 594 595
595 595
19. 596 596 597
597 597
599 599
602 603
, 603 607
61 0 61 5 620
624 624 625
625 631 633
633 634 634
634
637 638
, 639 640
641 641 642
642 642
20. 644, , "" 644
645 "" 646
646 647
try catch 652 652 catch 652 655
658 665 668 668
669 669 669 669
670 670 671
671 671
21. 673 673 674 #define 674
#define 674 #define 674 #else 675
676 678
678 680
680 682
682 682
683 assert() 683
assert() 685 assert() 685 686 686 691 693
699 (AND) 699 (OR) 699 (OR) 700 700 700 700 701 701
704 704 705 705 switch 705 706 706 707 707 708 708 708 assert() 709 const 709
709 709 710 71 0
710 711 71 2
71 2
. 725 . C++ 727 . 728 . 736 807
ASCII, 62; 732
, 101 , 539 , 113 , 266 , 429 , 610
, 47
, 731
ANSI, 548 iostream, 505 , 505 , 596; 624
, 731 , 699
, 701 , 506
-
, 531 , 525 , 514 , 530 , 638 , 509 , 515 , 522 , 509 , 538 , 692 , 517 , 511, 524; 530
, 625
, 626 , 631 , 626 , 626
, 323 , 452
, 732
, 633 , 466
, 307 , 326
#define, 674 #else, 675 #endif, 675 #ifdef, 674 #ifndef, 674 #include, 673 (#), 682 (##), 682
, 700
, 305 , 41 5 , 458 , 305 , 708 , 458
-, 483 , 603
3 , 313
, 333
, 271 , 338 , 351 , 344
- , 505
Java, 409 , 646; 685
, 658 , 647 , 655 , 662
, 645 , 647
, 631
, 634 , 732
Animal, 599; 693 CAT, 273; 292; 342; 413 Counter, 276 deque, 633 Employee, 456 iostream, 510 list, 631 Mammal, 303 map, 634 ofstream, 531 ostream, 522 PartsCatalog, 465 Pegasus, 374 Rectangle, 264 String, 354; 452; 627; 687 Timer, 408 vector, 625 , 638 , 452
, 466
, 483 , 536 , 686 , 655 , 624 , 395 , 371; 597 , 677
, 650 , 677 , 708 -, 509
, 727 catch, 647 class, 303 const, 65; 709 enum, 66 inline, 122; 680 namespace, 548 new, 347 operator, 286 protected, 305 return, 11 4 static, 415; 548 template, 597 try, 647typedef, 59; 431 using, 553 virtual, 322 , 57
, 44; 707
, 674 , , 674
, 64 #define,
65; 674 const, 65 , 66
, 64
, 64
, 307 , 327 , 269
, 309 , 272 , 269; 309 , 294 ,
384, 624
, 634 , 625 , 633 , 634, 625 , 631
, 544 , 351; 512
, 327 , 272 , 272
, 674
DEBUG, 683
, 678assert(), 683; 709 EVAL, 698 MAX, 678 MIN, 678 PRINT(x), 692 , 683
, 700
argv, 538 ,
347 ,
348
, 335 , 348, 333 , 338; 344 , 343 , 333 , 341 , 340 , 351 ,
350, 346 , 436 , 426 , 598
, 377 , 375 , 317; 387
, 404 , 391 , 475 , 305 , 380 , 302 , 387 , 303; 466 , 303
, 110; 546
, 183
in, 510 ut;, 42; 522 ofstream, 531 -, 509 , 546 , 280 , 281 , 324 , 271
, 272 , 277 , 291 ,
307, 287 ,
307 , 638
break, 171; 189 catch, 652 continue, 1 71 delete[], 350 dynamic_cast, 377 endl, 43 for, 1 77 goto, 1 67 return, 105; 114 switch, 186; 187; 705 using, 553 watch, 711 while, 1 69 , 725 (>>), 510 (::), 545 (
, 732 , 284
iostream, 510 ofstream, 531 ostream, 522 -, 509 , 526
, 284 , 673
HELLO.CPP, 40 , 44 , 683 , 704
Window, 548 , 550 , 550 , 557 , 544 , 548 , 557 std, 548; 558; 625
, 1 02
, 1 29 , 1 23
, 360
, 631 ,
361, 362 , 361 , 361
, 323
, 313 , 1 02
ASCII, 54; 62 , 44
, 669
, 63 , 43; 64 , 62 , 63 , 44; 64 , 530
, 728 , 730 , 729 , 732
, 631
STL, 624 , 129; 633
, 633 , 652
, 704 , 42; 351
, 351 , 358 , 359
, 323 , 47
char, 62 long, 60 short, 60 void, 47 (ADT), 396 (), 54 , 53 , 53 , 360 typedef, 59;
431 , 377 , 597 , 52; 57 , 294
, 669
ptr, 323; 436 rhs, 275 this, 283; 420 vptr, 323 , 130 , 433 , 348 , 420
, 676 , 536 -, 532 , 673 , 536
, 677 algorithm, 638 iomanip.h, 530 iostream.h, 43 list, 631 map, 634 stack, 633 stdio.h, 528 strin.h, 353 String.hpp, 458 vector, 625 , 1 02 , 1 02
, , 124; 184
, 699 , 701 , 700 , 700
stmcpy(), 353
bad(), 532 cin,get(), 515 cin.getline(), 518 cin.ignore(), 520 cin.peek(), 521
cin.putback(), 521close(), 532cout.fill, 525cout.put(), 522cout.setf 526cout.width(), 524cout.write(), 523eof(), 532fail(), 532flush(), 522Invariants(), 686main(), 41; 100; 116printf(), 528sizeof(), 53strcpy(), 353strlen(), 359, 47; 1 01 , 550, 319; 662 , 1 01, 1 00, 492 , 603 , 313 , 46; 1 00 , 102 , 1 04 , 47, , 1 01 , 11 9; 264 , 122; 680 , 11 9 , 1 00 , 1 02 , 1 02
315, 61 5 , 418; 620 , 47
, 266 , 399
, 603
, 167do...while, 175 for, 177 goto, 167 while, 169 , 189 , 182 , 181
, 596 Array, 597 , 599 , 607
, 599 , 597 , 598 , 597 , 602 , 620 , 597; 610
, 598; 610 , 333
, 340
(Jesse Liberty) , C++ - , C++ Report. Liberty Associates, Inc. (http://www.libertyassociates.com), Internet - , , .
(Distinguished Software Engineer at AT&T) - (Citibank's Development Division). (. ) (Stacey) (Robin) (Rachel). Internet : j [email protected]. Web- : http://www.libertyassociates.com Books and Resources ( ).
(David Levine) .
, . , .
Sams, . (Chris Denny) (Brad Jones) . (Tracy Dunkelbenjer), (Holly Allender), (Sean Dixon), (Heather Talbot), (Barbara Hacha) (Mona Brown).
, (Skip Gilbrech) - (David McCune), , (Steve Rogers) (Stephen Zagieboyio), C++. , : (Gordon Anderson), (Richard Ascheri), (Ron Barlow), (Ari Blachor), (Charles Campuzano), (Tammy Cercone), (Michael Chomiczewski), (. Raymond Church), (CharlesI. Desch), (Charles Dingman), (John Embow), (Jay Erikson), (Mark Fiedler), (Adam Fordyce), (Robert Francis), (Christopher Gariepy), (Greg Gordon), (Charles Hasegawa), (Elliot Kearsley), . (Andrew R. Kinnie), (Lari Kirby), (Joe Korty), (Richard Lee), (Roger Leininger), (Ruben Lopez), (Ray Lush), (Frank Marrero), (Joan Mathew), (James Mayhew), - (Sheryl McKenna), (Judith Mills), (Terry Milner), (Patrick Moore), (Chris Neely), (Gary Page), (James Parsons), (Neeral Patel), (Raoul van Prooijen), (Karen Risser), (Dan Rogers), (Ralph Russo), (Gregory Safford), (Joe Scalone), (Robert Sikes), (Sam Suboh), (Walter Sun), (Paul Sutton), (James Thompson), (Orlando Vanin), (Pascal Verdieu), (Stephen Wain), (Jerry Wares), (Jack White), (Scott Wilson), (Nick Witthaus), (Michelle Zulli) (Donald Xie).
, (Mrs. Kalish), 1965 ( ) : , , .
++.
21 , -, , , - , ++. , . , . .
, , . , .
, ,
++, . , , , , , ++. . ++.
++ .
" "? , . , , ^
21
.
.
, .
, ++ . ++ . , , . . .
. , , ( ). , .
, .
22
1
++
, . ++, , , , .
! , . , . , . , , , , , .
, , , .
, , . 6.
,
++ . . 3 , , . 4 , . 5 , , 6 . 7 , - .
C++ 21 \
, C++. :
C++ C++ , C++
C++
, , . , . . , , (, ADD MOV).
, BASIC COBOL. , , Let I = 100. . ( ) . ( ) . . , .
24 1
, , , . , , . , .
, C++, , . -.
(, Visual Basic) . Java (Virtual Machine, VM). ( Internet Explorer Netscape).
. , , . , ( ) , . , . . , , , .
:
( ), , . , , : .
, , .
. , . , . , , . C++.
, , .
. , , , ,
1. 25
. . , . , , , .
, , . , (.. ) -, , .. . , , , , , . , , .
Web . , , . , - - , , .
, . , -.
, -
, . , , , . , , . , .
. . , , , , .
. . .
1. , .2. .3. .4. .
26 1
.
1. .2. .3. .4. .
, .
1. .2. .3. .
. 1980- .
-, (, ) (, ..) . , , , .
-, , . ( ). , , . - . . , . .
, . , . (, ) . , .
, ,
. .
- , , .
- , , , , .. , - , .
1. 27
C++ - C++ -
, , : , .
,
, ( , ) . - , , .
, , . . , . , -.
: . (.. ) , , .
C++ () , . , , 6. , .. . . , ; .
Acme Motors
, : Star. , , . , .. , Star, . Quasar, Quasar Star, .
C++ . , (), . , , . Quasar Star, , . C++ 11 15.
28 1
Quasar, Star, -
. Quasar Star. . , .
C++ . , , , . 10 13.
C++ - ,
, (Bjame Stroustrup) ( ) , , - .
C++ C++, C++ . C++ , C++. , , C++, , : .
: C++ ,
? , C++, , , .
. , . 6, - .
C++ u JavaC++ ,
. , Java, , , C++ Java, . , , 90% .
1. 29
ANSI (Accredited Standards Committee),
(American National Standards Institute ANSI), C++.
C++ ISO International Standards Organization ( ). , C++, NCITS (National Committee for Information Technology Standards ), ( NCITS), ANSI/ISO. ANSI, .
ANSI ".
ANSI , C++ (.. ). , , ANSI Microsoft, . , ANSI- , , Mac, Windows Alpha.
C++ ANSI . ANSI . , , , ANSI-.
C++, ,
, . , , . , , , , , , . , . , 90% . , , .
, , : ? , , , , .
30 1
: , .. ? , , - ? , . , , : .
, , .
. , ++ .
,
, , Windows Macintosh. console easy window , .
, , . Windows Notepad, DOS Edit, Brief, Epsilon, EMACS vi. , WordPerfect, Word , .
, , . . , . . . , , . , , , .
C++ , , , ^ . : . , . C++. .
, ,
, ( , ++), , . , .
1. 31
, . . , . . . , .
, . - , .
, . , , - .
. ,obj. . .
++ ( . o b j ) . , , , . ++ ( ) , . , , . . (. 5 6).
, , .
1. , .2. obj3. .
.
32 1
,
: , , . , ( ) . , , .
, , , . . 1.1.
Hello World - . .
, , , , , . Hello World. , . , . .
1.1. h e l l o , - 1: include 2:3: int main()4: {5: cout "Hello World!\n";6: return 0;7: }
, . . 5 . . 5 (;). !
C++ - HELLO.cpp
. . . , 1.1 :
include
1. 33
. 1.1. ++
1
, , . , - . - , , . 1, iostream.h (cannot find file iostream.h), . , main, int main(); 3. main , . main, , .
:
1: include 2: int main(); // 3: int main()4: {5: cout
, , . : (# ) ---( )-". 5 --'Hello world!'.
, ! , ++. , , .
Visual C++ 6.0 , Microsoft Visual C++, 4.0 . , ANSI- , ANSI- ,
, .
, , , , , Microsoft. , - . Microsoft Visual C++ 6.0, .
, .
1. .2. File New.3. Win32 Console Application ( Win32),
, Example 1, .4. An Empty Project ( ) .5. File New.6. C++ Source File ( C++) 1.7. , .8. Build Build Example1.exe.9. .10. .11. .
Visual C++
36 1
.
. , , . !
, . hello, 7. , 1.2.
, , :
Hello,, line 5: Compound statement missing terminating; in function main(),
1.2. _______________________________________1: include 2:3: int main()4: {5: cout
, , ++ , . - . .
? , .
. , ..
, ? ,
. , . , , , , .
, ? ,
, . . ++ , , , , . , .
? , (
) ( ). , , .
, . , . , .
1. ?2. ?3. ?4. ?
38 1
, , .
include int main(){int x = 5; int = 7; cout "\n";cout
C++
++ , , . , , , - . :
++
HELLO. , 1, , . . 2.1 HELLO. .
2.1. C++ h e l l o , 1: include 2:3: int main()4: {5: cout
Hello World!
C++
40 1
1 iostream. h . #,
. . , , (#), , . -
V 21.Include ,
: . . , , , , . , , iostream.h , . h, . iostream. h (mput-output- stream -) cout, >. 1 iostream. h , * , (), .
3 main(). C++ main(). , . , nain() : .
main(), , . HELLO. main() int ( integer ), , . 0, 6. , - , C++ , main() ( ).
main() , void. C++, . main() int return 0;.
, . 0 .
({) (>). () 4 7. , , .
2. C++ 41
5. cout .
6, cout 16. , cout, C++ (, ) (, ).
cout: cout, ( ). , , . , ("), 5.
. (\) , Hello
world! . cout 17.
main() 7.
cout 16 , cout
. , cout, , cout, (
. cout 2.2. , , : Jesse Liberty , .
Hello there.Here is 5: 5The manipulator endl writes a new line to the screen.Here is a very big number: 70000 Here is the sum of 8 and 5: 13 Here's a fraction: 0.625And a very very big number: 4.9e+07 Dont forget to replace Jesse Liberty with your name...Jesse Liberty is a C++ programmer!
, cout . 11 :11: cout
10 \t, , ( 10-13). 10 long int. 11 , cout . cout , 8+5, 13.
12 cout 5/8. (float) cout, . 14 cout 7000 * 7000, (double) . double float 3.
16 . , , . !
, , .
, , , , , , .
, . , , .
++ : (//)
(/). ( ++) , .
( ) , ( / * ) , : (*/) /* */.
, , , .
++ , . , .
44 1
, , .
, , . , . , , - , . , ?
, . .
2.3 , , .
2.3. h e l l o , _______________1: include 2:3: int main()4: {5: /* ,6: , 7: */8: cout
, . .
, , , .
main(), , ,
. , . main() , .
, - . . , .
. , , . , . , , , , . 2.4.
2.4. ________________________________________________1: include 2:3: // DemonstrationFunction4: // 5: void DemonstrationFunction()6: {7: cout
58 01:1:1(). .
1() 13, 15 , , ( ) . 16 ^^^). , 01(). , 7, . ^^) ( 8) , . 17, ( ) .
, void, .. . , , . , , , void.
. , . . , , . :
int Sum(int , int b) , .
, , . . . , , .
( ), . . (return). . , void. , , , .
5; , , 3. , , , , , .
2. C++ 47
2.5 , . (, int ): 3.
Z.5. [ f u n c . ] _______________________1: include 2: int Add (int x, int y)3: {4:5: cout
, ,
: , . C++. , .
include? ,
. , , .
// /*?, (//),
. , (/*), , (*/) , , (/*). (*/), .
? ,
, . , . , , .
, . , . , .
1. ?2. mainO?3. ?4. ?5. ?
2. C++ 49
1. , I love C++.2. , ,
.3. : . ?
?
1: include 2: int main()3: {4: cout
.
. :
++ .
, , .
. . . , .
. 3.1.
(, \/1) , , , . . 3.1 . , 103. , \/1 .
100 101 102 103 104 105 106
3. 51
. . . , .
C++
, int, chart . , .
. , , .. (, int) , ( ) .
, . .
, .
char ( ) .
short int ( ) , long int ( ) , int ( short long) . (16- 32-) . ' 32- , (, Visual C++ 4 ), . 4- , . , 3.1, .
, , .
char. , . character, , , .
52 1
3.1. include
2:3:4:5:6:7;8910 11 121314
int main() {cout
, . unsigned short int 065 535. , , , , - 32 76832 767. - , .
C++ .
( ), ( ) .
, . 256 ASCII, ASCII.
ASCII , . ASCII American Standard Code for Information Interchange ( ). ASCII, .
, C++, . 3.1. , . , 3.1, . 3.1.
3.1.
Jan ,
bool 1 true falseunsigned short int 2 0-65 535short int 2 -32 768-32 767unsigned long int 4 0 -4 294 967 295long int 4 -2 147 483 648-2 147 483 647int (16 ) 2 -32 768-32 767
int (32 ) 4 -2 147 483 648-2 147 483 647
unsigned int (16 ) 2 0-65 535
unsigned int (32 ) 4 0-4 294 967 295
char 1 256 float 4 1,2-38-3,438double 8 2,2-308-1,8308
54 1
, . 3.1. , , , 3.1, , . 3.1 . , , , .
, , (
) , . , , : , J23qrsnf myAge. , , . myAge:
int myAge;
() . , , , , , . , ( , ).
, J23qrsnf, (, i) , . , myAge howMany. , , .
. , , .
1:
int main(){
unsigned short x; unsigned short y; unsigned short z; z = x * y; return 0;
}
3. 55
2:
int main (){
unsigned short Width; unsigned short Length; unsigned short Area; Area = Width * Length; return 0;
}
, , . , .
, , , , , , .
C++ . ,
. , AGE .
. , .
. , , .
. (, ), _ . , .
, (_) . , . : , 111
. , , . , ( int) /, ( long int) /. , , . , C++, , , ( . 6), , C++ .
C++
. . if, while, for main. . C++ .
. .
C++
. , , . :
unsigned int myAge, myWeight; // unsigned int long int area, width, length; // long int
, myAge myWeight, . area, width length. (long), .
. .-', ++ ., .
3. 57
(=). ,
5 Width, :unsigned short Width;Width = 5;
Width :
unsigned short Width = 5;
, . , , , , . , .
, . :// long long width = 5, length = 7;
width long int 5, length 7. , :
int myAge = 39, yourAge, hisAge = 40;
int, .
3.2 , . , .
3.2. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _1: // 2: include 3:4: int main()5: {6: unsigned short int Width = 5, Length;7: Length = 10;8:9: // Area unsigned short 10: // Width Length
long long int, a short short int.
58 1
,
? ,
, . 3.4 , short.
3.4. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _1: ((include ciostrean,h>2: int nain()3: {4: unsigned short int smallNumber;5: smallNumber = 65535;6: cout "small number:" smallNumber endl;7: smallNumber++;8: cout "small number:" smallNumber
, . . . , 1, 0. , , , , .
- , , char,
, 0 255. , ASCII .
, 97. ( , ) , . , 5 5 . 5 53, 97.
.. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _1: #include 2: int main()3: {4: for (int i = 32; i
char ( tabCharacter), \t, . , (, ).
, . , , (\), .
3.2. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
\
\ V \? \\
, ,
. , , ( ). , ,
C++ : .
,
. , int myAge = 39;
myAge int, 39 . 39.
, ( ,
). , , .
students, classes, , , (, 15 ):
students = classes * 15;
64 1
* .
15 . , :
students = classes * studentsPerClass
, , studentsPerClass, , .
C++ . define.
#define :
define studentsPerClass 15
, studentsPerClass (int, char ..). define . , studentsPerClass, 15.
, , 15.
const define , C++
, :
const unsigned short int studentsPerClass = 15;
studentsPerClass, unsigned short int. , . ( define) , (.. ).
^ . , , ^^, .
3. 65
, . , . short long, .
, .
,
, . , COLOR : RED, BLUE, GREEN, WHITE BLACK.
enum, : , , , , . :
enum COLOR { RED, BLUE, GREEN, WHITE, BLACK };
.1. COLOR, .2. : RED 0; BLUE 1;
GREEN 2 ..
. 0, , . , , , . , enum Color { RED=100, BLUE, GREEN=500, WHITE, BLACK=700 };
red 100; blue 101; GREEN 500; WHITE 501; BLACK 700.
COLOR, ( RED, BLUE, GREEN, WHITE BLACK 100, 101, 500, 501 700). COLOR . , , . , unsigned int . , . 3.7 , .
66 1
65 535, unsigned long int 4 294 967 295. ( ), , 100 200 . , , ( ) .
, , f loat , ?
int aNumber = 5.4;
, . . , 5,4 , 5. , float, 5.
? ,
. , . , 360, degreesInACircle ( ), , .
, ? :unsigned int aPositiveNumber = -1;
, . . , -1 11111111 11111111 (OxFF ) 65 535. - , .
C++, ?
, . C++ , , , . C++ . , , , .
, . , . , .
3. 69
1. ( )
?2. unsigned short int long int?3. ?4. const
define?
5. ?6. (enum) , Blue?
enum COLOR { WHITE, BLACK = 100, RED, BLUE, GREEN =300 };
7. , ?
) ) ! ) R79J) Totallncome) Invalid
1. ?
. . . .
2. .3. pi, 3.14159.4. float , pi.
70 1
,
. , , , . . :
++
++
, ( ). ++ . . :
= + ;
, , +. : , +. , . (=) , , , , .
4. 71
, ,
, . :=+;
: =+ b ;
, . , . . C++ , , .
.
, . ({) (}). , , : 1)..;' . '
"I'm confused.\n"; ''
5. 105
,
({). , if ( , 7). ( ).
,
. , , . , .
. , , , . 5.2 , .
5.Z. u _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _1: include 2:3: float Convert(float);4: int main()5: {6: float TempFer;7: float TempCel;8:9: cout
Please enter the temperature in Fahrenheit: 212
Here's the temperature in Celsius: 100
Please enter the temperature in Fahrenheit: 32
Here's the temperature in Celsius: 0
Please enter the temperature in Fahrenheit: 85
Here's the temperature in Celsius: 29.4444
6 7 float: (TempFer) ,
(TempCel) . 9 , Convert().
Convert() , 19, , TempCel. , TempCel, 7. Convert(). , TempFer, main() .
Convert() FerTemp CelTemp, . , .
TempCel , : 32 TempFer, 5 9. , 11 TempCel main(). 13 .
. 212, , (212) (100). . , .
, .
. ,
. , , , . , , , . .
, . 7.
5. 107
. , , .
, . , .
14 , ++, .
, , ,
. , , , . ++ , . , . , , , , , , 5.4.
5.4. 12345678910 11 1 21314151617181920 21 2 223242526 27
// 5.4. ,//
include
void myFuncO;
int main(){
int x = 5;cout
28: int x = 9;29:
cout
++ , , . , , . , , , : , .
, . , , , .
.
, , , int, long .
C++, , , .
, , .
, doublet), triple(), square() cube(), . :
Answer = (double(triple(square(cube(myVal)))));
myValue (), ( ) square(). squareO ( ), , triplet). triplet) ( ) doublet). , doublet) ( ) Answer.
, ( ?); , .
:unsigned long myValue = 2;unsigned long cubed = cube(myValue); / / 2 = 8unsigned long squared = square(cubed); / / 8 = 64unsigned long tripled = triple(squared); // 64 * 3 = 192unsigned long Answer = double(tripled); // 192 *2 = 384
, .
112 1
- , , .
, , , . , .. . , . 5.5.
5.5. // 5.5.
/(include
void swap(int x, int );
int main(){
int x = 5, = 10;
cout
9, 11. swap(), .
swap(), 21 , . , . 2325 , 27. , swap().
13, main(), , , , !
, swap() , .. swap() , . , 2325, , , main().
8 10 , main().
, void,
, . , return,
, . , , :
return 5; return ( > 5); return (MyFunction());
, , MyFunctionO . , return ( > 5), false, 5, true, 5. , , , false true ( ).
return, , , . return , , , return, .
return. 5.6.
114 1,
14 15 - input. 18
result. 20 Doubler() . result, 23 .
31, DoublerO, 10 . , 10 , . 10 , -1 .
35 , ( 10 000 ) 32, 34, 35. , , !
int main() void maln() ? , int maln(){ return 0;}?
, int mainO ANSI-, , int main() . , . int main() . , , . , ANSI .
, , ,
. . ,
long myFunction(int);
. , .
, , . , , . :
long myFunction (int = 50);
116 1
. myFunction long int. , 50. , -:long myFunction (int = 50);
, . -:
long myFunction (int )
, 50. , , , : , , , .
. : - , .
, long myFunction (int Paraml, int Param2, int );
Param2 , . Paraml , Param2, . , , 5.7.
5.7. , // 5.7. //
include
int VolumeCube(int length, int width = 25, int height = 1);
int main(){
int length = 100; int width = 50; int height = 2; int volume;
volume = VolumeCube(length, width, height); cout "First volume equals: " volume "\ n";
volume = VolumeCube(length, width);cout
C++
. . : , , . :
int myFunction (int, int); int myFunction (long, long); int myFunction (long);
myFunctionO . , .
. , , , .
. (. poly) , (rp. morphe) , .. , .
, . , . , . , , , , double Averagelnts(), AverageDoubles() ..
, , . int, long, float double. :
int Doubleint(int); long DoubleLong(long); float DoubleFloat(float); double DoubleDouble(double);
:
int Double(int); long Double(long); float Double(float); double Double(double);
, . , . 5.8.
5. 119
- . , , .
inl ine-
. , , . 10 , . , , 10.
, , . , , (.. - ), , . , .
inline (.. ), , . .
, . 10 , 10 . , .
? , - , . , . 5.9.
5.9. inline- __________// 5.9. inline-
include
inline int Double(int);
int main()
{int target;
cout
( ) : , . , ( , - ).
, , , . , , (() , , 5.4.
, :
1,1,2,3,5,8,13,21,34...
( ) . , , , 12- .
. 1. . , . - (-2)- (-1)- , > 2.
. , , . < 3.
:
1. , .2. 1(),
, .3. () (). < 3,
1; 1() (), -2, , -1, .
(1), 1. (2), 1. (3), , 1(2) (1). ^13(2) 1 (1) 1, (3) 2.
1(4), , (3) 1^13(2). , ^() 2 ( (2) 1(1)) 1^(2) 1, 1^(4) 3, .
. (5), , 14(4) (3). , 1=].(4) 3, (3) 2, 5.
124 1
( (20) ! 1 () 13 529 !), . . , . () . . , . 1() 5.10.
, 5.10, ( 15). , .
5.10. include
int fib (int n);
int main(){
int n, answer;cout
Enter number to find: 6
Processing fib(6).. . Call fib(4) and f b (5)Processing fib(4).. . Call fib(2) and fib(3)Processing fib(2).. . Return 1!Processing f b (3). .. Call fib(1) and f b (2)Processing fib(1).. . Return 1!Processing f b (2). .. Return 1!Processing f b (5). .. Call fib(3) and f b (4)Processing fib(3).. , Call fib(1) and f b (2)Processing fib(1).. . Return 1!Processing fib(2).. . Return 1!Processing f b (4). .. Call fib(2) and f b (3)Processing fib(2).. . Return 1!Processing fib(3).. . Call fib(1) and f b (2)Processing fib(1).. . Return 1!Processing f b (2). .. Return 1!8 is the 6th Fibonacci number
( cout. 28, , 28 :
28: cout
, , - . , .
/ return 1 /
__________ ^-----------------------------------'fibi7 L return 1 7 / retum1 / /return1 //W. 5.4.
. 5.5.
5. 127
1(5) , 3, 1() , 4 3. (4) , , (3) (2).
, 5.10, . , , 1, 2, 3, 6, .
. 20, 1(), ().
++ , .
. , , .
- .
, , . ( , void) .
? , ? ? , ? ? , ?
, . , .
. , , . , , . , , . . , .
, , . , , , . , , .
128 1
, . , , .
,
(, DOS Microsoft Windows) . C++, , , , .
. , , .
, , (Central Processing Unit CPU). , . - , . ( ) . , .
, , . , . , . . 5.6.
1
. 5.6.
, , . , " (. 5.7).
, , , . : , , ( ). , . 10 1 , , , .
100 3 X II
101 Int =7;
102 Cout ;
103 Func (,);
104 =9;
105 return;
5. 129
, . , , . .
. 5. 7.
. , . , . , ( ).
, . , , . , , , . 5.8.
theVariable
MyAge
YourAge
80
50
37
' 102!/
. 5.8.
, , , .
130 1
, . . 5.9.
theVariable
MyAge
YourAge
. 5.9.
, ,
DOS, .
1. , , , . .
2. . int, , .
3. , , . .
4. , . , , , .
5. , .6. ,
, .. .7. .
, , 2. , , .
5. 131
, , , , 1, . , , , .
, . : . .
, .
.
, . C++ main(), , , .
, , . ( inline). , .
, . . .
, , , . , , .
?- .
, , , . , ( ) .
inline? ( - )
, , , inline.
132 1
, , , ?
, .. . .
, , , , ?
8. , .
, :
int Area (int width, int length = 1); int Area (int size);
? , , |^.
, , Area () , , Area(int, int) Area(int).
, . , . , , .
1. ?2. , ,
?3. , *?4. ,
?5. ?6. ?7. ?8. ?9. ?10. ?
5. 133
1. Perimeter,
unsigned long int unsigned short int.2. Perimeter 1.
, ( ).
3. : ?include void myFunc(unsigned short int x);int main(){unsigned short int x, y; = myFunc(int);cout
C++,
, . :
- -
,
. . , Height Width (unsigned short int), 065 535, . , , . , , .
Height Width , .
, :
; , ; , .
. , , , , .. C++ , .
6. 135
! ,
. , , , . , , , , , . , .
.
( ), .
-, , , , , .. , , , , , , .. , .
, , . , , . , .. , , , , .
, . - - . -, , , ..
-, -, . - , .
-. - . Start() Break(). Cat -, , SleepO, Meow() ChaseMice().
- , -. - .
class,
, - . . , , Cat:
136 1
class Cat;
itsAge itsWeight, Meow() , . , .
Cat, , - itsAge main() :
Cat Boots;Boots.itsAge = 5; // !
, , itsAge, itsWeight Meow() Cat, - itsAge, Boots Cat. Boots , , , .
. - : Boots Cat, - , ?
, Cat , , . Boots , , . Cat, , .
. - Cat, :
class Cat< ,public:
unsigned int itsAge; unsigned int itsWeight; void MeowQ;
}; public (itsAge, itsWeight
MeowO) . 6.1 Cat -
.
140 1
.!. 1: // 2: // 3:4: include // cout 5:6: class Cat // 7: {8: public: // 9: int itsAge;10: int itsWeight:11: };12:13:14: int main()15: {16: Cat Frisky;17: Frisky.itsAge =5; // - 18: cout "Frisky is a cat who is ";19: cout Frisky.itsAge
Frisky is a cat who is 5 years old.
6 class. , .
class. Cat.
7 11. 8 public, , , private , .
9 10 - itsAge itsWeight. 14 main() . Frisky
16 Cat, .. Cat. 17 Frisky ( itsAge) 5. 18 19 - itsAge .
8 . 17, ,
.
6. 141
-
. . - , ( ). -.
? , ?
, , . , .
, Cat, itsAge, , Cat . - GetAgeO, Cat , GetAgeO . , unsigned integer long .
. , , , , , .
6.2 Cat, - - . , , .
.2. _____________________________1: // Cat2: // - , 3: // - 4:5: class Cat 6: {7: public:8: // 9: unsigned int GetAgeO;10: void SetAge(unsigned int Age);11:12: unsigned int GetWeightO;13: void SetWeight(unsigned int Weight);14:15: // -
142 1
16: void Meow();17:18: // - 19: private:20: unsigned int itsAge;21: unsigned int itsWeight;22:23: };
> . rg . 9 10 - itsAge.
12 13 - itsWeight. - .
16 - Meow(). Meow() . -, , Meow .
- 20 21. Frisky,
SetAge():
Cat Frisky;Frisky.SetAge(5); // Frisky
- ,
. , , , . . (Stroustrup), C++, : C++ , (ARM, 1990).
- ( private). - . -, - .
- .
6. 143
class class :
class _
// II
class . - , , . , , , ': . . (), (). . ( ) (.). w public private. . , . . 1:
unsigned int Age; unsigned int Weight; void Meow();
Frisky.Age = 8;Frisky Weight = 18;Fiisky.Meowt):
2:
public: // -void Start(). void AccelerateO;
void SetYear(int year);
144 1
private: // - ' -:-
' int Year;Char Model [255];
II
Car OldFaithful; // int bought; // int01dFaithful.SetYear(84); // 84bought = OldFaithful.GetYear(); // bought 84OldFaithful. StartO; StartO
,
- . , , . , .
- , , . 6.3 Cat, -.
8.3. Bnpegetem | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _1: // 2: // 3:4: ((include 5:6: class Cat 7: {8: public:9: int GetAgeO;10: void SetAge (int age);11: void Meow();12: private:13: int itsAge;14: };15:16: // GetAge, ,17: // - itsAge 18: int Cat::GetAge()19: {20: return itsAge;21: }22:23: // SetAge
// cout
//
// public // // // // private // -
6. 145
, ( 18). - , GetAgeO , Cat. , Get Age () , .
GetAgeO , , - itsAge. , main() , Cat. main( ) GetAge(). GetAgeO - Cat, - itsAge. GetAgeO itsAge mal().
26 - Set - itsAge ( 30). Cat, SetAgeO - - itsAge.
37 Meow() Cat. , Meow, . , \.
44 main(); . 46 main() Cat Frisky. 47 - itsAge 5 SetAge(). , (Frisky), (.), (SetAgeO). .
48 - MeowO, 49 - GetAgeO. 51 Meow() .
. -,
, ( ) , :
int Weight; // //
Weight =7; //
, :' j
int Weight = 7; // | RJTaKi
. . , , , , , .
6. 147
- ? -, . , void. , .
, . , . (') . . Cat :
CatO:
, ,
. .
- - , , ?
, , , , , . .
, .1 . , , , .. .
, , ?
. , , . , ,
Cat Rags; // Rags
:,-Cat();
. Cat , Cat :
Cat Frisky (5,7);
148 1
, :
Cat Frisky (3);
, (.. ), :
Cat Frisky;
, , , . :
Cat Frisky;
, , , .
, , . , .. . , .
, , . , , . .
6.4 Cat . Cat . , .
.4. muipgumpH 1: // 2: -5
// Cat0 4:
include // 01^.6: class Cat // 7: {8; public: // 9: Cat(int initialAge); // 10 ~Cat(); // 11 int GetAgeO; // 12 void SetAge(int age); // 13 void Meow();14 private: // 15 int itsAge; // -16 };1718 // Cat19 Cat::Cat(int initialAge)
6. 149
. K Meow.Frisky is a cat who is 5 years old.Meow.Now Frisky is 7 years old.
6.4 6.3 , 9 ,
. 10 , . , ; , , void.
1922 , SetAgeO, .
2426 'Cat - , , .
58 Cat Frisky. Frisky 5. - SetAgeO, Frisky 5, - itsAge, 61. 63 itsAge Frisky 7, SetAgeO. 65.
- I .
. .
- const
C++ , - . const, , . , - SomeFunctionO, void:
void SomeFunctionO const;i y/i
const. Cat :
void SetAge(int anAge); int GetAge();
6. 151
Set Age () const, - itsAge. Get () const, . GetAgeO - itsAge. , : '
void SetAge(int anAge); int GetAgeO const;
const, - , . , GetAgeO , , Cat, , (.. GetAgeO) Cat.
const , , . .
, ,
. ( ) , .
, Cat , - SetAgeO GetAgeO. , Cat Meow - Meow(). , - itsAge, . GetAgeO SetAgeO, itsAge, , .
GetAgeO const, , , GetAgeO Cat.
C++ , , . 6.5 , - .
const -, . .
152 1
6.5 !
S.S. // , //
include // cout
class Cat {public:
Cat(int initialAge);"Cat();int GetAge() const; // const void SetAge (int age); void Meow();
private: int itsAge;
>;
// Cat Cat::Cat(int initialAge){itsAge = initialAge;cout
45: // Meow46: // void47: // 48: // "Meow"49: void Cat::Meow()50: {51: cout
44 :
, . - I., * .
, .
11 GetAge( ) - , const. GetAgeO, 32, - itsAge. const, itsAge. , .
13 Meow(), const. , . , - Cat, const.
58 Cat Frisky. Cat , . . 58 , .
- , ^ . .
60 BarkQ. , , .
154 1
61 7. -, .
, . , , , . , , . , , . , , , : , , , . , .. , , , . . , , . , , . , , , .
, , . . , .
, . C++ , .. .,
.
, &^ , , . ++. , . ++.
6. 155
, . , , , , , . h, . hp . hpp. . hpp, .
, Cat CAT, hpp, .. .. . :include "Cat.hpp"
CAT. hpp . include , . , include .
hpp , ? , . . , , . hpp , .
, , . , , . , . , . . . . , .. , , , .
inline
, . inline . , - GetWeightO :
inline int Cat: .GetWeigtrtt){return itsweight; // - Weight }
156 1
, :
class Cat {public:int GetWeightO { return itsWeight; } // void SetWeight(int aWeight);>;
GetWeightO. , . , . , , :
class Cat {public:int GetWeightO const {return itsWeight:} // void SetWeight(int aWeight);};
6.6 6.7 Cat, CAT.hpp, .. , 6.7 Meow() .
> 8 . . ca t c a t , hpp________________________________
1: iinclude 2: class Cat 3: {4: public:5: Cat (int initialAge);6: ~Cat();7: int GetAgeO const { return itsAge; > // !8: void SetAge (int age) { itsAge = age;} // !9: void Meow() const { cout
1829 main( ) 6.4. , .
, -
. , , , .., . . , .
. . . . 6.8 Rectangle, RECTANGLE, hpp. , , , Point , . 6.9 .
6.8. _______________________________1: // Rect.hpp2: include 3: class Point // x,y4: {5: // , 6: public:7: void SetX(int ) { itsX = ; }8: void SetY(int y) { itsY = y; }9: int GetX() const < return itsX;}10: int GetY() const { return itsY;}11: private:12: int itsX;13: int itsY;14: }; // Point 15:16:17: class Rectangle18: {19: public:20: Rectangle (int top, int left, int bottom, int right);;21: 'Rectangle ()
28: Point GetUpperLeft() const { return itsUpperLeft; >29: Point GetLowerLeft() const { return itsLowerLeft: >30: Point GetUpperRight() const { return itsllpperRight; }31: Point GetLowerRight() const { return itsLowerRight; }32:33: void SetUpperLeft(Point Location) {itsUpperLeft = Location;}34: void SetLowerLeft(Point Location) {itsLowerLeft = Location;}35: void SetUpperRight(Point Location) {itsUpperRight = Location;}36: void SetLowerRight(Point Location) {itsLowerRight = Location;} 37:38: void SetTop(int top) { itsTop = top; }39: void SetLeft (int left) { itsLeft = left; }40: void SetBottom (int bottom) { itsBottom = bottom; }41: void SetRight (int right) { itsRight = right; }42;43: int GetArea() const;44:45: private:46: Point itsUpperLeft;47: Point itsUpperRight;48: Point itsLowerLeft;49: Point itsLowerRight;50: int itsTop;51: int itsLeft;52: int itsBottom;53: int itsRight;54: };55: // Rect.hpp
6.9. rect. ____________________________1: // rect.2: include "rect.hpp"3: Rectangle::Rectangle(int top, int left, int bottom, int right)4: {5: itsTop = top;6: itsLeft = left;7: itsBottom = bottom;8: itsRight = right;9:10: itsUpperLeft.SetX(left);11: itsUpperLeft.SetY(top);1 2 :13: itsUpperRight.SetXtright);14: itsUpperRight.SetY(top);15:16: itsLowerLeft.SetX(left);17: itsLowerLeft.SetY(bottom);18:
760 1
, , - - (. 6.9), ( Point).
- , Rectangle GetAreaO, 43. 28 29 6.9. , .
UpperLeft X. GetUpperLeftO Rectangle, , itsUpperLeft. itsUpperLeft Point, itsX , GetUpperLeftO . itsX GetX().
33 6.9 . 36 , , . , , , ( , ).
36 ( Rectangle) , Left, Bottom Right.
37 int. . , - GetAreaO Rectangle.
Rectangle Rectangle , GetAreaO.
6.8 Rect.hpp. , Rectangle, , GetAreaO int. Rectangle GetAreaO. , Rectangle GetAreaO, , Rectangle.
?
, , .
class
struct, . C++ , . , , -
162 1
. , , .
6.8 :
3 class Point struct Point; 17 class Rectangle struct Rectangle.
. .
, , , . . C++ . , . C++, , , struct class, .
. , . -.
const , ., , .
, .
, , . -,
, . , - , . - - .
, . , - .
, , , . hpp. .
. , , , .
6. 163
? -
. , . ,
. , , , .
Cat itsAge, Cat Friscky Boots, Boots - itsAge Friscky?
. - , . , Friscky Boots Cat, - Friscky ( Friscky), Boots.
- ? -
, , . , Cat GetAgeO, Cat ( Cat), , - - .
const , const ?
- , const , , GetAge() Cat, :
if (itsAge = 100) cou
, , . , , .
, . , . , .
1. ?2. ?3. ?4. (public) (private) -
?5. - ?6. - ?7. Cat,
- itsAge?8. ?
?9. - Meow Cat,
void?10. ?
1. , Employee ()
-: (), yearsOfService ( ) Salary ().
2. Employee, - -.
3. Employee, Employee; - age, YearsOfService Salary, .
4. 3 Employee, , , 1 .
5. Employee , -age, YearsOfService Salary .
6. 165
6. : ?class Square {public:
int Side;}
7. : ?class Cat large;16:17: cout "small: "
. small large.
, , . : , .
1215 . 20 .
1. small large.2. large .3. small MAXSMALL.
, 23, small 5000, small . small 5000 , 0. . 26 small 1, 28 large 2.
, . 29, .
break u continue
. continue. , ,
. break. 7.4.
. , , . , small . large , (skip). , small , large. large (target), .
, , target.
7.4. b r e a k c o n t i n u e1 : // 7.4.2: // break continue 3:4: include 5:6: int main()7: {8: unsigned short small;9: unsigned long large;10; unsigned long skip;11; unsigned long target;12: const unsigned short MAXSMALL=65535;13:
7. 171
, , -* , .
. small large, large small SMALLINT, .
32 small skip. small skip, continue ( 26). large.
target large 38. , . , break 46.
while(true] , ,
, C++. , . true. 7.5 , .
7.5. w h i l e ______________________________1: // 7.5.2: // "" 3:4: include 5:6: int main()7: {8: int counter = 0;9:10: while (true)11: {12: counter ++;13: if (counter > 10)14: break;15: }16: cout
counter 10. break 14, counter ( 16).
, . while. counter .
C++ . , .
du...while while ,
. , , , . 7.6.
7.. w h i l e ________________________________1: // 7.6.2: // ,3: // .4:5: include ciostream.h>6 :7: int main()8: {9: int counter;10; cout "How many hellos?: ";11: cin counter;12: while (counter > 0)13: {14: cout
10 , counter. 12
, . , . 0. , . .
, ? while , . if counter.
If (counter < 1) // counter = 1;
, .
du...wlrilc do...while
. . 7.7 , while do.. while.
7.7. d o . . w h i l e _____________________________________1: // 7.7.2: // do...while 3 :4: include 5:6: int main()7: {8: int counter;9; cout > counter;11: do12 : {13: cout 0 );16: cout "Counter is: "
f ' 9 , counter. do.. while
, . 13 , 14 counter . 15. , ( 13). 16.
do.. while break continue , while. . , .
for while
: , ( 7.8).
7 . 8 . while___________________________1: // 7.8.2: // while 3:4: include 5:6: int main()7: {8: int counter = 0;9:10: while(counter < 5)11: {12: counter++;13: cout
, . , .
for , , . : , . for , , . .
for . , , , for, C++ , - - . for , . , while. ( ). C++. , , for C++, , . for 7.9.
7 . 8 . f o r ___________________________________________1 : / / 7 , 9 .2: // for 3:4: ((include 5:6 : i n t m a i n ( )7: itsLength = length; } int GetLength() const { return this->itsLength; }
1112131415161718 19
private;
void SetWidth(int width) { itsWidth = width; } int GetWidth() const { return itsWidth; }
int itsLength int itsWidth;
20; };2122 Rectangle;:Rectangle()23; SetWidth(10) ;41: // pConstRect->SetWidth(10);42: pConstPtr->SetWidth(10);43:44: cout
. , ,
, , . ( 8.11).
8 . 1 1 . _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
1: #include 2: include 3: include 4: bool GetWord(char* string, char* word, int& wordOffset);5: // 6: int main()7
9:1 0111 213141516 17
const int bufferSize = 255; char buffer'[bufferSize+1 ];
char word[bufferSize+1 ]; int wordOffset = 0;
// // //
cout
GetWordO 27. , 30, st ng [ wo rdOffset] . , . GetWordO false.
33 . 34 , WordOffset. WordOffset 0, .
37 38 1 , . , false ( 41 42).
, 1 . 46 2 .
49 50 , , . 2 . 1 2 . 2 1 . ( 55). .
61 , . 2 , WordOffset . true, , .
, , , .
.
, (&). .
, , . . , 0.
, , (*). . . , , .
- , new, . delete. , , .
230 2
? ,
. 13 .
?, ,
, . , , . .
, ? , .
, . const .
, , . , . , .
1. ?2. , ,
?3. ?4. , , ,
?5. ?6. : const int * ptrOne int * const
ptrTwo?
1. :
int *
int vTwo
int * pThree = &vTwo
8. 231
2. , youiAge unsigned short. , ?
3. you f Age 50.4. , int
. . , - ,
5. : .((include int mainQ
{int * pint;pint = 9;cout "The value at pint: "
g
,
. , , , , . :
, . , . , , , .
, (&), . , . , somelnt, ,
int &rSomeRef = somelnt:
: rSomeRef , somelnt. 9.1.
, (&) , , . , , .
9. 233
8 . 1 .
1: // 9.1.2: // 3:4: include 5:6: int main()7: {8: int intOne;9: int &rSomeRef = intOne;10 :11: intOne = 5;12: cout
9.Z. 1: // 9.2.2: // 3:4: ((include 5:6: int main()7: {8: int intOne;9: int &rSoneRef = intOne;1 0 :
11: intOne = 5;12: cout
City; . , beanTown, boston.
& 9 9.2, rSomeRef int, & 15 16, intOne rSomeRef.
. , 13.
, , , . , , . 9.3.
8.3. ___________________________________________1: // 9.3.2: //
3456 7
((include ciostream.h>
int main()
8 int intOne;int &rSomeRef = intOne;9:
10: 11: 1 2 : 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24; 25: 26:
intOne = 5;cout
intOne: 5rSomeRef; 5intOne: 0x213e&rSomeRef: 0x213e
intOne: 8intTwo: 8rSomeRef: 8&intOne:
COX
&intTwo: 0x2130&rSomeRef: 0x213e
8 9 . 11
5, 1215 , . 17 11:,
8. 18 !^ , , . !^ 110, :
10 = .;
, !^ ( 19 21): ].1:\. 2224 , ^ ^, 11:\.
. .
. .
, (
) . , , . :
int & In tRef = int; //
rlntRef , , :
int howBig = 200: int & rlntRef = howBig;
CAT:
CAT & rCatRef = CAT; //
9. 237
rCatRef , CAT:
CAT frisky;CAT & rCatRef = frisky;
, . - - ( . ) , , , . 9.4.
9.4. _____________________________________________1: // 9.4.2: // 3:4: include 5:6: class SimpleCat 7: {8 public9:1 0111 2131415
SimpleCat (int age, int weight); ~SimpleCat() { }int GetAge() { return itsAge; } int GetWeightO { return itsWeight; }
private:int itsAge; int itsWeight;
16: } ; 17:18: Sim19: {
SimpleCat::SimpleCat(int age, int weight)
2021
itsAge = age; itsWeight = weight;
22: }23:24: 25: {
int main()
262728293031323334
cout
26 Frisky SimpleCat. 27 rCat
SimpleCat, Frisky. 30 32 SimpleCat, SimpleCat (Frisky), SimpleCat (rCat). , . : .
, (&) . . 1:int hisAge;int &rAge'= hisAge;
2:CAT boots;CAT &rCatRef = boots;
,
(0). . , , , . . , - .
, , - . , .
5 , :
, .
. ++ : . : , , .
, . , , .
9. 239
5 , , , . ( , ), , .
, . , , , .
, .
, 5.5 (. 5) , 1() . - ( 9.5).
8.5. 1; // 9.5. 2:3: include 4:5: void swap(int x, int y);6:7: int main()89:1 0111213141516 17 void swap (int x, int y)
int x = 5, = 10;
cout
!(), , , , . () , .
, () , .. . , .
++ : 8\
Main. Before swap, x: 5 y: 10 Swap. Before swap, *px: 5 *py: 10 Swap. After swap, *px: 10 *py: 5 Main. After swap, x: 10 y: 5
! 5 swapQ int,
int. 12 swap() .
19 swap() temp, : * (.. ) . temp .
23 temp , . 24 , , . 25 , temp (.. , ), .
, swap(), .
, , , swap() . -, swap() , , . -, swap().
C++ . , . 9.7, swapQ .
9.7. s w a p ( ) , ________________________1: // 9.7. -.2: // !
swap))
3456 7
void swap(int &, int &);
include
8 int main()9:1 0111 21314
cout
151617181920 21 2 22324252627282930
return 0;
void swap (int &rx, int &ry)
int temp;
cout
, . , , , , , swapO, , , .
swapO - , , , .
C++ . , . , .
(Colonel John Roebling) (Brooklyn Bridge), . , . , .
C++ , . , , , , .
, , , , ., , , , .
, .
, ? . . , , . , ( ) .
, , . 9.8 , : - .
.8. _____________________________ _1: // 9,8,2: // 3:4: include 5: int6: short Factor(int n, int* pSquared, int* pCubed);7:8: int tnain()9: {
244 2
10: int number, squared, cubed;11: short error;1 2 :
13: cout > number;15:16: error = Factor(number, &squared, &cubed);17:18: if (!error)19: {20: cout
, ( ) , , .
36 37 main() . 38 Value , . 40 Value .
, :
enum ERR0R_VALUE { SUCCESS, FAILURE} ;
0 1 SUCCESS FAILURE.
9.8 , , , . 9.9 , , ERROR.
3.9. _________________________________1 : // 9.9,2: // 3: //
45 include
7 typedef unsigned short USHORT; enum ERR_C0DE < SUCCESS, ERROR }8
9:1 011121314151617181920 21 2 223242526
ERR_C0DE Factor(USH0RT, USH0RT&, USH0RT&);
int main()
cout
27: }28: else29: cout "Error encountered!!\ n";30: return 0;31: }32:33: ERR_C0DE Factor(USH0RT n, USHORT &rSquared, USHORT &rCubed) 34: {35: if (n > 20)36: return ERROR; // simple error code37: else38: {39: rSquared = n*n;40: rCubed = n*n*n;41: return SUCCESS;42: }43: >
Enter a number (0 - 20): 3 number: 3 square: 9 cubed: 27
9.9 9.8 . ERR C0DE (. 36
41), , , ( 22). Factor().
, squared cubed, .
. .
5 , . , , .
, , . - -. - , , , , .
, . -- . , -
9. 247
-, , - , .
, , . , , .
. 9.10 SimpleCat. , , , - .
, 9.10 SimpleCat, . Cat , . , , .
3.10. _ ________________________1 : // 9.10.2: // 3:4: include 5:6: class SimpleCat 7: {8: public:9: SimpleCat (); // 10: SimpleCat(SimpleCat&); // -11: "SimpleCatO; // 1 2 : } :13:14: SimpleCat::SimpleCat()15: {16: cout
36 main() FunctionOne, ( 3 ). FunctionOne() SimpleCat , SimpleCat . , ( 4).
46, , ( 5 ). , SimpleCat . - , , ( 6 ).
, FunctionOne(), , , , , , , 7 . FunctionOneO , , , 8 .
main(), FunctionTwo(), . , - . FunctionTwoO , 10 , SimpleCat ( ), .
Frisky , , ( 11 ).
, , FunctionOne() , , FunctionTwoO .
FunctionTwoO ,
, . FunctionTwoO , SimpleCat, SimpleCat. , .
. - , . .
, SimpleCat. , SimpleCat. 9.11.
250 2
9.11. ____________________________1: // 9.11.2: // 3:4: include 5:6: class SimpleCat7: {8: public:9: SimpleCatO;10: SimpleCat(SimpleCat&);11: 'SimpleCatO;1 2 :
13: int GetAgeO const { return itsAge; }14: void SetAge(int age) { itsAge = age; }15:16: private:17: int itsAge;18: > ;19:20: SimpleCat::SimpleCat()21: {22: cout "Simple Cat Constructor...\n";23: itsAge = 1;24: }25:26: SimpleCat::SimpleCat(SimpleCat&)27: {28: cout
49:50:51:52:53:54:55: return 0 56: }57:58: // functionTwo, passes a const pointer59: const SimpleCat * const FunctionTwo (const SinipleGat * const theCat) 60: {61: cout "Function Two, Returning,..\ n";62: cout "Frisky is now " theCat->GetAge();63: cout SotAge(8): const!65: return theCat:66: }
cout
, , main(), Frisky SetAge(). FunctionTwoO, , FunctionTwoO , , , !
, 9.11,
, , . , . - , .
, , , , . 9.12.
3.12. 1: // 9.12.2: // 3:4: include 5:6: class SimpleCat 7: {8: public:9: SimpleCat();10: SimpleCat(SimpleCat&);11: "SimpleCat();1 2 :13: int GetAge() const { return itsAge; >14: void SetAge(int age) { itsAge = age: }15:16: private:17: int itsAge;18: } ;19:20: SimpleCat::SimpleCat()21: {22: cout
, C++, SimpleCat SimpleCat. , , . const, , .
, -
, . , , .
. , , . , , - , , . .
new. new , . , , , . , :
int * pint = new int; if (pint != NULL) int &rlnt = pint;
pint int, , new. ( pint) , , null, pint . int int, rlnt . , rlnt int, new.
, . .
, . , . const , .
9. 255
,
, , , :
CAT * SomeFunction (Person &theOwner, House *theHouse, int age);
, SomeFunction . Person, House, . CAT.
, (&) (*). :
1: & rFrisky;2: CAT & rFrisky;3: CAT &rFrisky;]
^ C++ , , , , . , , . , . . rFrisky rFrisky, CAT. , & . . CAT . & , . , & CAT :
& rFrisky, rBoots;
, rFrisky, rBoots CAT. . , rFrisky CAT, rBoots ( ) , CAT. -:
CAT &rFrisky, rBoots;
, . :
& rFrisky;CAT Boots;
256 2
(, , , , (, , ), 2:
2: CAT & rFrisky;
, (&) (*). , , , . .1. ,
.2. ,
.
, !
, . , . , . : , , ?
9.13 , , .
9.13. _______1: // 9.13.2: // ,3: // 4:5: include 6:7: class SimpleCat8: {9: public:10: SimpleCat (int age, int weight);11: 'SimpleCat() { }12: int GetAge() { return itsAge; }13: int GetWeight() { return itsWeight; }14: private:
9. 257
, 9.13, TheFunctionO Frisky . TheFunctionO Frisky .
: , Frisky, ? 9.14.
.14. 1: // 9.14.2: // 3: include 4:5: class SimpleCat6: {7: public:8: SimpleCat (int age, int weight);9: "SimpleCatO { }10: int GetAgeO { return itsAge; >11: int GetWeightO { return itsWeight; }12:13 private:14: int itsAge;15: int itsWeight;16: } ;17:18: SimpleCat::SimpleCat(int age, int weight)19: {20: itsAge = age;21: itsWeight = weight;2 2 : }23:24: SimpleCat & TheFunctionO;25:26; int main()27: {28: SimpleCat & rCat = TheFunctionO;29: int age = rCat.GetAgeO;30: cout "rCat " age
. SimpleCat 28 TheFunction . SimpleCat ( TheFunction()), , TheFunction() . , , delete.
(, ) , TheFunction()ccbuiKy .
?
. , , .
, , , , . , . , .
, , . : delete . . , , .
, , , . , . , , , .
, . I -1 , .
, . .
,
. , . , , , . , .
9. 261
, , . .
, , , , , , .
, , .
, ? , .
, .
, ? , .
, .
? , ,
, .
, ?
, .
, , . , . , .
1. ?2. , ?3. new, ?4. ?5. ?
262 2
1. , int,
int int. int.
2. , . , varOne. varOne 6. , varOne 7. varTwo. , varTwo. .
3. , 2. ? ?
4. , .5. 4, .6. , .7. 6.8. : ?
1: include 2:3: class CAT 4: {5: public:6: CAT(int age) { itsAge = age; }7: CAT(){ >8: int GetAge() const { return itsAge;}9: private:10: int itsAge;11: ) ;1 2 :13: CAT & MakeCat(int age);14: int main()15: {16: int age = 7;17; CAT Boots = MakeCat(age);18; cout "Boots is " Boots.GetAgeO " years old!\n";19; return 0;20: }21:22: CAT & MakeCat(int age)23: {24: CAT * pCat = new CAT(age);25; return *pCat;26: }
9. 8.
9. 263
5 . , , , . :
-
- 5 ,
. . - .
Rectangle ( 10.1) DrawShape(). , , , . , .
10.1. -___________________________________________1: // 10.1. -2: ((include 3:4: int5: // Rectangle6: class Rectangle7: {
264 2
58: theRect. DrawShapeO;59: cout ''\nDrawShape(40, 2): \n"; 60: theRect.DrawShape(40,2);61: return 0;62: >
************************************************************
DrawShape(40,2):************************************************************************************************************************
10.1 , - . , . .
14 15, DrawShapeO. , 30 50. , DrawShapeO , , -. . , .
52 62 DrawShapeO. , unsigned short integer.
. DrawShapeO, , , .
, - , ,
, . - , , , 10.2
266 2
1B.Z. , 1: // 10.2. , 2: ((include 3:4: int 5:6: // Rectangle 7: class Rectangle 8: {9: public:10: // 11: Rectangle(int width, int height);12: "Rectangle( ) { }13: void DrawShape(int aWidth, int aHeight, bool UseCurrentVals = false) const; 14:15: private:16: int itsWidth;17: int itsHeight;18: } ;19:20: // 21: Rectangle::Rectangle(int width, int height):22: itsWidth(width), // 23: itsHeight(height)24: < } // 25:26:27: // 28: void Rectangle::DrawShape(29: int width,30: int height,31: bool UseCurrentValue32: ) const 33: {34: int printWidth;35: int printHeight;36:37: if (UseCurrentValue == true)38: {39: printWidth = itsWidth; // 40: printHeight = itsHeight;41 : } ' . '42: else 43: 47:48:
10. 267
10.1 10.2 , 10.1 . , , , , , , .
, ? . , :
, ;
;
, .
, , , ,
, . , , , .
, , . , . , , .
, - , . , , , !
. ,
Rectangle . . . .
10. 269
, , . .
, Rectangle . , . 10.3.
10.3. ____________________________ _________________1 : // 10.3.2: // 3:4: include 5:6: class Rectangle 7: {8 public:
RectangleO;Rectangle(int width, int length);"RectangleO { >int GetWidthO const < return itsWidth; } int GetLength() const { return itsLength; }
9:10111 21314151617181920 21 222324252627282930313233343536373839404142
Rectangle::Rectangle()
private:int itsWidth; int itsLength;
Rectangle:: Rectangle (int width, int length)
}
int main()
Rectangle Recti; cout "Recti width: " Recti.GetWidth() endl; cout "Recti length: " Recti.GetLength() endl;
int aWidth, aLength; cout
4344454647
Rectangle Rect2(aWidth, alength);cout "\ nRect2 width: " Rect2.GetWidth() endl; cout Rect2 length: " Rect2.GetLength() endl;
return 0;
Recti width: 5 Recti length: 10 Enter a width: 20
Enter a length: 50
Rect2 width: 20 Rect2 length: 50
Rectangle