(.. ) . ++ , . 5.1  , , ++ , . , . . . . , ++ float +, -, * .. , . , , . , trunk_module (-), - explosion (), , , - list_of_paragraphs (-). , . . , . - (, ) , (, , ). , , ( " "). : $$5.2 . , . , . - . -, . - . . $$5.3 . , . $$5.4 . . , , , . . . , . $$5.5 . , . , ( ), . , . 5.2  - . , , , , . 5.2.1 - , , , : struct date { int month, day, year; }; date today; void set_date(date*, int, int, int); void next_date(date*); void print_date(const date*); // ... date . , : struct date { int month, day, year; void set(int, int, int); void get(int*, int* int*); void next(); void print(); }; -. , : date today; date my_birthday; void f() { my_birthday.set(30,12,1950); today.set(18,1,1991); my_birthday.print(); today.next(); } - , - : void date::next() { if (++day > 28 ) { // } } - . , . 5.2.2  date, , , date. , : class date { int month, day, year; public: void set(int, int, int); void get(int*, int*, int*); void next(); void print() }; public () . , (private) , -. - - . - , . - , : void date::print() // { cout << month << '/' << day << '/' << year ; } date : void backdate() { today.day--; // } , . (, December, 36, 1985) -, - - . : date . , -. . , . 5.2.3  - , : class X { int m; public: int readm() { return m; } }; void f(X aa, X bb) { int a = aa.readm(); int b = bb.readm(); // ... } readm() m aa.m, - bb.m. - , , . this. , - X this X *const this; , , - . , (*const). , .. this - . X: class X { int m; public: int readm() { return this->m; } }; this . this -, . - , : class dlink { dlink* pre; // dlink* suc; // public: void append(dlink*); // ... }; void dlink::append(dlink* p) { p->suc = suc; // .. p->suc = this->suc p->pre = this; // "this" suc->pre = p; // .. this->suc->pre = p suc = p; // .. this->suc = p } dlink* list_head; void f(dlink* a, dlink* b) { // ... list_head->append(a); list_head->append(b); } , 8. , , this, pre suc. dlink, - dlink::append() . ++ , . - , , , . , , (.. this*), const : class X { int m; public: readme() const { return m; } writeme(int i) { m = i; } }; - const , - - : void f(X& mutable, const X& constant) { mutable.readme(); // mutable.writeme(7); // constant.readme(); // constant.writeme(7); // } , X::writeme() . , . - "" , const. , readme() writeme() : class X { int m; public: readme() const; writeme(int i); }; readme() const . this - X const X *const. , this : class X { int m; public: // ... void implicit_cheat() const { m++; } // void explicit_cheat() const { ((X*)this)->m++; } // }; const , "" . , " " , . , " " , () . , . , , , , - -. , const: class calculator1 { int cache_val; int cache_arg; // ... public: int compute(int i) const; // ... }; int calculator1::compute(int i) const { if (i == cache_arg) return cache_val; // ((calculator1*)this)->cache_arg = i; ((calculator1*)this)->cache_val = val; return val; } , const: struct cache { int val; int arg; }; class calculator2 { cache* p; // ... public: int compute(int i) const; // ... }; int calculator2::compute(int i) const { if (i == p->arg) return p->val; // p->arg = i; p->val = val; return val; } , const , -. , ($$7.1.6). 5.2.4  set_date() - . , , , , . , . , . - , : class date { // ... date(int, int, int); }; , . , : date today = date(23,6,1983); date xmas(25,12,0); // date my_birthday; // , . : class date { int month, day, year; public: // ... date(int, int, int); // , , date(int, int); // , date(int); // date(); // : date(const char*); // }; , ($$4.6.6). , : date today(4); date july4("July 4, 1983"); date guy("5 Nov"); date now; // c date . , - - . , , , , . . date , : " ". class date { int month, day, year; public: // ... date(int d =0, int m =0, y=0); // ... }; date::date(int d, int m, int y) { day = d ? d : today.day; month = m ? m : today.month; year = y ? y : today.year; // // ... } , . , - , , . , , .. 1 . .. (year==-1) 1 . .. (year==1). , , . . , : date d = today; // , , . X , X::X(const X&). $$7.6. 5.2.5  , , , . - , . X ~X (" "). , (. $$3.2.6), . , , , : class char_stack { int size; char* top; char* s; public: char_stack(int sz) { top=s=new char[size=sz]; } ~char_stack() { delete[] s; } // void push(char c) { *top++ = c; } void pop() { return *--top; } }; char_stack , : void f() { char_stack s1(100); char_stack s2(200); s1.push('a'); s2.push(s1.pop()); char ch = s2.pop(); cout << ch << '\n'; } f(), char_stack, 100 s1 200 s2. f() , , . 5.2.6  , . , , , . , , , . . C++ , - , , . - (inline). - , , . , , , char_stack , , ! , . , , . . - inline : class char_stack { int size; char* top; char* s; public: char pop(); // ... }; inline char char_stack::pop() { return *--top; } , -, , ($$R.7.1.2). . 5.3  ? , . , " ", . , , . , . : , , , , .. . : ? . , . . 5.3.1  - , , , . , 3. : struct name { char* string; name* next; double value; }; table ( ): // table.h class table { name* tbl; public: table() { tbl = 0; } name* look(char*, int = 0); name* insert(char* s) { return look(s,1); } }; 3 , . , .. : #include "table.h" table globals; table keywords; table* locals; main() { locals = new table; // ... } table::look(), : #include <string.h> name* table::look(char* p, int ins) { for (name* n = tbl; n; n=n->next) if (strcmp(p,n->string) == 0) return n; if (ins == 0) error(" "); name* nn = new name; nn->string = new char[strlen(p)+1]; strcpy(nn->string,p); nn->value = 1; nn->next = tbl; tbl = nn; return nn; } table , (- ), . , , , , table, : class table { name** tbl; int size; public: table(int sz = 15); ~table(); name* look(char*, int = 0); name* insert(char* s) { return look(s,1); } }; , . , , , . , , . : table::table(int sz) { if (sz < 0) error(" "); tbl = new name*[size = sz]; for ( int i = 0; i<sz; i++) tbl[i] = 0; } table::~table() { for (int i = 0; i<size; i++) { name* nx; for (name* n = tbl[i]; n; n=nx) { nx = n->next; delete n->string; delete n; } } delete tbl; } name, table::~table(). ($$3.13): name* table::look(const char* p, int ins) { int ii = 0; char* pp = p; while (*pp) ii = ii<<1 ^ *pp++; if (ii < 0) ii = -ii; ii %= size; for (name* n=tbl[ii]; n; n=n->next) if (strcmp(p,n->string) == 0) return n; name* nn = new name; nn->string = new char[strlen(p)+1]; strcpy(nn->string,p); nn->value = 1; nn->next = tbl[ii]; tbl[ii] = nn; return nn; } , - , - . . , . , , . , , . ( ), , . . : ++ , ? ? , , , ? - . , , , (.. ) . , "" . , , , . , . , . -, . , ++ , (struct). ++. , ++ , . 6 , $$13.3 , . - , , , . 5.3.2  ( ) , ( ). , . , , . . , , . intset, " ": class intset { int cursize, maxsize; int *x; public: intset(int m, int n); // m 1..n ~intset(); int member(int t) const; // t ? void insert(int t); // t void start(int& i) const { i = 0; } void ok(int& i) const { return i<cursize; } void next(int& i) const { return x[i++]; } }; , . , . , , . , : #include <iostream.h> void error(const char *s) { cerr << "set: " << s << '\n'; exit(1); } intset main(), : , - : int main(int argc, char* argv[]) { if (argc != 3) error(" "); int count = 0; int m = atoi(argv[1]); // int n = atoi(argv[2]); // 1..n intset s(m,n); while (count<m) { int t = randint(n); if (s.member(t)==0) { s.insert(t); count++; } } print_in_order(&s); } argc 3, . , argv[0] , . extern "C" int atoi(const char*) , . , , , . rand: extern "C" int rand(); // : // int randint(int u) // 1..u { int r = rand(); if (r < 0) r = -r; return 1 + r%u; } , -. , , : intset::intset(int m, int n) // m 1..n { if (m<1 || n<m) error(" intset"); cursize = 0; maxsize = m; x = new int[maxsize]; } intset::~intset() { delete x; } , : void intset::insert(int t) { if (++cursize > maxsize) error(" "); int i = cursize-1; x[i] = t; while (i>0 && x[i-1]>x[i]) { int t = x[i]; // x[i] x[i-1] x[i] = x[i-1]; x[i-1] = t; i--; } } , : int intset::member(int t) const // { int l = 0; int u = cursize-1; while (l <= u) { int m = (l+u)/2; if (t < x[m]) u = m-1; else if (t > x[m]) l = m+1; else return 1; // } return 0; // } , , ( , intset, ). , ( intset ?). : start() - , ok() - , , next() - : class intset { // ... void start(int& i) const { i = 0; } int ok(int& i) const { return i<cursize; } int next(int& i) const { return x[i++]; } }; , , . . , . print_in_order: void print_in_order(intset* set) { int var; set->sart(var); while (set->ok(var)) cout << set->next(var) << '\n'; } $$7.8. 5.4  . , ($$5.4.1). , ($$5.4.2) ($$5.4.3), ($$5.4.4). (static), , , ($$5.4.5). , , () ($$5.4.6). 5.4.1  : vector () matrix (). , . , , . , 0 3, 0 3. elem(), . multiply () : vector multiply(const matrix& m, const vector& v); { vector r; for (int i = 0; i<3; i++) { // r[i] = m[i] * v; r.elem(i) = 0; for (int j = 0; j<3; j++) r.elem(i) +=m.elem(i,j) * v.elem(j); } return r; } , . multiply() elem() 4*(1+4*3) . elem() , , , . , elem() , , . multiply vector matrix, , elem(). , . , , . - , - , . , friend (). : class matrix; class vector { float v[4]; // ... friend vector multiply(const matrix&, const vector&); }; class matrix { vector v[4]; // ... friend vector multiply(const matrix&, const vector&); }; - , . , this, . friend . , , . friend , , . multiply, : vector multiply(const matrix& m, const vector& v) { vector r; for (int i = 0; i<3; i++) { // r[i] = m[i] * v; r.v[i] = 0; for ( int j = 0; j<3; j++) r.v[i] +=m.v[i][j] * v.v[j]; } return r; } , - , . -. - : class x { // ... void f(); }; class y { // ... friend void x::f(); }; , . : class x { friend class y; // ... }; - y x. 5.4.2  . :: ( ): class X { int m; public: int readm() const { return m; } void setm(int m) { X::m = m; } }; X::setm() m m, , X::m. :: . :: . read, put, open, -, , . : class my_file {