// ... public: int open(const char*, const char*); }; int my_file::jpen(const char* name, const char* spec) { // ... if (::open(name,flag)) { // open() UNIX(2) // ... } // ... } 5.4.3  . : class set { struct setmem { int mem; setmem* next; setmem(int m, setmem* n) { mem=m; next=n; } }; setmem* first; public: set() { first=0; } insert(int m) { first = new setmem(m,first); } // ... }; : setmem m1(1,0); // : setmem // , , : class setmem { friend class set; // set int mem; setmem* next; setmem(int m, setmem* n) { mem=m; next=n; } // }; class set { setmem* first; public: set() { first=0; } insert(int m) { first = new setmem(m,first); } // ... }; - , , (. $$12.3). - ( ) , : class X { struct M1 { int m; }; public: struct M2 { int m; }; M1 f(M2); }; void f() { M1 a; // : `M1' M2 b; // : `M1' X::M1 c; // : X::M1 X::M2 d; // } , . - X:: . : M1 X::f(M2 a) // : `M1' { /* ... */ } X::M1 X::f(M2 a) // { /* ... */ } X::M1 X::f(X::M2 a) // , X:: { /* ... */ } 5.4.4  - , , , . , , . , . , : class task { // ... static task* chain; // ... }; chain , , , .. task. task, , . : if (task::chain == 0) // - - chain. . , . -, , : class task { // ... static task* task_chain; static void shedule(int); // ... }; - , - , , : task* task::task_chain = 0; void task::shedule(int p) { /* ... */ } , . , static . , : , , ? static ++. , , : " " , .. , , "" , .. , . - static. 5.4.5  . - , , $$4.6.9, . , & , , &class_name::member_name. " X", X::*. : #include <iostream.h> struct cl { char* val; void print(int x) { cout << val << x << '\n'; } cl(char* v) { val = v; } }; : typedef void (cl::*PMFI)(int); int main() { cl z1("z1 "); cl z2("z2 "); cl* p = &z2; PMFI pf = &cl::print; z1.print(1); (z1.*pf)(2); z2.print(3); (p->*pf)(4); } typedef . .* ->* , , . () , .* ->*, . ($$6.2.5) . 5.4.6  - , , .. struct s { ... class s { public: ... , ($$R.9.5). , , , . , : union tok_val { char* p; // char v[8]; // ( 8 ) long i; // double d; // }; , , , . : void strange(int i) { tok_val x; if (i) x.p = "2"; else x.d = 2; sqrt(x.d); // , i != 0 } , : tok_val val1 = 12; // : int tok_val tok_val val2 = "12"; // : char* tok_val : union tok_val { char* p; // char v[8]; // ( 8 ) long i; // double d; // tok_val(const char*); // p v tok_val(int ii) { i = ii; } tok_val(double dd) { d = dd; } }; (. $$4.6.6 $$7.3). : void f() { tok_val a = 10; // a.i = 10 tok_val b = 10.0; // b.d = 10.0 } (, char* char[8] int char ..), , , , , . : tok_val::tok_val(const char* pp) { if (strlen(pp) <= 8) strncpy(v,pp,8); // else p = pp; // } . strncpy() strcpy() , , . , , , , . , , : class tok_val { public: enum Tag { I, D, S, N }; private: union { const char* p; char v[8]; long i; double d; }; Tag tag; void check(Tag t) { if (tag != t) error(); } public: Tag get_tag() { return tag; } tok_val(const char* pp); tok_val(long ii) { i = ii; tag = I; } tok_val(double dd) { d = dd; tag = D; } long& ival() { check(I); return i; } double& fval() { check(D); return d; } const char*& sval() { check(S); return p; } char* id() { check(N); return v; } }; tok_val::tok_val(const char* pp) { if (strlen(pp) <= 8) { // tag = N; strncpy(v,pp,8); } else { // tag = S; p = pp; // } } tok_val : void f() { tok_val t1(""); // v tok_val t2(" "); // p char s[8]; strncpy(s,t1.id(),8); // strncpy(s,t2.id(),8); // check() } Tag get_tag() , , tok_val . , . , , , , , char*, int double: extern tok_val no_arg; void error( const char* format, tok_val a1 = no_arg, tok_val a2 = no_arg, tok_val a3 = no_arg); 5.5  , . , , . : [1] , , , , ; [2] , ; [3] , new delete; [4] -, , . , ($$7.3) ($$R.12.2). . , . table $$5.3.1. 5.5.1  , . , . , : void f(int i) { table aa; table bb; if (i>0) { table cc; // ... } // ... } aa bb ( ) f(), f() - bb, aa. f() i , cc bb . aa bb - table, aa=bb bb aa (. $$2.3.8). ( ) , , : void h() { table t1(100); table t2 = t1; // table t3(200); t3 = t2; // } table : t1 t3. t2, . , table : t1, t2 t3! , - , h() t1, t2 t3 , t1. , t3, . (. $$1.4.2 $$7.6). 5.5.2  : table tbl(100); void f(int i) { static table tbl2(i); } int main() { f(200); // ... } , $$5.3.1, : tbl tbl2. table::~table() : tbl tbl2 main(). , , . , . main() . , . () , main() () main(). ++ . , . <iostream.h>. cin, cout cerr? ? : , , ? : main() (. $$10.5.1). , , , . exit(), . , abort(), . , exit() . exit() . , , , ($$9). : . , . 5.5.3  : main() { table* p = new table(100); table* q = new table(200); delete p; delete p; // , } table::table() , table::~table(). , .. ++ , , new. q , p ! p q . , , , . p - . delete , . , . new delete (. $$3.2.6 $$6.7). , new delete (. $$5.5.6 $$6.7.2). $$5.5.5. 5.5.4  : class classdef { table members; int no_of_members; // ... classdef(int size); ~classdef(); }; , , , classdef , size, : table::table() size. , , : classdef::classdef(int size) :members(size) { no_of_members = size; // ... } (.. table::table()) ( ) , (.. classdef::classdef()). , . ( ): class classdef { table members; table friends; int no_of_members; // ... classdef(int size); ~classdef(); }; ( ), : classdef::classdef(int size) : friends(size), members(size), no_of_members(size) { // ... } , . , , , , , . , no_of_members , . , . , table::table() , 15, : classdef::classdef(int size) : members(size), no_of_members(size) { // ... } friends 15. , (, classdef), , , . : : class classdef { table* members; table* friends; int no_of_members; // ... }; classdef::classdef(int size) { members = new table(size); friends = new table; // // table no_of_members = size; // ... } new, delete: classdef::~classdef() { // ... delete members; delete friends; } , , members friends , . , , -. 5.5.5  , , .. , . , table tbl[10]; 10 , table::table(15), table::table() 15. . , . , . , new, . , , : void f() { table* t1 = new table; table* t2 = new table[10]; delete t1; // delete t2; // : // 10 } , t2 - : void g(int sz) { table* t1 = new table; table* t2 = new table[sz]; delete t1; delete[] t2; } . delete[] . ++ . 5.5.6  , , , . , , . name, table. : struct name { char* string; name* next; double value; name(char*, double, name*); ~name(); void* operator new(size_t); void operator delete(void*, size_t); private: enum { NALL = 128 }; static name* nfree; }; name::operator new() name::operator delete() () operator new() operator delete(). , operator new() operator delete(). , , "" , name, ; . nfree : void* name::operator new(size_t) { register name* p = nfree; // if (p) nfree = p->next; else { // name* q = (name*) new char[NALL*sizeof(name) ]; for (p=nfree=&q[NALL-1]; q<p; p--) p->next = p-1; (p+1)->next = 0; } return p; } , new, , delete . , , . , name::operator new() name 16 , operator new() 20 . , name::operator new() : name* q= new name[NALL]; , .. new name::name(). : void name::operator delete(void* p, size_t) { ((name*)p)->next = nfree; nfree = (name*) p; } void* name* , , name, sizeof(name). size_t name::operator new() name::operator delete() . , $$6.7. , name, names. 5.6  1. (*1) 3 , table. 2. (*1) tnode ($$R.9) .., tnode .. 3. (*1) intset ($$5.3.2) . 4. (*1) intset tnode. tnode . 5. (*3) , , , +, -, * /. : class expr { // ... public: expr(char*); int eval(); void print(); }; expr::expr() -, . expr::eval() , expr::print() cout. : expr("123/4+123*4-3"); cout << "x = " << x.eval() << "\n"; x.print(); expr: , - . , : , , .. 6. (*1) char_queue ( ) , . : (1) (2) . . 7. (*2) histogram (), , . . , . : <task.h>. 8. (*2) , . , draw, "" . : <task.h> intset. 9. (*2) date ($$5.2.2 $$5.2.4), char_stack ($$5.2.5) intset ($$5.3.2), - ( ). class friend. , -. 10.(*3) , . , , . 11.(*2) expr 5 , =. 10. 12.(*1) : #include <iostream.h> main() { cout << " \n"; } , : main() .  * 6 . - . . - , . , . , . , , . - . 6.1  , , . , , . , , . , , , .. . , , , , - . , , , - . : $$6.2 , . $$6.3 , . $$6.4 $$6.5 , , . $$6.6 . $$6.7 . , . 6.2  , . , , : struct employee { // char* name; // short age; // short department; // int salary; // employee* next; // ... }; next (employee). (manager): struct manager { employee emp; // employee employee* group; // short level; // ... }; , employee emp manager. , emp . manager (manager*) employee (employee*), . , manager employee. manager*, employee emp. . , manager employee : struct manager : employee { employee* group; short level; // ... }; manager employee, , , employee manager. group manager employee (name, age ..). : employee ^ | manager , , . , - . , . , . employee manager, , : void f() { manager m1, m2; employee e1, e2; employee* elist; elist = &m1; // m1 elist m1.next = &e1; // e1 elist e1.next = &m2; // m2 elist m2.next = &e2; // m2 elist e2.next = 0; // } , manager* employee*. , employee* manager*. , derived base, derived , base. base derived : void g() { manager mm; employee* pe = &mm; // employee ee; manager* pm = &ee; // : // pm->level = 2; // : ee // `level' pm = (manager*) pe; // : pe // mm manager pm->level = 2; // : pm mm // manager, // `level' } , , . . , ++ , , , pe pm, (. $$13.5). 6.2.1 - employee manager , . : class employee { char* name; // ... public: employee* next; // , // void print() const; // ... }; class manager : public employee { // ... public: void print() const; // ... }; . - manager employee? employee - manager? employee , manager? , ? : void manager::print() const { cout << " " << name << '\n'; } , .. . , , this, name this->name. , manager::print() : , name . , : -