Upload
buinguyet
View
263
Download
13
Embed Size (px)
Citation preview
www.samouchiteli.ru
1
++
++ . . ++ , , , , , , , , - , . ++ , , , , Standard Template Library (STL). string, , , vector, list, map, set, , . , , ++. , , ++ , C++ .
++, . , , - : (Stan) , (Jose) . ++ : C++ Walt Disney Feature Animation, ++, ++ IBM Canada Laboratory.
++ 1984 . , Bell Laboratories (Bjarne Stroustrup), . cfront, , 1.1 1986 3.0, 2.1 3.0. , C++.
, ++ IBM Canada Laboratory . 1990 . - .
C++ , , .
http://www.samouchiteli.ru
www.samouchiteli.ru
2
C++ ++. , ++; . , , , - .., . , - , , . , . : , . , . ( .)
, - . , ++, ! , . , , , , .
++ , . ++, . . , , , , . , , , . , , . ++. , ++, .
1 2 ++ . , . ++, , . : !
1 : , , , . ++, , /. , ++ , . 2 , - .
http://www.samouchiteli.ru
www.samouchiteli.ru
3
. , , , (generic) . , . .
++ , , , . . II ( 3-6) .
3 , , string, complex vector ++. , . 4 , , . , ++ , 5. 6. , .
7-12 ( III) - ++. 7 . , , , , print(). ( , .) , , 8. 9: , , , - . , print() . 10 . , .
++ . , , , , . , , , . - , - . . 11 , . , 19, , - .
12 6, . . , 6, 12, . -. -
http://www.samouchiteli.ru
www.samouchiteli.ru
4
, , , . .
13-16 ( IV) , . , , ++ . , . .
13 : , ( ), . , .
14 , ++ . -, , . , , .
15 . , , , , , , new delete, . , , , . , . , , .
16. , . , vector . , . , , : , , . 16 , , , , (-, ) , . .
- () ++ 17-20 ( IV). 17
http://www.samouchiteli.ru
www.samouchiteli.ru
5
. , , / ( /). , - -. - () , . , Employee () : TemporaryEmpl ( ) Manager (), Employee. . , , . , Employee , TemporaryEmpl Manager. , . ++ .
, 17 . 6 Query. 18 . 16 .
19 (RTTI run time type identification). RTTI . , Employee, Manager. , 19 , . .
20 / iostream. , , , , . / .
, , .
, , . , , , . . C++ , Inside the C++ Object Model [LIPPMAN96a], . C++ , , .
++ , .
http://www.samouchiteli.ru
www.samouchiteli.ru
6
++ , . , ([MUSSER96] [STRUOSTRUP97u]). , ++.
:
, : , , , bool, ;
, ++, complex, string, auto_ptr, pair, ( list, vector, map set) ;
, , , ++ . , . , , . , ( clone() 17.5.7). - ( , , !);
. , ++ . , , , .
++
++ ISO/ANSI ++. (ISO) 1998 .
++, , . , ++ . , ++ . , ++ .
, . . ++. , .
http://www.samouchiteli.ru
www.samouchiteli.ru
7
, . , ++. , , ++ .
, , , , , . ++ ( ) ++.
: (Paul Abrahams), (Michael Ball), (Stephen Edwards), (Cay Horstmann), (Brian Kernighan), (Tom Lyons), (Robert Murray), (Ed Scheibel), (Roy Turner), (Jon Wada). . (Clovis Tondo) (Bruce Leung) .
- (Shyh-Chyuan Huang) (Jinko Gotoh) - (Firebird), , , .
(Gabby Silbermarr), (Karen Bennet), (Centre for Advanced Studies) . .
: (Debbie Lafferty), , (Mike Hendrickson) (John Fuller). Big Purple Company . 6.1 (Elena Driskill). .
, . (Barbara Moo). , , . , , (Stephen Dewhurst), ++, (Nancy Wilkinson) cfront.
(Dag Bruck), (Martin Carroll), (William Hopkins), (Brian Kernighan), (Andrew Koenig), (Alexis Layton) (Barbara Moo) . . (Andy Baily), (Phil Brown), (James Coplien), (Elizabeth Flanagan), (David Jordan), (Don Kretsch), (Craig Rubin), (Jonathan Shopiro), (Judy Ward), (Nancy Wilkinson) (Clay Wilson)
http://www.samouchiteli.ru
www.samouchiteli.ru
8
. (David Prosser) , ANSI C.
(Jerry Schwarz), iostream, , ( 20 ). . , 3.0: (Laura Eaves), (George Logothetis), (Judy Ward) (Nancy Wilkinson).
(James Adcock), (Steven Bellovin), (Jon Forrest), (Maurice Herlihy), (Norman Kerth), (Darrell Long), (Victor Milenkovic) (Justin Smith) Addison-Wesley.
(David Beckedorff), (Dag Bruck), (John Eldridge), (Jim Humelsine), (Dave Jordan), (Ami Kleinman), (Andrew Koenig), ' (Tim O'Konski), (Clovis Tondo) (Steve Vinoski) .
(Brian Kernighan) (Andrew Koenig) .
, ++, .
[BOOCH94] Booch, Grady, Object-Oriented Analysis and Design, Benjamin/Cummings. Redwood City, CA (1994) ISBN 0-8053-5340-2.
[GAMMA95] Gamma, Erich, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns, Addison Wesley Longman, Inc., Reading, MA (1995) ISBN 0-201-63361-2.
[GHEZZI97] Ghezzi, Carlo, and Mehdi Jazayeri, Programming Language Concepts, 3rd Edition, John Wiley and Sons, New York, NY (1997) ISBN 0-471-10426-4.
[HARBISON88] Samuel P. Harbison and Guy L. Steele, Jr., C: A Reference Manual, 3rd Edition, Prentice-Hall, Englewood Cliffs, NJ (1988) ISBN 0-13-110933-2.
[ISO-C++97] Draft Proposed International Standard for Information Systems Programming Language C++ Final Draft (FDIS) 14882.
[KERNIGHAN88] Kernighan, Brian W.I. and Dennis M. Ritchie, The C Programming Language, Prentice-Hall, Englewood Cliffs, NJ (1988) ISBN 0-13-110362-8.
[KOENIG97] Koenig, Andrew, and Barbara Moo, Ruminations on C++, Addison Wesley Longman, Inc., Reading, MA (1997) ISBN 0-201-42339-1.
[LIPPMAN91] Lippman, Stanley, C++ Primer, 2nd Edition, Addison Wesley Longman, Inc., Reading, MA (1991) ISBN 0-201-54848-8.
[LIPPMAN96a] Lippman, Stanley, Inside the C++ Object Model, Addison Wesley Longman, Inc., Reading, MA (1996) ISBN 0-201-83454-5.
[LIPPMAN96b] Lippman, Stanley, Editor, C++ Gems, a SIGS Books imprint, Cambridge University Press, Cambridge, England (1996) ISBN 0-13570581-9.
http://www.samouchiteli.ru
www.samouchiteli.ru
9
[MEYERS98] Movers, Scott, Effective C++, 2nd Edition, Addison Wesley Longman, Inc., Reading, MA (1998) ISBN 0-201-92488-9.
[MEYERS96] Meyers, Scott, More Effective C++, Addison Wesley Longman, Inc., Reading, MA (1996) ISBN 0-201-63371-X.
[MURRAY93] Murray Robert B., C++ Strategies and Tactics, Addison Wesley Longman, Inc., Reading, MA (1993) ISBN 0-201-56382-7.
[MUSSER96] Musser, David R., and Atui Saint, STL Tutorial and Reference Guide, Addison Wesley Longman, Inc., Reading, MA (1996) ISBN 0-201-63398-1.
[NACKMAN94] Barton, John J., and Lee R. Nackman, Scientific and Engineering C++, An Introduction with Advanced Techniques and Examples, Addison Wesley Longman, Inc., Reading, MA (1994) ISBN 0-201-53393-6.
[NEIDER93] Neider, Jackie, Tom Davis, and Mason Woo, OpenGL Programming Guide, Addison Wesley Inc., Reading, MA (1993) ISBN 0-201-63274-8.
[PERSON68] Person, Russell V., Essentials of Mathematics, 2nd Edition, John Wiley & Sons, Inc., New York, NY (1968) ISBN 0-132-84191-6.
[PLAUGER92] Plauger, P.J., The Standard C Library, Prentice-Hall, Englewood Cliffs, NJ (1992) ISBN 0-13-131509-9.
[SEDGEWICK88] Sedgewick, Robert, Algorithms, 2nd Edition, Addison Wesley Longman, Inc., Reading, MA (1988) ISBN 0-201-06673-4.
[SHAMPINE97] Shampine, L.E, R.C. Alien, Jr., and S. Pruess, Fundamentals of Numerical Computing, John Wiley & Sons, Inc., New York, NY (1997) ISBN 0-471-16363-5.
[STROUSTRUP94] Stroustrup, Bjarne, The Design and Evolution of C++, Addison Wesley Longman, Inc., Reading, MA (1994) ISBN 0-201-54330-3.
[STROUSTRUP97] Stroustrup, Bjarne, The C++ Programming Language, 3rd Edition, Addison Wesley Longman, Inc., Reading, MA (1997) ISBN 0-201-88954-4.
[UPSTILL90] Upstill, Steve, The RenderMan Companion, Addison Wesley Longman, Inc., Reading, MA (1990) ISBN 0-201-50868-0.
[WERNECKE94] Wernecke, Josie, The Inventor Mentor, Addison Wesley Longman, Inc., Reading, MA (1994) ISBN 0-201-62495-8.
[YOUNG95] Young, Douglas A., Object-Oriented Programming with C++ and OSF/ Motif, 2nd Edition, Prentice-Hall, Englewood Cliffs, NJ (1995) ISBN 0-132-09255-7.
http://www.samouchiteli.ru
www.samouchiteli.ru
10
I C++
, , :
;
, .
, ( ) . .
. , , . : . . FORTRAN, C Pascal. ++ . . III , ++ : , , .
70- ( ). . ++ . , , , .. , , . . , CLU, Ada Modula-2. IV ++.
- ( ) ( ). -. , , - , ( ) , , . , . , - , Simula, Smalltalk Java. V - ++.
++ - , , . , . : ++ .
I ++. , . , 13, .
http://www.samouchiteli.ru
www.samouchiteli.ru
11
, . , , , . , , . . , ++.
1 : , , , (statements) . ++ , , /.
2 , , -. , ++, . , , . , ++, .
, 2 . , . , . 3 . 2.
1.
: , , , . ++ , , , , . , ++ .
1.1.
, - . , . , , . . . .
. , , . , , , , . . :
http://www.samouchiteli.ru
www.samouchiteli.ru
12
.
.
.
.
1, 2 4 , . . .
3a. .
3b. .
3c. . .
. , , . , , . :
.
: , .
, .
.
. , ++.
1.2. C++
++ , , , . ++ , ++ .
++: cout
www.samouchiteli.ru
13
. cout , ,
www.samouchiteli.ru
14
, . -, readIn(), sort(), compact() print().
: void compact() { cout
www.samouchiteli.ru
15
}
iostream / ( : ). cout, . #include , iostream. ( 1.3.)
#include
using namespace std;
using. , ++ (, cout), std , , . ( 2.7 8.5.)1
, prog1.C, . UNIX :
$ CC prog1.C
$ . CC ++, UNIX-. .
, , .
1 ++ . , . , , , , using .
#include using namespace std; void readIn() { cout
www.samouchiteli.ru
16
, . :
. ,
++, . : }
. ++ . , 10 . "hello", , . , , .
. , , - , . . .
, . .
. , ,
: print()
++ , , . . .
: bool found = false;
int main( { // ')' readIn(): // ':' sort(); compact(); print(); return 0 // ';'
readIn() sort() compact()
int age = 10; double price = 19.99; char delimiter = ' ';
http://www.samouchiteli.ru
www.samouchiteli.ru
17
: age, price, delimiter, found, , , . 10, 19.99, false. . age, int, double, :
age = 33.333;
age 33. ( , 4.14.)
++ ,
, , , , . : vector chapter_titles(20);
current_chapter string, "". chapter_titles 20 .
vector
, string. 20 , :
vector ivec(20);
, , . . ++ . ++, , , , , , ++. /.
++, 2 .
1.2.1.
, .
// string #include string current_chapter = ""; // vector #include
http://www.samouchiteli.ru
www.samouchiteli.ru
18
}
readIn(), sort(), compact() print(). , : 1 0. sort() compact() . - , print() . if. readIn() ,
: }
if : sort() compact() , count 1. if , , .. count 0. , print(). ( if 5.3.) , .
int main() { readIn(); sort(); compact(); print(); return 0;
// readIn() // int int readIn() { ... } // ... int main() { int count = readIn(); // 1, // sort() compact() if ( count > 1 ) { sort(); compact(); } if ( count == 0 ) cout
www.samouchiteli.ru
19
, true false.
: }
while , iterations 5 continue_loop false.
iterations++;
iterations . ( 5.)
1.3.
#include. (#), . , , ( ).
#include .
: #include "my_file.h"
(), , , . ( .) , , , .
#include. ,
int main() { int iterations = 0; bool continue_loop = true; while ( continue_loop != false ) { iterations++; cout
www.samouchiteli.ru
20
, . . :
#ifndef BOOKSTORE_H #endif
#ifndef , BOOKSTORE_H . (BOOKSTORE_H ; .) #endif. #ifndef # endif.
#define BOOKSTORE_H
BOOKSTORE_H. #ifndef, , bookstore.h , .
. : }
DEBUG ,
: { string word;
#define BOOKSTORE_H /* bookstore.h */
int main() { #ifdef DEBUG cout > word ) { #ifdef DEBUG cout
www.samouchiteli.ru
21
vector text; while ( cin >> word ) { text.push_back(word); } // ... }
: { cout > word ) { cout
www.samouchiteli.ru
22
cerr
www.samouchiteli.ru
23
.
++ . , , /* */ . , :
/* */ . : }
, , . , width height . . , . , , .
. .
: }
:
/* * /
/* * C++. * , * - . * Screen 13. */ class Screen { /* */ public: void home(); /* 0,0 */ void refresh ();/* */ private: /* " " */ /* */ /* */ /* ( ). */ /* "private:" */ int height, width;
#include /* /* */ . * " " , * . */ int main() { cout
www.samouchiteli.ru
24
*/ , .
. // . . Screen
: }
. , , /* */, .
1.5. /
++ iostream, /.
, , cin. , , cout. , cerr, . .
/ :
#include
www.samouchiteli.ru
25
cout ifile >> ofile;
?
2.2, , while ( cin >> word ) // ...
, .
( cin >> word )
int v1, v2; // ... cout
www.samouchiteli.ru
26
false, . ( 20.) , cin
cout: }
:
riverrun, past Eve and Adam's
, :
!
( 6 , .)
1.5.1. /
iostream /. , , . , :
#include
, ofstream:
ofstream outfile("name-of-file");
, , :
#include #include int main () { string word; while ( cin >> word ) cout
www.samouchiteli.ru
27
cerr word ) outfile
www.samouchiteli.ru
28
2. ++
++ . , . , , Array, . , Array. Array vector ++, . ++, , .
2.1.
1, ++
, : float fval = 3.14159;
: , , , .. int ival3 = ival2 - ival; // dval = fval * ival; // ival = ival3 / 2; // bool result = ival2 == ival3; // result = ival2 + ival != ival3; // result = fval + ival2 < dval; // result = ival > ival2; //
++ , , . ( vector 2.7.)
// ival // ival 1024 int ival = 1024; // dval // dval 3.14159 double dval = 3.14159; // fval // fval 3.14159
int ival2 = ival1 + 4096; //
http://www.samouchiteli.ru
www.samouchiteli.ru
29
. ( 2.2.)
. ,
0 1 1 2 3 5 8 13 21
9 . ( , .)
, ++:
int fibon[9] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 };
fibon . int, () 9. 0, 21. () , . , :
int first_elem = fibon[1];
: ++ ( ) 0, 1 , 0. ,
, : fibon[9]; // ...
fibon 0 8. 1-9 ++.
. , 0 9
: { int ia[10]; int index;
fibon[0]; // fibon[1]; // ... fibon[8]; //
int main()
http://www.samouchiteli.ru
www.samouchiteli.ru
30
for (index=0; index=0; --index) cout
www.samouchiteli.ru
31
array0[index] = array1[index];
. , . , . , ++ - . .
2.1
, ? , ?
2.2
?
2.2.
- , ++. - , .
++ , , . . , , , . , : . , , .
. , ival
int ival = 1024;
, int, ival 1024. , .
ival : , 1024 , , . . :
int ival2 = ival + 1;
int array0[10]; array1[10]; ... array0 = array1; // : for (int index=0; index
www.samouchiteli.ru
32
, ival: 1 ival2 , 1025. , ?
++ , . , ival, :
int *pint; // int
, &. . pint
ival: pint = &ival; // pint ival
, pint (ival ), , . *. ival, :
*pint = *pint + 1; // ival
,
ival = ival + 1; // ival
: ival . , . .
:
, , . , , ;
. . . , . new delete.
new . :
int *pint = new int(1024);
int *pint;
http://www.samouchiteli.ru
www.samouchiteli.ru
33
new int, 1024 . pint. , .. .
new , :
int *pia = new int[4];
int. , new .
, new , . pint, pia , pint int, pia int. , . delete, ,
new, : delete[] pia;
, ? , , , . . - (, , ). , , .
, , , . 8.4 . , Array, , .
2.3
: (d) int *pi3 = new int[1024];
2.4
// delete pint; //
(a) int ival = 1024; (b) int *pi = &ival; (c) int *pi2 = new int(1024);
http://www.samouchiteli.ru
www.samouchiteli.ru
34
? ? (, ([]) pia.
3.9.2.) delete[] pia;
2.3.
, ++. int. .
, , . , , . , :
1. . ;
2. ;
3. , : ; ; , , ;
4. . , - : - .
5. , , : . ( , .)
6. .
7. . .
8. . , , ++.
int *pi = new int(10); int *pia = new int[10]; while ( *pi < 10 ) { pia[*pi] = *pi; *pi = *pi + 1; } delete pi;
http://www.samouchiteli.ru
www.samouchiteli.ru
35
, , . ++?
: };
class, public private ++, classname , . IntArray: . , Array. , . , .
, : IntArray myArray; // IntArray IntArray *pArray = new IntArray;
: (, class) , .
. // class IntArray;
public, private protected. ( protected.) , , , , , . , , - , -, . IntArray:
class classname { public: // private: // ,
// IntArray
// IntArray
http://www.samouchiteli.ru
www.samouchiteli.ru
36
int find (int value) const; // #3c private: // , // ... }
, , , . const, , . , .
- (, min()) . , (.), , (->) . , , IntArray,
:
// myArray IntArray, :
int min_val = pArray->min();
(, , . -, . .)
, . IntArray:
class IntArray { public: // : #2b bool operator== (const IntArray&) const; bool operator!= (const IntArray&) const; // : #2a IntArray& operator= (const IntArray&); int size() const; // #1 void sort(); // #4 int min() const; // #3a int max() const; // #3b // find // // -1,
// min_val
int min_val = myArray.min();
http://www.samouchiteli.ru
www.samouchiteli.ru
37
IntArray myrray0, myArray1;
: cout
www.samouchiteli.ru
38
_size , . , - size(), _size. _size, - size() . .
, , . (,
_size public): int array_size = array.size(); array_size = array._size;
, , , . , ?
, . ++ (inline) . , . ( , , ++. , .)
. : // ...
size() _size . , , :
for (int index=0; index
www.samouchiteli.ru
39
-, . , .
- , , , . ( , - -.) , . : ?
++ . , / . . , , . , . . min(). ( ,
-.) string min (string,string);
. . ( 9 .)
, IntArray.
: }
IntArray (int sz = DefaultArraySize);
, . ( explicit.) int,
// min() // #include int min (const int *pia,int size); int min (int, int); int min (const char *str); char min (string);
class IntArray { public: explicit IntArray (int sz = DefaultArraySize); IntArray (int *array, int array_size); IntArray (const IntArray &rhs); // ... private: static const int DefaultArraySize = 12;
http://www.samouchiteli.ru
www.samouchiteli.ru
40
IntArray array1(1024);
1024 . , :
IntArray array2;
DefaultArraySize. ( static DefaultArraySize: 13.5. , .)
: }
. , . ? -, new : . ( 2.6 , .) -, sz - , .
? , (::):
IntArray::IntArray(int sz);
, - ( ) . , IntArray, . ( 8; 13.9.)
IntArray IntArray . : .
IntArray : IntArray iA3(ia,10);
IntArray::IntArray (int sz) { // _size = sz; ia = new int[_size]; // for (int ix=0; ix
www.samouchiteli.ru
41
. ( , .) }
. IntArray IntArray. :
IntArray array; // : IntArray ia1 = array; IntArray ia2 (array);
IntArray, -
: { // _size = rhs._size; ia = new int[_size]; // for (int ix=0; ix_size. ( 3.6.)
, . , . , . , init():
IntArray::IntArray (int *array, int sz) { // _size = sz; ia = new int[_size]; // for (int ix=0; ix
www.samouchiteli.ru
42
{ init (rhs._size,rhs.ia); }
- , , . , (~). , . , . IntArray - , . ( 14.)
IntArray: public: // explicit IntArray (int sz = DefaultArraySize); IntArray (int *array, int array_size); IntArray (const IntArray &rhs); // ~IntArray() { delete[] ia; } // ... private: // ... };
class IntArray { public: explicit IntArray (int sz = DefaultArraySize); IntArray (int *array, int array_size); IntArray (const IntArray &rhs); // ... private: void init (int sz,int *array); // ... }; // , void IntArray::init (int sz,int *array) { _size = sz; ia = new int[_size]; for (int ix=0; ix
www.samouchiteli.ru
43
IntArray. , IntArray ,
, : int last_pos = array.size()-1; int temp = array[0]; array[0] = array[last_pos]; array[last_pos] = temp;
.
, : }
, , , /. , , , . , . , . ( 15, 3.15 .)
, , , - , . IntArray IntArray.h. , IntArray, #include. - , , , ++ . . (, ++ ) IntArray.C. 2.5
++ . (), ; , . , . , , ,
IntArray array;
#include int& IntArray::operator[] (int index) { assert (index >= 0 && index < _size); return ia[index];
http://www.samouchiteli.ru
www.samouchiteli.ru
44
. ( ):
(a)
(b)
(c)
(d)
(e)
(f)
2.6
, , . ? , : , . , , - .
2.7
. , . ? ? ?
2.4. -
. , , , , . , IntArray , , min(), max(), find(), . , .
. , ? ?
: . , , . , , . , , .
, , , , IntArray. , ,
http://www.samouchiteli.ru
www.samouchiteli.ru
45
. , ,
: class IntSortedArray { ... };
:
, . . , , , ;
- , ,
: void process_array (IntSortedArray&);
. , , , , , .
- . . (, IntArrayRC IntArray), -, -. IntArrayRC , IntArray, , .
++ , , , - , . , (.. - ). , , , , , . . . swap(), . IntArray:
// class IntArray { ... }; // class IntArrayRC { ... }; //
void process_array (IntArray&); void process_array (IntArrayRC&);
http://www.samouchiteli.ru
www.samouchiteli.ru
46
// - string IntArray string str(" IntArray!"); swap (str,0,10);
-. , swap() . , swap() IntArrayRC:
swap (iarc,0,10);
IntArrayRC,
swap (ias,0,10);
IntSortedArray. ++.
IntArray . ? . , . , , , virtual. , .
(, ), . (public) (private). , , . , , -,
#include void swap (IntArray &ia, int i, int j) { int temp ia[i]; ia[i] = ia[j]; ia[j] = temp; } // swap: IntArray ia; IntArrayRC iarc; IntSortedArray ias; // - ia IntArray swap (ia,0,10); // - iarc IntArray swap (iarc,0,10); // - ias IntArray swap (ias,0,10);
http://www.samouchiteli.ru
www.samouchiteli.ru
47
. , - . , , (protected). , , -, . ( .)
IntArray: virtual int& operator[](int index) { return ia[index]; } virtual void sort(); virtual int min() const; virtual int max() const; virtual int find (int value) const; protected: static const int DefaultArraySize = 12; void init (int sz; int *array); int _size; int *ia; }
- - , . , .
, , , . IntArray .
, - . . , IntArrayRC. size() , , .
class IntArray { public: // explicit IntArray (int sz = DefaultArraySize); IntArray (int *array, int array_size); IntArray (const IntArray &rhs); // virtual ~IntArray() { delete[] ia; } // : bool operator== (const IntArray&) const; bool operator!= (const IntArray&) const; // : IntArray& operator= (const IntArray&); int size() const { return _size; }; // ...
http://www.samouchiteli.ru
www.samouchiteli.ru
48
. , . ( ) ,
. : { for (int ix=0; ix
www.samouchiteli.ru
49
, IntArrayRC IntArray, , . public , , . IntArrayRC IntArray, , , swap(). , IntArrayRC IntArray.
: }
check_range(): }
( assert() 1.3.) , ? , - - , , assert(), . ? , , . , , , .
, IntArrayRC , . , ? .
, , ++. , .
. IntArrayRC : IntArrayRC iarc(ia,8);
IntArrayRC::operator[]( int index ) { check_range( index ); return _ia[ index ];
#include inline void IntArrayRC::check_range(int index) { assert (index>=0 && index < _size);
int ia[] = {0,1,1,2,3,5,8,13};
http://www.samouchiteli.ru
www.samouchiteli.ru
50
ia 8 IntArray. .
IntArrayRC: : IntArray( iar, sz ) {}
( 14 17. , IntArrayRC.) , , . , , . , . IntArrayRC, . , , , , : , . , IntArrayRC, .
IntArrayRC IntArrayRC.h. , IntArrayRC.C . , IntArray IntArrayRC:
inline IntArrayRC::IntArrayRC( int sz ) : IntArray( sz ) {} inline IntArrayRC::IntArrayRC( const int *iar, int sz )
http://www.samouchiteli.ru
www.samouchiteli.ru
51
}
:
swap() with IntArray ia1 swap() with IntArrayRC ia2 Assertion failed: ix >= 0 && ix < _size, file IntArrayRC.h, line 19
2.8
. , IntArrayRC IntArray, ,
.. ?
(i)
#include #include "IntArray.h" #include "IntArrayRC.h" void swap( IntArray &ia, int ix, int jx ) { int tmp = ia[ ix ]; ia[ ix ] = ia[ jx ]; ia[ jx ] = tmp; } int main() { int array[ 4 ] = { 0, 1, 2, 3 }; IntArray ia1( array, 4 ); IntArrayRC ia2( array, 4 ); // : size-1 // IntArray cout
www.samouchiteli.ru
52
2.9
,
, , :
(h) is_on_loan(); // 2.10
, . , . , . ?
2.11
. , , , - , , . , , . ?
2.12
, , , , , . . , . .
, .
(a) rotate(); (b) print(); (c) size(); (d) DateBorrowed(); // (e) rewind(); (f) borrower(); // (g) is_late(); //
(a)
(b)
(c)
(d) _
(e) __
http://www.samouchiteli.ru
www.samouchiteli.ru
53
(f) _
2.5.
IntArray . . , double , .
++ . . - , . Array, IntArray int -. -, int, double string. , .
Array: };
template , , (). elemType; class , .
- Array elemType , :
template class Array { public: explicit Array( int sz = DefaultArraySize ); Array( const elemType *ar, int sz ); Array( const Array &iA ); virtual ~Array() { delete[] _ia; } Array& operator=( const Array & ); int size() const { return _size; } virtual elemType& operator[]( int ix ) { return _ia[ix]; } virtual void sort( int,int ); virtual int find( const elemType& ); virtual elemType min(); virtual elemType max(); protected: void init( const elemType*, int ); void swap( int, int ); static const int DefaultArraySize = 12; int _size; elemType *_ia;
http://www.samouchiteli.ru
www.samouchiteli.ru
54
}
Array: Array da(array_size); Array ca(array_size);
, ? Array, elemType , . , :
// Array ia(array_size); int _size; int *_ia;
, IntArray.
: int _size; double *_ia;
#include #include "Array.h" int main() { const int array_size = 4; // elemType int Array ia(array_size); // elemType double Array da(array_size); // elemType char Array ca(array_size); int ix; for ( ix = 0; ix < array_size; ++ix ) { ia[ix] = ix; da[ix] = ix * 1.75; ca[ix] = ix + 'a'; } for ( ix = 0; ix < array_size; ++ix ) cout
www.samouchiteli.ru
55
// Array ca(array_size); int _size; char *_ia;
-? - elemType , , - . ( 16.8.)
:
[ 0 ] ia: 0 ca: a da: 0 [ 1 ] ia: 1 ca: b da: 1.75 [ 2 ] ia: 2 ca: c da: 3.5 [ 3 ] ia: 3 ca: d da: 5.25
.
ArrayRC: #include "Array.h" template class ArrayRC : public Array { public: ArrayRC( int sz = DefaultArraySize ) : Array( sz ) {} ArrayRC( const ArrayRC& r ) : Array( r ) {} ArrayRC( const elemType *ar, int sz ) : Array( ar, sz ) {} elemType& ArrayRC::operator[]( int ix ) { assert( ix >= 0 && ix < Array::_size ); return _ia[ ix ]; } private: // ... };
- elemType , .
ArrayRC ia_rc(10);
, IntArrayRC .
. , swap( ia1, 1, ia1.size() );
, swap() .
#include
// swap()
http://www.samouchiteli.ru
www.samouchiteli.ru
56
template inline void swap( Array &array, int i, int j ) { elemType tmp = array[ i ]; array[ i ] = array[ j ]; array[ j ] = tmp; }
swap() ,
. , Array ArrayRC: #include "Array.h" #include "ArrayRC.h" template inline void swap( Array &array, int i, int j ) { elemType tmp = array[ i ]; array[ i ] = array[ j ]; array[ j ] = tmp; } int main() { Array ia1; ArrayRC ia2; cout
www.samouchiteli.ru
57
(e) Array< Pstring > aps(1024); 2.14
, : }
2.15
: public: explicit Example2 (elemType val=0) : _val(val) {}; bool min(elemType value) { return _val < value; } void value(elemType new_val) { _val = new_val; } void print (ostream &os) { os pri(1024); (b) Array< Array > aai(1024); (c) Array< complex< double > > acd(1024); (d) Array< Status > as(1024);
class example1 { public: example1 (double min, double max); example1 (const double *array, int size); double& operator[] (int index); bool operator== (const example1&) const; bool insert (const double*, int); bool insert (double); double min (double) const { return _min; }; double max (double) const { return _max; }; void min (double); void max (double); int count (double value) const; private: int size; double *parray; double _min; double _max;
template class Example2 {
http://www.samouchiteli.ru
www.samouchiteli.ru
58
ostream& operator
www.samouchiteli.ru
59
2.6.
, : , - . , , , , .
++ . , , . , , .
:
, . , - , . ++ throw.
: }
, . . . , , , , . ++ catch. :
catch (string exceptionMsg) { log_message (exceptionMsg); return false; }
catch- , , try. try- catch-,
. : { int *pstats = new int [4];
if ( !infile ) { string errMsg(" : "); errMsg += fileName; throw errMsg;
int* stats (const int *ia, int size)
http://www.samouchiteli.ru
www.samouchiteli.ru
60
try { pstats[0] = sum_it (ia,size); pstats[1] = min_val (ia,size); pstats[2] = max_val (ia,size); } catch (string exceptionMsg) { // } catch (const statsException &statsExcp) { // } pstats [3] = pstats[0] / size; do_something (pstats); return pstats; }
stats() try-, . .
1) int *pstats = new int [4]; new . ++ bad_alloc . bad_alloc, .
2) do_something (pstats); do_something(). , , , , , , , . do_something , stats() . , .
, ,
pstats [3] = pstats[0] / size;
, .
, try-. sum_it(), min_val() max_val() , , try- . , , try- . , sum_it() :
throw string (": adump27832");
sum_it() , , try- , , pstats[0] . catch-. catch string:
http://www.samouchiteli.ru
www.samouchiteli.ru
61
}
, catch-, try-.
pstats [3] = pstats[0] / size;
(, , . catch-, , stats().)
: // cerr
www.samouchiteli.ru
62
int elem_cnt; infile >> elem_cnt; int *pi = allocate_array(elem_cnt); int elem; int index=0; while (cin >> elem) pi[index++] = elem; sort_array(pi,elem_cnt); register_data(pi); return pi; }
2.19
allocate_array(), sort_array() register_data() noMem, int string . alloc_and_init(), try catch . cerr . 2.20
alloc_and_init() , ( , allocate_array(), sort_array() register_data() - alloc_and_init()). string , , .
2.7.
, Array, . ; , - -, , Intel . - , , . , , ,
class Cplusplus_Primer_Third_Edition_Array { ... };
, , . , , !
++ , . , . , , Array:
http://www.samouchiteli.ru
www.samouchiteli.ru
63
}
namespace , Cplusplus_Primer_3E. , , :
namespace IBM_Canada_Laboratory { template class Array { ... }; class Matrix { ... }; } namespace Disney_Feature_Animation { class Point { ... }; template class Array { ... }; }
, ; . , , , (::).
: IBM_Canada_Laboratory::Matrix mat; Disney_Feature_Animation::Point origin(5000,5000);
. . :
// namespace LIB = IBM_Canada_Laboratory; namespace DFA = Disney_Feature_Animation; int main() { LIB::Array ia(1024); }
, . , , . , :
namespace LIB = Cplusplus_Primer_3E; int main() { LIB::Array ia(1024); }
namespace Cplusplus_Primer_3E { template class Array { ... };
Cplusplus_Primer_3E::Array text;
http://www.samouchiteli.ru
www.samouchiteli.ru
64
, , , . , Array Disney_Feature_Animation . :
namespace LIB = Disney_Feature_Animation; int main() { LIB::Array ia(1024); }
, , . using:
#include "IBM_Canada_Laboratory.h" using namespace IBM_Canada_Laboratory; int main() { // IBM_Canada_Laboratory::Matrix Matrix mat(4,4); // IBM_Canada_Laboratory::Array Array ia(1024); // ... }
IBM_Canada_Laboratory . , ( using):
#include "IBM_Canada_Laboratory.h" using namespace IBM_Canada_Laboratory::Matrix; // Matrix int main() { // IBM_Canada_Laboratory::Matrix Matrix mat(4,4); // : IBM_Canada_Laboratory::Array Array ia(1024); // ... }
, ++ std. , :
#include
// : string string current_chapter = " ++";
http://www.samouchiteli.ru
www.samouchiteli.ru
65
using: #include using namespace std; // Ok: string string current_chapter = " ++";
, , ,
. : // : std::string current_chapter = " ++"; using: #include using namespace std::string; // Ok: string string current_chapter = " ++";
.
. , , , ++. ( using- ++ 8.6.)
: String, Stack, List Stack. Cplusplus_Primer_3E. ( 8.)
2.21
template class Array { ... }; template void print (Array< EType > ); class String { ... } template class List { ... }; }
:
#include
namespace Exercize {
http://www.samouchiteli.ru
www.samouchiteli.ru
66
const int size = 1024; Array as (size); List il (size); // ... Array *pas = new Array(as); List *pil = new List(il); print (*pas); }
, Exercise. , (a)
(b) using (c)
(d) using
2.8.
, , , . C++ , . C++. , .
, . ,
vector ivec(10); vector svec(10);
Array vector. , , , , . . -, , , sort(), min(), max(), find() , vector : , size() empty(). , , .
vector .
#include
int main() {
http://www.samouchiteli.ru
www.samouchiteli.ru
67
vector vec4(vec2);
, Array, vector .
: extern int getSize(); void mumble() { int size = getSize(); vector vec(size); for (int ix=0; ix
www.samouchiteli.ru
68
vector::iterator iter = vec.begin(); for (int ix=0; iter!=vec.end(); ++iter, ++ix) *iter = ix; // ... }
iter
vector::iterator iter = vec.begin();
vec. iterator typedef vector, int.
++iter
. , :
*iter
++ , vector, - , . :
: find(), find_if(), search(), binary_search(), count(), count_if(); : sort(), partial_sort(), merge(), partition(), rotate(), reverse(), random_shuffle(); : unique(), remove(); : accumulate(), partial_sum(), inner_product(), adjacent_difference(); : generate(), fill(), transform(), copy(), for_each(); : equal(), min(), max(). , , . , ivec, :
sort ( ivec.begin(), ivec.end() );
sort() , :
sort ( ivec.begin(), ivec.begin() + ivec.size()/2 );
. , , :
http://www.samouchiteli.ru
www.samouchiteli.ru
69
int ia[7] = { 10, 7, 9, 5, 3, 7, 1 };
sort():
sort ( ia, ia+7 );
:
sort ( ia, ia+4 );
#include
, vector:
http://www.samouchiteli.ru
www.samouchiteli.ru
70
}
++ . , , . (map). , ,
, : #include #include "TelephoneNumber.h"
#include #include #include int ia[ 10 ] = { 51, 23, 7, 88, 41, 98, 12, 103, 37, 6 }; int main() { vector< int > vec( ia, ia+10 ); vector::iterator it = vec.begin(), end_it = vec.end(); cout
www.samouchiteli.ru
71
map telephone_directory;
( , 6. , . 12 , .)
++, - . .
2.22
:
2.23
min(), . . for
elemType min (const vector &vec);
string pals[] = { "pooh", "tiger", "piglet", "eeyore", "kanga" }; (a) vector svec1(pals,pals+5); (b) vector ivec1(10); (c) vector ivec2(10,10); (d) vector svec2(svec1); (e) vector dvec;
template
http://www.samouchiteli.ru
www.samouchiteli.ru
72
II
, , . , 0, 1. , , , , . , - :
00011011011100010110010000111011 ...
, ( ). ++ . ( 4.)
, - , . 8 , 4 , 32 . . 64- , 16- . , -.
, .
1.
, , 1040 1024 , 1032 1040.
, - , - . 8 ? ( , ), , .
++ : , , : , , . , : , . , , . ++ , . ++ , II .
3 , , . , , , 2.3. 4 , , . 5 . 6 ++ .
http://www.samouchiteli.ru
www.samouchiteli.ru
73
3. ++
, , ++. , , 3.14159 pi, , , . . , , ++. , ++, . ++.
3.1.
++ , , , . char . . short, int long . , , . short , int , long . 32- int long, , . float, double long double ( ) . float ( ) , double ( ) , long double ( ) . char, short, int long , , , (signed) (unsigned). (0 , 1 ), . . 8- signed char -128 127, unsigned char 0 255. , 1, , . , , , . : , , , . . , 0 int, 3.14159 double. , . 20, , :
20 //
024 //
014 //
http://www.samouchiteli.ru
www.samouchiteli.ru
74
0, , 0 0, . .
signed int. long, L ( L, l, : 1). U ( u) unsigned int, UL LU unsigned long. :
128u 1024UL 1L 8Lu
, , , () . double. float F f, long double - L l, . :
3.14159F 0/1f 12.345L 0.0 3el 1.0E-3E 2. 1.0L
true false bool. . :
'a' '2' ',' ' ' ()
(, ) escape- . ( ):
\n \t \b \v \r \f \a \\ \? \' \"
escape- \ooo, ooo . . ASCII-, :
\7 () \14 ( ) \0 (null) \062 ('2')
http://www.samouchiteli.ru
www.samouchiteli.ru
75
L (, L'a'), wchar_t , , char, , , . , . , . escape-. :
"" ( ) "a" "\nCC\toptions\tfile.[cC]\n" "a multi-line \ string literal signals its \ continuation with a backslash"
, ++ 0 (\0).
'A' , "" : '' \0 ( ). wchar_t, , , , L:
L"a wide string literal"
wchar_t , .
( char wchar_t), . ,
"two" "some"
twosome .
. : "two" L"some"
- , . , , . .
3.1
:
// this is not a good idea
http://www.samouchiteli.ru
www.samouchiteli.ru
76
(c) 3.14, 3.14f, 3.14L 3.2
? comment"
3.2.
, 2 10. : int main() { // a first solution cout
www.samouchiteli.ru
77
, ! , , .
, , . , , . , , , . ! .
. , . ,
. : }
value, pow, res cnt , , . for pow .
, . . , , ,
. { for ( int res = 1; exp > 0; --exp ) res = res * val; return res; }
#include int main() { // objects of type int int value = 2; int pow = 10; cout
www.samouchiteli.ru
78
.
0 15: int main() { int val = 2; int exp = 15; cout
www.samouchiteli.ru
79
ch = ch - '0';
ch . (ch '0'): . : , ch, . l-
. : salary + salary * 0.10 = new_salary;
. , . , ,
, . : // fileName string fileName; // ... fileName // module1.C // fileName // , : // fileName module1.C ifstream input_file( fileName );
++ , . . module1.C , fileName . , fileName.
: // fileName // fileName , // extern string fileName;
// : l- // : - l- 0 = 1; // : - l-
// module0.C
// module1.C
http://www.samouchiteli.ru
www.samouchiteli.ru
80
ifstream input_file( fileName )
, , , - . . ( extern 8.2.) , . , , . . ( 8.2.)
3.2.2.
, , , . . ++ , gosh_this_is_an_impossibly_name_to_type . ++ ; 3.1 .
3.1. C++
asm auto bool break case
catch char class const const_cast
continue default delete do double
dynamic_cast else enum explicit export
extern false float for friend
goto if inline int long
mutable namespace new operator private
protected public register reinterpret_cast return
short signed sizeof static static_cast
struct switch template this throw
true try typedef typeid typename
union unsigned using virtual void
volatile wchar_t while
, :
, index ( : Index , INDEX , #define);
http://www.samouchiteli.ru
www.samouchiteli.ru
81
- , , : birth_date salary;
, , , birth_date, (birth_date), (birthDate). , , , _____ . .
3.2.3.
. : unsigned long distance;
. :
double salary, wage; int month, day, year; unsigned long distance;
. , ++ , . ( new), , .
, . , , , . , . ( 2.3. , 3.11 3.15,
string complex .) // int ival; // string // string project;
double salary; double wage; int month; int day; int year;
int main() {
http://www.samouchiteli.ru
www.samouchiteli.ru
82
// ... }
. ++ ,
: string project = "Fantasia 2000";
, : string project( "Fantasia 2000" );
ival 1024 project "Fantasia 2000".
: day = 07, year = 1955;
( ) , wage salary . , :
// , int bizarre = bizarre;
, .
: int ival = int(); double dval = double();
: vector< int > ivec( 10 );
int ival = 1024;
int ival( 1024 );
double salary = 9999.99, wage = salary + 0.01; int month = 08;
// ival 0, dval - 0.0
// int() 10
http://www.samouchiteli.ru
www.samouchiteli.ru
83
int(). ( vector 2.8. . 3.10 6.)
,
. : #include double price = 109.99, discount = 0.16; double sale_price( price * discount ); string pet( "wrinkles" ); extern int get_value(); int val = get_value(); unsigned abs_val = abs( val );
abs() , . get_value() , . 3.3
? (e) cin >> int input_value;
3.4
l- r-. .
3.5
name student
: vector students;
3.6
++? , :
#include
(a) int car = 1024, auto = 2048; (b) int ival = ival; (c) int ival( int() ); (d) double salary = wage = 9999.99;
(a) extern string name; string name( "exercise 3.5a" ); (b) extern vector students;
http://www.samouchiteli.ru
www.samouchiteli.ru
84
(e) char 1_or_2 = '1'; (f) float Float = 3.14f;
3.7
? int main() { int local_int; string local_class; // ... }
3.3.
2.2. , . , , , , .
, : , , 5. , . , , , :
int, 1000, 1000-1003 ( 32- );
double, 1000, 1000-1007 ( 32- ).
:
5 : (. 7.9).
(a) int double = 3.14159; (b) vector< int > _; (c) string namespase; (d) string catch-22;
string global_class; int global_int;
int *ip1, *ip2; complex *cp; string *pstring; vector *pvec;
http://www.samouchiteli.ru
www.samouchiteli.ru
85
double *dp;
. (. : ip1 ip2). lp long, lp2 long:
long *lp, lp2;
fp float, fp2 :
float fp, *fp2;
(*) .
: string* ps;
: , :
//: ps2 !
string* ps, ps2;
, ps, ps2 , .
0, , . int:
int ival = 1024;
int pi pi2: pi2 = 0;
, :
string *ps;
//pi int *pi = 0; // pi2 ival int *pi2 = &ival; // : pi pi2 ival pi = pi2; // pi2
http://www.samouchiteli.ru
www.samouchiteli.ru
86
pi = ival
,
. : double *ps = &dval;
, , : // : int*
www.samouchiteli.ru
87
*pi = *pi + 1; // ival = ival + 1;
(&) int, int*
int *pi = &ival;
int* ( int), int, .. int**. int** , int. ppi, int*, ival. ival,
ppi .
www.samouchiteli.ru
88
int *iter = &ia[0]; int *iter_end = &ia[10]; while (iter != iter_end) { do_something_with_value (*iter); ++iter; }
3.8
: int *pi1 = &ival, *pi2 = &ival2, **pi3 = 0;
?
? (d) pi2 = *pi1; (h) pi3 = &pi2;
3.9
++,
. , pi = pi + 1024;
, pi . ?
3.10
,
: }
? ?
int ival = 1024, ival2 = 2048;
(a) ival = *pi3; (e) pi1 = *pi3; (b) *pi2 = *pi3; (f) ival = *pi1; (c) ival = pi2; (g) pi1 = ival;
pi = &ival;
int foobar(int *pi) { *pi = 1024; return *pi; } int main() { int *pi2 = 0; int ival = foobar(pi2); return 0;
http://www.samouchiteli.ru
www.samouchiteli.ru
89
3.11
- ++ . , ? , ?
3.4.
++ , , string ++. string , , , . ( , main(). 7.)
3.4.1.
, ++ . , char*.
. : char* strcpy( char*, const char* );
++. :
#include
char, , . ,
const char *st = " \n";
st . , ?
. , 1, . :
// int strlen( const char* ); // int strcmp( const char*, const char* ); //
http://www.samouchiteli.ru
www.samouchiteli.ru
90
while (*st++ ) { ... }
st , . , , , , 0. ++ 1 st . , . , , ( ),
: }
: ( ) , ( , ).
// pc1 char *pc1 = 0; // pc2 const char *pc2 = "";
- . , . : . .
. const char *st = " \n"; int main() { int len = 0; while ( st++ ) ++len; cout
www.samouchiteli.ru
91
.
, . ? const char *st = " \n"; int main() { int len = 0; while ( *st++ ) ++len; cout
www.samouchiteli.ru
92
, st:
const char *p = st;
p , st :
while ( *p++ )
3.4.2. string
, - , . , . , ! . string ++ .
, string:
( ) string. ;
. strcpy();
. ;
. strcmp();
, , . strcat(), , strcpy() strcat();
. strlen();
, .
: return;
char str = 0; //... if ( ! str || ! *str )
http://www.samouchiteli.ru
www.samouchiteli.ru
93
string ++ ( , 6). .
string, :
#include
, string
: string st( " \n" );
- size() (
).
:
string st2; //
, ? , 0:
if ( ! st.size() ) // :
empty(), true false :
if ( st.empty() ) // :
string :
string st3( st );
st3 st. , ? (==):
if ( st == st3 ) //
#include
cout
www.samouchiteli.ru
94
? :
st2 = st3; // st3 st2
(+) (+=). :
string s1( "hello, " ); string s2( "world\n" );
, , :
string s3 = s1 + s2;
s2 s1, :
s1 += s2;
string , . , , , , string:
const char *pc = ", "; string s2( "world" ); string s3 = s1 + pc + s2 + "\n";
, , string. string:
string s1; const char *pc = "a character array"; s1 = pc; //
, , . :
char *str = s1; //
, - c_str():
char *str = s1.c_str(); //
string s1( "hello" );
http://www.samouchiteli.ru
www.samouchiteli.ru
95
c_str() , string , . char *str . c_str() , ,
const char *
( const). :
const char *str = s1.c_str(); //
string, , . , , ,
: str[ ix ] = '_';
, string . , . , - replace():
replace( str.begin(), str.end(), '.', '_' );
replace() , 2.8 12. begin() end(), , , , .
3.12
: (h) pc = st; (l) *pc = ival;
3.13
:
string str( "fa.disney.com" ); int size = str.size(); for ( int ix = 0; ix < size; ++ix ) if ( str[ ix ] == '.' )
(a) char ch = "The long and winding road"; (b) int ival = &ch; (c) char *pc = &ival; (d) string st( &ch ); (e) pc = 0; (i) pc = '0'; (f) st = pc; (j) st = &ival; (g) ch = pc[0]; (k) ch = *pc;
http://www.samouchiteli.ru
www.samouchiteli.ru
96
++cnt; while ( *st++ ) ++cnt;
3.14
.
, string: #include #include int main() { int errors = 0; const char *pc = "a very long literal string"; for ( int ix = 0; ix < 1000000; ++ix ) { int len = strlen( pc ); char *pc2 = new char[ len + 1 ]; strcpy( pc2, pc ); if ( strcmp( pc2, pc )) ++errors; delete [] pc2; } cout
www.samouchiteli.ru
97
}
?
, . ? ?
3.15
- string, ? .
3.5. const
: ... ;
512 . . 512? ? ...
. , 10 000 , 512 4% . , 80% 512 1024. , , ?
: 512. , bufSize, : , .
index < bufSize
bufSize 400 320 .
! 512 . for ( int index = 0; index < bufSize; ++index ) // ...
: bufSize l-, , . (=) (==):
// bufSize
for ( int index = 0; index < 512; ++index )
int bufSize = 512; // // ...
http://www.samouchiteli.ru
www.samouchiteli.ru
98
if ( bufSize = 1 ) // ...
bufSize 1, . , .
const .
const int bufSize = 512; //
512, : : , ,
. if ( bufSize = 0 ) ...
, . :
const double pi; // :
. . ?
? double *ptr = &minWage;
? minWage , . , :
*ptr += 1.40; // minWage!
, . . .
, ? . , const:
const double *cptr;
// :
const double minWage = 9.60; // ? ?
http://www.samouchiteli.ru
www.samouchiteli.ru
99
cptr const double. ,
, , . : *pc = 3.14159; //
. , :
pc = &dval;
. dval , dval pc. (- , , .)
. , , ,
. : int strcmp( const char *str1, const char *str2 );
( 7, .)
. ( !). , . :
int errNumb = 0; int *const currErr = &errNumb;
curErr . , , . curErr:
const double *pc = 0; const double minWage = 9.60; // : minWage pc pc = &minWage; double dval = 3.14; // : minWage pc // dval pc = &dval; // dval = 3.14159; //
// //
http://www.samouchiteli.ru
www.samouchiteli.ru
100
if ( *curErr ) { }
:
curErr = &myErNumb; //
. const double *const pi_ptr =
, pi_ptr, .
3.16
. ? (c) const int *pic;
3.17
? ? (e) const int *const cpic = ⁣
3.18
,
. . (c) cpi = pic; (f) ic = *cpic;
do_something();
errorHandler(); *curErr = 0; // : errNumb
const double pi = 3.14159;
(a) int i; (d) int *const cpi; (b) const int ic; (e) const int *const cpic;
(a) int i = -1; (b) const int ic = i; (c) const int *pic = ⁣ (d) int *const cpi = ⁣
(a) i = ic; (d) pic = cpic; (b) pic = ⁣ (i) cpic = ⁣
http://www.samouchiteli.ru
www.samouchiteli.ru
101
3.6.
, , . , , . , . . .
(&)
. . : int &refVal2;
, , , , .
: int *&ptrVal2 = pi;
, , ( ). refVal,
ival , refVal. refVal = min_val;
. . :
refVal += 2;
int ival = 1024; // : refVal - ival int &refVal = ival; // :
int ival = 1024; // : refVal int, int* int &refVal = &ival; int *pi = &ival; // : ptrVal -
int min_val = 0; // ival min_val, // refVal min_val
http://www.samouchiteli.ru
www.samouchiteli.ru
102
2 ival , refVal.
int ii = refVal;
ii ival,
int *pi = &refVal;
pi ival. , (&) ( ,
). : int &rval3 = ival3, &rval4 = ival2;
(, , ),
, . : const double &dr = dval + 1.0;
const, . , , , . .
: , . ,
. , : const int &ri = dval;
:
// int int ival = 1024, ival2 = 2048; // int &rval = ival, rval2 = ival2; // , int inal3 = 1024, *pi = ival3, &ri = ival3; //
double dval = 3.14159; // const int &ir = 1024; const int &ir2 = dval;
double dval = 1024;
http://www.samouchiteli.ru
www.samouchiteli.ru
103
const int &ri = temp;
ri, dval, temp. dval , . , const. , . ,
: int *&pi_ref = &ival;
const : const int *&pi_ref = &ival;
? , , pi_ref int.
, : int *const &piref = &ival;
. -, . -, , , . . :
int *pi = 0;
pi , , pi .
const int &ri = 0;
:
int temp = dval;
const int ival = 1024; // :
const int ival = 1024; //
const int ival = 1024; //
http://www.samouchiteli.ru
www.samouchiteli.ru
104
const int &ri = temp;
, : pi = pi2;
ival, pi, , pi ival2. pi, pi2 ival2.
: ri = ri2;
ival , ri - ival. ++ ,
. : Matrix operator+( const Matrix&, const Matrix& );
- -? : while (get_next_value( ival )) ...
:
int &next_value = ival;
( 7.)
3.19
int temp = 0;
int ival = 1024, ival2 = 2048; int *pi = &ival, *pi2 = &ival2;
int &ri = ival, &ri2 = ival2;
// // next_value bool get_next_value( int &next_value ); //
int ival;
http://www.samouchiteli.ru
www.samouchiteli.ru
105
? . ? (i) const int &ival2 = 1; (j) const int &*prval2 = &ival;
3.20
(
)? (d) *prval2 = ival2;
3.21
: pi = &rval;
3.7. bool
bool : true false. : else cout > next_word ) if ( next_word == search_word ) found = true; // ... // : if ( found == true ) if ( found ) cout
www.samouchiteli.ru
106
short bool found = false;
bool int. true 1,
false 0. : }
bool. 0 false, true: // : found == true
3.8.
, . , : , , .
, : const int append = 3;
:
//
bool found = false; int occurrence_count = 0; while ( /* mumble */ ) { found = look_for( /* something */ ); // found 0 1 occurrence_count += found;
// extern int find( const string& ); bool found = false; if ( found = find( "rosebud" )) // : found == true // extern int* find( int value ); if ( found = find( 1024 ))
const int input = 1; const int output = 2;
http://www.samouchiteli.ru
www.samouchiteli.ru
107
open_file( "Phoenix_and_the_Crane", append );
, , , , open_file() 1, 2 3. . :
enum open_modes{ input = 1, output, append };
open_modes. 1, 2 3, . , :
void open_file( string file_name, open_modes om );
input, output append . . open_modes ( ) , . :
open_file( "Phoenix and the Crane", append );
, ( ), . ,
, : open_file( "Jonah", 1 );
open_modes,
: om = append; open_file( "TailTell", om );
. :
cout
www.samouchiteli.ru
108
1 3
, , , , .
, :
www.samouchiteli.ru
109
}
int. : int chunk_size = array_size * pt2w;
3.9.
2.1. , . :
int ival;
ival int,
int ia[ 10 ];
int. , , :
ival = ia[ 2 ];
ival ia 2.
ia[ 7 ] = ival;
7 ival. , . ( 1) . , , , . :
void mumble() { Points pt3d = point3d; // : pt2d == 3 // : pt3w int Points pt3w = 3; // : polygon Points pt3w = polygon; // : Points pt3w = pt3d;
const int array_size = 1024; // : pt2w int
http://www.samouchiteli.ru
www.samouchiteli.ru
110
int test_scores[ get_size() ];
buf_size max_files , input_buffer fileTable . staff_size ( 27), , salaries[staff_size] . ( staff_size salaries.) max_files-3 , , fileTable[max_files-3] . 0, 10 1 10, 0 9.
: }
,
, : int ia[ array_size ] = { 0, 1, 2 };
, :
: int ia[] = { 0, 1, 2 };
extern int get_size(); // buf_size max_files const int buf_size = 512, max_files = 20; int staff_size = 27; // : char input_buffer[ buf_size ]; // : : 20 - 3 char *fileTable[ max_files-3 ]; // : double salaries[ staff_size ]; // :
int main() { const int array_size = 10; int ia[ array_size ]; for ( int ix = 0; ix < array_size; ++ ix ) ia[ ix ] = ix;
const int array_size = 3;
// 3
http://www.samouchiteli.ru
www.samouchiteli.ru
111
, , . . , , .
, : int ia[ array_size ] = { 0, 1, 2 };
, .
. , const char cal2[] = "C++";
ca1 3, ca2 4 (
). : const char ch3[ 6 ] = "Daniel";
, . ,
. : }
, :
// ia ==> { 0, 1, 2, 0, 0 } const int array_size = 5;
const char cal[] = {'C', '+', '+' };
// : "Daniel" 7
const int array_size = 3; int ix, jx, kx; // : int* int *iar [] = { &ix, &jx, &kx }; // error: int &iar[] = { ix, jx, kx }; int main() { int ia3{ array_size ]; // // : ia3 = ia; return 0;
http://www.samouchiteli.ru
www.samouchiteli.ru
112
}
,
. : ia2[ get_index() ] = someVal;
, ++ , . , . . , , , , .
3.22
? . (c) int ia[ 4 * 7 - 14 ];
3.23
. : }
const int array_size = 7; int ia1[] = { 0, 1, 2, 3, 4, 5, 6 }; int main() { int ia3[ array_size ]; for ( int ix = 0; ix < array_size; ++ix ) ia2[ ix ] = ia1[ ix ]; return 0;
int someVal, get_index();
(a) int ia[ buf_size ]; (d) int ia[ 2 * 7 - 14 ] (b) int ia[ get_size() ]; (e) char st[ 11 ] = "fundamental";
int main() { const int array_size = 10; int ia[ array_size ]; for ( int ix = 1; ix
www.samouchiteli.ru
113
3.9.1.
++ , . :
int ia[ 4 ][ 3 ];
(4) , (3) . ia .
: };
, , , , . , :
int ia[4][3] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
. :
int ia[ 4 ][ 3 ] = { {0}, {3}, {6}, {9} };
, . , 0.
int ia[ 4 ][ 3 ] = { 0, 3, 6, 9 };
( ).
: }
int ia[ 4 ][ 3 ] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 }
int main() { const int rowSize = 4; const int colSize = 3; int ia[ rowSize ][ colSize ]; for ( int = 0; i < rowSize; ++i ) for ( int j = 0; j < colSize; ++j ) ia[ i ][ j ] = i + j j;
http://www.samouchiteli.ru
www.samouchiteli.ru
114
ia[ 1, 2 ]
++, , . 1 2. , 2 (. 4.2). ia[1,2] ia[2]. .
3.9.2.
:
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 };
?
ia;
: &ia[0]
: ia[0];
, :
&ia[1];
,
ia+1;
. ,
: ia[1];
ia;
// *ia;
*(ia+1);
http://www.samouchiteli.ru
www.samouchiteli.ru
115
:
*ia+1
*(ia+1);
, ( 4.13). ia , 1. . ,
, . :
int *pend = ia + 9; }
pbegin . 1, . , ? pend , ia. pbegin pend, , .
,
, : }
#include int main() { int ia[9] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 }; int *pbegin = ia;
while ( pbegin != pend ) { cout
www.samouchiteli.ru
116
, , int. :
( 2.5): }
print() : }
. ( 3.4), . , . , ,
: };
( 12; .)
++ , . ( 2.8.) vector, - .
#inc1ude template void print( elemType *pbegin, elemType *pend ) { while ( pbegin != pend ) { cout
www.samouchiteli.ru
117
3.10. vector
vector (. 2.8) . , . , . , 7.8. vector, string, ++. :
#include
, STL. vector , . :
vector< int > ivec( 10 );
:
int ia[ 10 ];
: }
, size(), ,
, empty(). : }
. 0.
void simp1e_examp1e() { const int e1em_size = 10; vector< int > ivec( e1em_size ); int ia[ e1em_size ]; for ( int ix = 0; ix < e1em_size; ++ix ) ia[ ix ] = ivec[ ix ]; // ...
void print_vector( vector ivec ) { if ( ivec.empty() ) return; for ( int ix=0; ix< ivec.size(); ++ix ) cout
www.samouchiteli.ru
118
, (. 2.3). , :
vector< int > ivec( 10, -1 );
-1. :
int ia[ 6 ] = { -2, -1, , 1, 2, 1024 };
vector .
: vector< int > ivec( ia, ia+6 );
ivec ia , .
, : vector< int > ivec( &ia[ 2 ], &ia[ 5 ] );
vector
. : }
STL6, . , :
vector< string > text;
6 STL Standard Template Library. ++ vector, string , STL.
// 6 ia ivec
// 3 : ia[2], ia[3], ia[4]
vector< string > svec; void init_and_assign() { // vector< string > user_names( svec ); // ... // svec = user_names;
http://www.samouchiteli.ru
www.samouchiteli.ru
119
. , push_back() . ,
: }
: cout word ) { text.push_back( word ); // ...
cout
www.samouchiteli.ru
120
. , :
vector ia( 10 );
, . , ,
: ivec.push_back( ia[ ix ] );
ivec ia, ivec 14. STL, , . ( 6.)
3.24
? (e) vector< string > svec( 10, string( "null" ));
3.25
: const vector &ivec );
is_equal() . . , , , true, false. . main(), is_equal().
3.11. complex
complex . , :
const int size = 7; int ia[ size ] = { 0, 1, 1, 2, 3, 5, 8 }; vector< int > ivec( size ); for ( int ix = 0; ix < size; ++ix )
int ia[ 7 ] = { 0, 1, 1, 2, 3, 5, 8 }; (a) vector< vector< int > > ivec; (b) vector< int > ivec = { 0, 1, 1, 2, 3, 5, 8 }; (c) vector< int > ivec( ia, ia+7 ); (d) vector< string > svec = ivec;
bool is_equa1( const int*ia, int ia_size,
http://www.samouchiteli.ru
www.samouchiteli.ru
121
#include
. .
2 + 3i
2 , 3i .
complex: comp1ex< double > purei2( purei );
complex, vector, , float, double long double, .
complex: };
: complex< double > &ref = *ptr;
, , , , , . ( complex 4.6.)
3.12. typedef
typedef . :
// : 0 + 7-i comp1ex< double > purei( 0, 7 ); // 0: 3 + Oi comp1ex< float > rea1_num( 3 ); // , 0: 0 + 0-i comp1ex< long double > zero; //
complex< double > conjugate[ 2 ] = { complex< double >( 2, 3 ), complex< double >( 2, -3 )
complex< double > *ptr = &conjugate[0];
http://www.samouchiteli.ru
www.samouchiteli.ru
122
typedef int *Pint;
, typedef, ,
: Pint table [ 10 ];
typedef, , , .
, typedef? , . , , (. 3.14), - (. 13.6).
, . typedef . :
typedef char *cstring;
cstr :
extern const cstring cstr;
, :
const char *cstr
. const cstr, char:
char *const cstr;
typedef double wages; typedef vector vec_int; typedef vec_int test_scores; typedef bool in_attendance;
// double hourly, weekly; wages hourly, weekly; // vector vecl( 10 ); vec_int vecl( 10 ); // vector test0( c1ass_size ); const int c1ass_size = 34; test_scores test0( c1ass_size ); // vector< bool > attendance; vector< in_attendance > attendance( c1ass_size ); // int *table[ 10 ];
http://www.samouchiteli.ru
www.samouchiteli.ru
123
3.13. volatile
volatile (, ), , , . , .
volatile const: volatile Screen bitmap_buf;
display_register int. curr_task Task. ixa , . bitmap_buf Screen, . volatile , , . , .
3.14. pair
pair () ++ , - . . :
#inc1ude
,
pair< string, string > author( "James", "Joyce" );
author pair, .
first second: firstBook = "Stephen Hero";
, typedef:
volatile int disp1ay_register; volatile Task *curr_task; volatile int ixa[ max_size ];
string firstBook; if ( Joyce.first == "James" && Joyce.second == "Joyce" )
http://www.samouchiteli.ru
www.samouchiteli.ru
124
Authors musil( "robert", "musi1" );
.
, . }
( pair 6 12.)
3.15.
; string, vector, complex pair, . 2 , - , Array. , , String, , , 2.3. ( 13, 14 15. , . , ++, .)
String String, , . . String . , : size() ; String String ; / . .
class, , . , public () private (). ,
typedef pair< string, string > Authors; Authors proust( "marcel", "proust" ); Authors joyce( "James", "Joyce" );
class EntrySlot; extern EntrySlot* 1ook_up( string ); typedef pair< string, EntrySlot* > SymbolEntry; SymbolEntry current_entry( "author", 1ook_up( "author" )); // ... if ( EntrySlot *it = 1ook_up( "editor" )) { current_entry.first = "editor"; current_entry.second = it;
http://www.samouchiteli.ru
www.samouchiteli.ru
125
, , - . - , , , . -, . _string char, _size int. _size , _string .
: }
#inc1ude class String; istream& operator>>( istream&, String& ); ostream& operator
www.samouchiteli.ru
126
String . 2.3, , / .
String();
, . :
String str1;
str1 . . ,
String str2(" ");
String(const char*);
String str3(str2);
String(const String&);
. , String(const String&), , .
:
String str4(1024);
, int. :
return_type operator op (parameter_list);
operator , op : +, =, ==, [] . ( . 15.) :
char& operator[] (int);
int char. ,
http://www.samouchiteli.ru
www.samouchiteli.ru
127
. String .
- (.)
(->). String: String array[2];
size() : sizes[ 2 ] = array[0].size();
5, 4 0.
, : namel = name2;
- , , . ( size() c_str() .) , , , . , , String.C, (String.h ), :
String object("Danny"); String *ptr = new String ("Anna");
vector sizes( 3 ); // objects (.); // objects 5 sizes[ 0 ] = object.size(); // pointers (->) // ptr 4 sizes[ 1 ] = ptr->size(); // (.) // array[0] 0
String namel( "Yadie" ); String name2( "Yodie" ); // bool operator==(const String&) if ( namel == name2 ) return; else // String& operator=( const String& )
http://www.samouchiteli.ru
www.samouchiteli.ru
128
}
, strcmp() . , 0 . (?:) , . , , , . false, strcmp() , true . ( 4.7.)
, , (inline). , . ( 7.6.) -, , .
, , inline: }
, . == , String.C String.h. String
: }
// : String. // String #inc1ude "String.h" // strcmp() #inc1ude bool // String:: // , operator== // : (const String &rhs) // { if ( _size != rhs._size ) return false; return strcmp( _strinq, rhs._string ) ? false : true;
inline bool String::operator==(const String &rhs) { //
inline bool String::operator==(const char *s) { return strcmp( _string, s ) ? false : true;
http://www.samouchiteli.ru
www.samouchiteli.ru
129
. , , , . . ,
. }
new, delete, String . - , , . (. 7 .) (~) . String. delete, , :
inline String: :~String() { delete [] _string; }
this.
: namel = "Orville Wright";
#include // default constructor inline String::String() { _size = 0; _string = 0; } inline String::String( const char *str ) { if ( ! str ) { _size = 0; _string = 0; } else { _size = str1en( str ); _string = new char[ _size + 1 ]; strcpy( _string, str ); } // copy constructor inline String::String( const String &rhs ) { size = rhs._size; if ( ! rhs._string ) _string = 0; else { _string = new char[ _size + 1 ]; strcpy( _string, rhs._string ); }
String namel( "orville" ), name2( "wilbur" );
http://www.samouchiteli.ru
www.samouchiteli.ru
130
this , name1 .
this , . obj[ 1024 ];
size() this , ptr. this obj. this ( *this),
. ( this 13.4.) return *this; }
: , , . ,
this: if ( this != &rhs ) {
String :
ptr->size();
inline String& String::operator=( const char *s ) { if ( ! s ) { _size = 0; delete [] _string; _string = 0; } else { _size = str1en( s ); delete [] _string; _string = new char[ _size + 1 ]; strcpy( _string, s ); }
inline String& String::operator=( const String &rhs ) { // // namel = *pointer_to_string // this name1, // rhs - *pointer_to_string.
http://www.samouchiteli.ru
www.samouchiteli.ru
131
}
Array,
2.3: }
, . ( 15.2. 20.4 20.5 iostream.) 4095 . setw() , 1, , inBuf. ( 20 setw() .)
: }
inline String& String::operator=( const String &rhs ) { if ( this != &rhs ) { delete [] _string; _size = rhs._size; if ( ! rhs._string ) _string = 0; else { _string = new char[ _size + 1 ]; strcpy( _string, rhs._string ); } } return *this;
#include inline char& String::operator[] ( int elem ) { assert( elem >= 0 && elem < _size ); return _string[ elem ];
#include inline istream& operator>>( istream &io, String &s ) { // : 4096 const int 1imit_string_size = 4096; char inBuf[ limit_string_size ]; // setw() iostream // 1imit_string_size-l io >> setw( 1imit_string_size ) >> inBuf; s = mBuf; // String::operator=( const char* ); return io;
http://www.samouchiteli.ru
www.samouchiteli.ru
132
String. operator
www.samouchiteli.ru
133
}
: , ( 6). :
#include #inc1ude "String.h" int main() { int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, theCnt = 0, itCnt = 0, wdCnt = 0, notVowel = 0; // "The" "It" // operator==( const char* ) String but, the( "the" ), it( "it" ); // operator>>( ostream&, String& ) while ( cin >> buf ) { ++wdCnt; // operator
www.samouchiteli.ru
134
Alice Emma has long flowing red hair. Her Daddy says when the wind blows through her hair, it looks almost alive, 1ike a fiery bird in flight. A beautiful fiery bird, he tells her, magical but untamed. "Daddy, shush, there is no such thing," she tells him, at the same time wanting him to tell her more. Shyly, she asks, "I mean, Daddy, is there?" : 65 the/The: 2 it/It: 1 : 190 a: 22 e: 30 i: 24 : 10 u: 7
3.26
. -, 2.3. , .
3.27
, b, d, f, s, t.
3.28
-,
String, : };
3.29
(+) ,
String. : };