window . window_w_border_and_menu: , $$6.5, satellite. , , . , , , . draw(). , . , : void window_w_border::draw() { window::draw(); // } void window_w_menu::draw() { window::draw(); // } . , ($$6.2.1), . , : void window_w_border_and_menu::draw() // ! { window_w_border::draw(); window_w_menu::draw(); // , // } . , , , , . window::draw() ! , . . , . , , , . _draw(), , draw() , . window : class window { // void _draw(); void draw(); }; : class window_w_border : public virtual window { // " " // , void _draw(); void draw(); }; void window_w_border::draw() { window::_draw(); _draw(); // }; , window::draw(), window::_draw() : class window_w_border_and_menu : public virtual window, public window_w_border, public window_w_menu { void _draw(); void draw(); }; void window_w_border_and_menu::draw() { window::_draw(); window_w_border::_draw(); window_w_menu::_draw(); _draw(); // , // } window::draw() window::_draw(), . window window_w_border window_w_menu . , , , . : , . , - , . , . window get_input(): class window { // virtual void draw(); virtual void get_input(); }; , , : class window_w_banner : public virtual window { // " " void draw(); void update_banner_text(); }; void window_w_banner::update_banner_text() { // ... get_input(); // } get_input() , , : class window_w_menu : public virtual window { // " " // , void draw(); void get_input(); // window::get_input() }; : class window_w_banner_and_menu : public virtual window, public window_w_banner, public window_w_menu { void draw(); }; , - : class window_w_input : public virtual window { // ... void draw(); void get_input(); // window::get_input }; class window_w_input_and_menu : public virtual window, public window_w_input, public window_w_menu { // : window_w_input // window_w_menu // window::get_input void draw(); }; , : window_w_input window_w_menu , "-", - : class window_w_input_and_menu : public virtual window, public window_w_input, public window_w_menu { void draw(); void get_input(); }; window_w_input_and_menu::get_input() get_input(). $$R.10.1.1. 6.6  (private), (protected) (public): X - X. X - X, - X (. $$5.4.1). . : , ( ), , ( ) . . , . , -, .. , : class X { private: enum { A, B }; void f(int); int a; }; void X::f(int i) { if (i<A) f(i+B); a++; } void g(X& x) { int i = X::A; // : X::A x.f(2); // : X::f x.a++; // : X::a } 6.6.1  , window . _draw() , , . . , draw() . , window - : class window { public: virtual void draw(); // ... protected: void _draw(); // , private: // }; , , window_w_border window_w_menu. _ , , : , _ , , . , , ( ). , : class X { // int priv; protected: int prot; public: int publ; void m(); }; X::m : void X::m() { priv = 1; // prot = 2; // publ = 3; // } : class Y : public X { void mderived(); }; Y::mderived() { priv = 1; // : priv prot = 2; // : prot , // mderived() Y publ = 3; // : publ } : void f(Y* p) { p->priv = 1; // : priv p->prot = 2; // : prot , f() // X Y p->publ = 3; // : publ } 6.6.2  , : class X { public: int a; // ... }; class Y1 : public X { }; class Y2 : protected X { }; class Y3 : private X { }; X - Y1, , , () Y1* X*, X: void f(Y1* py1, Y2* py2, Y3* py3) { X* px = py1; // : X - Y1 py1->a = 7; // px = py2; // : X - Y2 py2->a = 7; // px = py3; // : X - Y3 py3->a = 7; // } class Y2 : protected X { }; class Z2 : public Y2 { void f(); }; X - Y2, Y2, Y2 ( Z2) () Y2* X*. X: void Z2::f(Y1* py1, Y2* py2, Y3* py3) { X* px = py1; // : X - Y1 py1->a = 7; // px = py2; // : X - Y2, // Z2 - Y2 py2->a = 7; // px = py3; // : X - Y3 py3->a = 7; // } , : class Y3 : private X { void f(); }; X - Y3, Y3 () Y3* X*. X: void Y3::f(Y1* py1, Y2* py2, Y3* py3) { X* px = py1; // : X - Y1 py1->a = 7; // px = py2; // : X - Y2 py2->a = 7; // px = py3; // : X - Y3, // Y3::f Y3 py3->a = 7; // } 6.7  operator new() operator delete(), . , ( ), , . , employee ($$6.2.5) : class employee { // ... public: void* operator new(size_t); void operator delete(void*, size_t); }; void* employee::operator new(size_t s) { // `s' // } void employee::operator delete(void* p, size_t s) { // `p' `s' , // employee::operator new(); // } size_t . - . sizeof(employee), - sizeof(manager). . , ( ) size_t operator delete(), , . , operator delete()? , operator delete(), , ; : class manager : public employee { int level; // ... }; void f() { employee* p = new manager; // delete p; } . , . employee: class employee { // ... public: // ... void* operator new(size_t); void operator delete(void*, size_t); virtual ~employee(); }; : employee::~employee() { } ( ), employee ( ), . : void f() { employee* p = new manager; // delete p; } ( ) employee::operator new(sizeof(manager)) employee::operator delete(p,sizeof(manager)) , , , size_t. , , . "" , . , , , .. , , , : class X { // ... public: // ... virtual void f(); // X , // virtual ~X(); }; 6.7.1  , : " ?" - . : ", ". , . , - . , . - , . . , , . , , . , , . . , , , . . " " , : class expr { // ... public: expr(); // virtual expr* new_expr() { return new expr(); } }; new_expr() expr, . new_expr() , : class conditional : public expr { // ... public: conditional(); // expr* new_expr() { return new conditional(); } }; , , expr, " ": void user(expr* p1, expr* p2) { expr* p3 = p1->new_expr(); expr* p4 = p2->new_expr(); // ... } p3 p4 , . , , : class expr { // ... expr* left; expr* right; public: // ... // `s' `this' inline void copy(expr* s); // , this virtual expr* clone(int deep = 0); }; deep ( ) , ( ). 0 . clone() , , : void fct(expr* root) { expr* c1 = root->clone(1); // expr* c2 = root->clone(); // // ... } , clone() expr . : void expr::copy(expression* s, int deep) { if (deep == 0) { // *this = *s; } else { // : left = s->clone(1); right = s->clone(1); // ... } } expr::clone() expr ( expr ), expr, : expr* expr::clone(int deep) { expr* r = new expr(); // r->copy(this,deep); // `*this' `r' return r; } clone() expr , - ( ): class arithmetic : public expr { // ... // - => // clone }; , -, clone(): class conditional : public expression { expr* cond; public: inline void copy(cond* s, int deep = 0); expr* clone(int deep = 0); // ... }; copy() clone() expression: expr* conditional::clone(int deep) { conditional* r = new conditional(); r->copy(this,deep); return r; } void conditional::copy(expr* s, int deep) { if (deep == 0) { *this = *s; } else { expr::copy(s,1); // expr cond = s->cond->clone(1); } } expr::copy() expr::clone() (.. ). . copy() clone() ($$1.4.2) _draw() draw() ($$6.5.3). , copy() . , clone(). , -. 6.7.2  new . , ? . : class X { // ... public: X(int); // ... }; , : // : void* operator new(size_t, void* p) { return p; } new : char buffer[sizeof(X)]; void f(int i) { X* p = new(buffer) X(i); // X buffer // ... } operator new(), new, ($$R.13.2). operator new() size_t. new. operator new() . , : class Arena { // ... virtual void* alloc(size_t) = 0; virtual void free(void*) = 0; }; void operator new(size_t sz, Arena* a) { return a.alloc(sz); } (Arena): extern Arena* Persistent; // extern Arena* Shared; // void g(int i) { X* p = new(Persistent) X(i); // X X* q = new(Shared) X(i); // X // ... } , , . : void h(X* p) { p->~X(); // Persistent->free(p); // } , , , , , . , , , , . 6.8  1. (*1) class base { public: virtual void iam() { cout << "base\n"; } }; base iam(), . iam() . base* iam() . 2. (*2) ($$6.4.1) . 3. (*2) triangle () circle (). 4. (*2) , , . , . 5. (*2) shape , line rectangle, . 6. (*2) class char_vec { int sz; char element [1]; public: static new_char_vec(int s); char& operator[] (int i) { return element[i]; } // ... }; new_char_vec() char_vec , element[]. ? 7. (*1) , shape $$6.4, , . 8. (*1.5) , satellite $$6.5, , . 9. (*2) , window $$6.5.3, , . 10. (*2) , . - , , . , , . - , , . , . . 11. (*2) clone() $$6.7.1, Arena ($$6.7.2), . Arena Arena. 12. (*2) Circle (), Square () Triangle (), shape. intersect() Shape*, , , . . , , , . 13. (*5) , . : <task.h>. . task (). task ( task::save() task::restore()) . , task. , , . . , . task::delay(long), "" . : task, ? . queue (). , . . , ?  * 7 , , , . - ++. , . , (), [], ->, . . , , , . 7.1  , . , ++ int +, -, *, / .. ( ) . , , , . , . , , , ++ . , , ++ . , , . : class complex { double re, im; public: complex(double r, double i) { re=r; im=i; } friend complex operator+(complex, complex); friend complex operator*(complex, complex); }; , , + *. operator+ operator*. , b c complex, b+c ( ) operator+(b,c). : void f() { complex a = complex(1,3.1); complex b = complex(1.2,2); complex c = b; a = b+c; b = b+c*a; c = a*b+complex(1,2); } , b=b+(c*a), b=(b+c)*a. 7.2  , : + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete : ($$7.9), ($$7.7), ($$7.8), ($$3.2.6). , . , % , !. , , . pow(), ** . , . , ** , . , : ** ( ) ( )? a**p a*(*p) (a)**(p)? operator, , , operator<<. . : void f(complex a, complex b) { complex c = a + b; // complex d = operator+(a,b); // } complex . 7.2.1  - , . , @ aa @ bb aa.operator(bb), operator@(aa,bb). , ($$R.13.2). - , . @ @aa aa.operator@(), operator@(aa). , ($$R.13.2). @ @aa aa.operator@(int), operator@(aa,int). $$7.10. , ($$13.2). , ++. , % , + . : class X { // ( `this'): X* operator&(); // & // ( ) X operator&(X); // & ( ) X operator++(int); // X operator&(X,X); // : & X operator/(); // : / }; // ( ) X operator-(X); // X operator-(X,X); // X operator--(X&,int); // X operator-(); // : X operator-(X,X,X); // : X operator%(X); // : % [] $$7.7, () $$7.8, -> $$7.9, ++ -- $$7.10. 7.2.2  . , operator=, operator[], operator() operator-> -. , . , . , a int, ++a a+=1, a=a+1. , . , operator+=() complex complex::operator+() complex operator=(). , = (), &( ) , ( ) . , : class X { // ... private: void operator=(const X&); void operator&(); void operator,(const X&); // ... }; void f(X a, X b) { a= b; // : = &a; // : & a,b // : , } , . 7.2.3  , , ( , new delete, ). , , . , , . , ++ , ( =, &, , ). ,