, , . . , pow(12.3,"abcd") , "abcd" , int. pow(2,i) ( 2) (float), . pow : float pow ( float x, int n ) { if ( n < 0 ) error ( ": pow () "); switch ( n ) { case 0: return 1; case 1: return x; default: return x * pow ( x, n-1 ); } } , ( ), ( ). return. , , , . , , . , : - , - : int pow ( int, int ); double pow ( double, double ); //... x = pow ( 2,10 ); // pow ( int, int ) y = pow ( 2.0, 10.0 );// pow ( double, double ) ; 7. " ", " ". , . , : void swap ( int * p, int * q ) { int t = * p; * p = * q; * q = t; } * ( ), , . : void f ( int i, int j ) { swap ( & i, & j ); } , : void swap (int & r1, int & r2 ) { int t = r1; r1 = r2; r2 = t; } void g ( int i, int j ) { swap ( i, j ); } T T& " T". , . , swap . 1.3.6  ++ "". "" , - . , , . extern , . : extern "C" double sqrt ( double ); extern ostream cout; - , . , . , sqrt math.h, , 4, : #include <math.h> //... x = sqrt ( 4 ); , , . , , -, ($$4.3). , , . ( - <math.h>) , . - /usr/include/CC. , , , . : #include "math1.h" #include "/usr/bs/math2.h" math1.h math2.h /usr/bs. , , . header.h : // header.h extern char * prog_name; extern void f (); main.c : // main.c #include "header.h" char * prog_name = ", "; int main () { f (); } f.c: // f.c #include <stream.h> #include "header.h" void f () { cout << prog_name << '\n'; } ++ - ++. : $ CC main.c f.c -o silly $ silly , $ ++ ($$5.4). 1.4  ( ) . . , , , . . 1.4.1  , . - . : class vector { // ... public: void init ( init size ); // init () // vector // ... }; void f () { vector v; // v v.init ( 10 ); // } . , . , ( ). . , , . , . ++ . , , ~ ( ++ ). : class vector { int sz; // int * v; // public: vector ( int ); // ~vector (); // int& operator [] ( int index ); // }; vector : vector::vector ( int s ) { if ( s <= 0 ) error ( " " ); sz = s; v = new int [ s ]; // s } vector : vector::~vector () { delete [] v; // , // v } ++ new , ( , " "). . , (., , $$9.4). 1.4.2  , , . . vector: void f () { vector v1 ( 100 ); vector v2 = v1; // v2, // v1 v1 = v2; // v2 v1 // ... } v2 v1. , : class vector { int * v; int sz; public: // ... void operator = ( const vector & ); // vector ( const vector & ); // }; , vector . : void vector::operator = ( const vector & a ) // { if ( sz != a.sz ) error ( " =" ); for ( int i = 0; i < sz; i++ ) v [ i ] = a.v [ i ]; } " " , , , : vector::vector ( const vector & a ) // { sz = a.sz; // v = new int [ sz ]; // for ( int i = 0; i < sz; i++ ) // v [ i ] = a.v [ i ]; } ++ T(const T&) T. T T. T(const T&) . 1.4.3  , ? , , Vector. , , , "" : template < class T > class Vector { // T T * v; int sz; public: Vector ( int s ) { if ( s <= 0 ) error ( " Vector " ); v = new T [ sz = s ]; // s T } T & operator [] ( int i ); int size () { return sz; } // ... }; . . Vector , . template<class T>, , , - ( ). : void f () { Vector < int > v1 ( 100 ); // 100 Vector < complex > v2 ( 200 ); // 200 // v2 [ i ] = complex ( v1 [ x ], v1 [ y ] ); // ... } , , . , Clu . - , . 1.4.4  , (, , " "). , -68 Clu . vector. , , ? vector , , ( , ). : , . . : class vector { // class range { }; // ... }; vector::operator[]() , . " " ("throw the exception"): int & vector::operator [] ( int i ) { if ( i < 0 || sz <= i ) throw range (); return v [ i ]; } , , , range (vector::range); . : void f ( int i ) { try { // // vector v ( i ); // ... v [ i + 1 ] = 7; // range // ... g (); // range // } catch ( vector::range ) { error ( "f (): vector range error" ); return; } } . 9. 1.4.5  , , , , complex(double), ++. , , , : complex a = complex ( 1 ); complex b = 1; // : 1 -> complex ( 1 ) a = b + complex ( 2 ); a = b + 2; // : 2 -> complex ( 2) ++ , , . , , "" (, , , ) ( ). : complex a = 2; complex b = a + 2; // : operator + ( a, complex ( 2 )) b = 2 + a; // : operator + ( complex ( 2 ), a ) "+" , . , complex , - . 1.4.6  , - , : ,- , . : template < class T > class stack { public: virtual void push ( T ) = 0; // virtual T pop () = 0; // }; =0 , , stack , .. . , : class cat { /* ... */ }; stack < cat > s; // : - void some_function ( stack <cat> & s, cat kitty ) // { s.push ( kitty ); cat c2 = s.pop (); // ... } , . . , : template < class T > class astack : public stack < T > { // // - // ... public: astack ( int size ); ~astack (); void push ( T ); T pop (); }; : template < class T > class lstack : public stack < T > { // ... }; : void g () { lstack < cat > s1 ( 100 ); astack < cat > s2 ( 100 ); cat Ginger; cat Snowball; some_function ( s1, Ginger ); some_function ( s2, Snowball ); } , , , (.. g()), (.. some_function()) . , . 1.5 -  - , - ( , , ). -. , ( ). , , , - . , , , . - , , . 1.5.1  - - - , . , , p. p->rotate(45)? ++ , , rotate() . , p->rotate() , p , rotate . , ( ), . , . , , , $$1.2.5: class shape { // ... public: // ... virtual void rotate ( int ); // ... }; p , , : T * p; T - shape . , , p, rotate(), int. , p->rotate(45) . shape::rotate() , . , rotate , , . , , circle::rotate, . , rotate . shape : center vtbl: color &X::draw &Y::rotate ... ... vtbl , vtbl, , ... . X Y , . circle X Y circle. , . 1.5.2  . , " -". ++ . , , , . , , Smalltalk, " -". : template < class T > class stack { T * p; int sz; public: stack ( int ); ~stack (); void push ( T ); T & pop (); }; , plane (): stack < plane * > cs ( 200 ); void f () { cs.push ( new Saab900 ); // : // plane*, car* cs.push ( new Saab37B ); // : Saab 37B - // , .. plane cs.pop () -> takeoff (); cs.pop () -> takeoff (); } , : // // ; ++ Stack s; // // void f () { s.push ( new Saab900 ); s.push ( new Saab37B ); s.pop () -> takeoff (); // : Saab 37B - cs.pop () -> takeoff (); // : // } , , , ++. , , . ++ , Smalltalk , . , ++ , , . 1.5.3  A B, B A. .. B A -. , , B A1 A2. . . displayed task. , , - , . , , : class my_displayed_task: public displayed, public task { // }; class my_task: public task { // // , .. displayed // }; class my_displayed: public displayed { // // .. task // }; , . , , , , , . ++ , . . : class task { public: void trace (); // ... }; class displayed { public: void trace (); // ... }; class my_displayed_task:public displayed, public task { // trace () }; void g ( my_displayed_task * p ) { p -> trace (); // : } ++ - , . : , , , . ++ , , : class my_displayed_task:public displayed, public task { // ... public: void trace () { // displayed::trace (); // trace () displayed task::trace (); // trace () task } // ... }; void g ( my_displayed_task * p ) { p -> trace (); // } 1.5.4  ( - , ) " ". , ? , - , : , , , -. : class window { // ... protected: Rectangle inside; // ... }; class dumb_terminal : public window { // ... public: void prompt (); // ... }; window inside Rectangle (protected), - , , dumb_terminal::prompt(), , . window::inside . (, "" ) , , (, " " ). : , , , - . . , - , . ++ (private) . complex shape. , .. , . $$6.6 . 1.6  ++ " ", - . . , , - , , , , : - ; - ; - ; - . , ( , ) , . , , . , , - , , , , - . , , , . , , . ++ , , , . , ++ , , , . , , . , , . , . , . , , . ++ , , , ($$12.2). ++, , . , , " ". , , , .  * 2.  " ". (.. ) (char, int, float ..) (, , ..). , , , . , , , , . ++ . , . , ++, . . 5 7. 2.1  () , ++. , , , . , : char ch; int count = 1; char* name = "Njal"; struct complex { float re, im; }; complex cvar; extern complex sqrt(complex); extern int error_number; typedef complex point; float real(complex* p) { return p->re; }; const double pi = 3.1415926535897932385; struct user; template<class T> abs(T a) { return a<0 ? -a : a; } enum beer { Carlsberg, Tuborg, Thor }; , . , .. , . ch, count, name cvar . , , . real . pi 3.1415926535897932385. complex . point complex, point complex. : extern complex sqrt(complex); extern int error_number; struct user; , , , - . sqrt - . error_number int error_number. - user, , . ++ , . . : int count; int count; // : extern int error_number; extern short error_number; // : ( extern . #4.2): extern int error_number; extern int error_number; "" , : struct complex { float re, im; }; typedef complex point; float real(complex* p) { return p->re }; const double pi = 3.1415926535897932385; , "" ; , , : int count = 1; char* name = "Bjarne"; //... count = 2; name = "Marian"; : char ch; , , . 2.1.1  . , . ( " "), , . ( " "), , . ; .. , . ( ) . : int x; // x void f() { int x; // x x x = 1; // x { int x; // x x = 2; // x } x = 3; // x } int* p = &x; // x . ,