Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://al.cmc.msu.ru/files/cpp.tasks.2013.pdf
Äàòà èçìåíåíèÿ: Sat Jan 25 10:32:50 2014
Äàòà èíäåêñèðîâàíèÿ: Thu Feb 27 20:29:04 2014
Êîäèðîâêà: IBM-866
..

. . , . . , . .

++
( II )

í 2013


004.43(075.8) 32.973-018.173 67 - .. : .. -- ..-..; .. -- ..-..

. ., .., . . 67 ++: II . -- .: . . ( 05899 24.09.2001 .); , 2013 -- 64 .
ISBN 978-5-317-04595-1 B ++, 2 ë ¨. ++ ISO/IEC (1998). : , , . 2008--2013 . ë ¨, , . .
004.43(075.8) 32.973-018.173


++

II
19992, -2, , , . .. , 2 -

ISBN 978-5-317-04595-1 .., 2013 .., .., .., 2013
2


I.
1.

(). .

1.1. ? , , .1 , f(), f() ? f()?
class A { int a, b; public: A (A & x) { a = x.a; b = x.b; cout << 1; } A (int a) { this -> a = a; b = a; cout << 2; } }; void f () { A x (1); A y; A z = A (2.5, 4); A s = 6; A w = z; x = z = w; }

1.2. , : ) , ) :
A A A A a; b(1); c(1, 2); d('1', 1);

1.3. , , , . main ()?
a) class X { int i; double t; X(int k) { i = k; t = 0; cout << 1; }

1

, , #include using namespace std #include. 3


public: X(int k, double r = 0) { i = k; t = r; cout << 2; } X & operator= (X & a) { i = a.i; t = a.t; cout << 3; return * this; } X(const X & a) { i = a.i; t = a.t; cout << 4; } }; int main() { X a; X b(1); X c (2, 3.5); X d = c; X e (6.5, 3); c = d = e; return 0; } b) class X { int i; double t; X( ) { i = 0; t = 1.0; cout << 1; } public: X(int k = 0, double r = 1.5) { i = k; t = r; cout << 2; } X(const X & a) { i = a.i; t = a.t; cout << 3; } }; int main() { X a; X b(1); X c (1.5, 2); X d = b; X e = 3; b = c = e; return 0; } 4


1.4. , main () , 100 300.
int main () { A a1 (5), a2 = 3; a1 *= 10; a2 *= a1 *= 2; cout << a1.get() << a2.get() << endl; return 0; }

1.5. B , main , 10 20 30.
int main () { B b1, b2 = b1, b3 (b2); cout << b1.get() << b2.get() << return 0; }

b3.get () << endl;

1.6. C , main , 14 10 48.
int main () { C c1 (7), c2 = 5, c3 (c1 + c2); cout << c1.get() << c2.get() << c3.get () << endl; return 0; }

1.7.

, : - main , - , - 15 60 7. .
int main () { A a1(5), a2 = 4, 3; a2 *= a1 *= 3; cout << a1.get( ) << ' ' << a2.get() << ' ' << return 0; }

a3.get( ) << endl;

1.8.

, : - main , - , - 17 11 6. .
int main () { B b1 (1), b2(2,3), b3 (b1); b1 += b2 += b3; cout << b1.get() << ' ' << b2.get() << ' ' << b3.get () << endl; return 0; }

5


1.9.

, : - main 2, - C , - 2 14 56. .
int main () { C c1(7), c2 = 5, c3(c1 + c1); cout << 1.get ( ) << ' ' << 3.get ( ) << endl; return 0; }

1.10. ?
class I { int i; public: I() : i(9) { cout << "sun" <
1.11. ?
class I { int i; public: I() : i(6) { cout << "owl" << endl; } I(int a) : i(a) { cout << "sheep " << i << endl; } I(const I & other) : i(other.i) { cout << "horse " << i << endl; } ~I() { cout << "wolf" << endl; } int Get() { return i; } void operator*=(const I & op) { i*=op.i; } }; void f(I x, I & y) { x *= 1; y *= x; }

6


int main() { I i1; I i2(3); i1 *= 7; f(i1, i2); cout << i1.Get() << ' ' << i2.Get()<< endl; return 0; }

1.12. ?
class I { int i; public: I() : i(5) { cout << "fist" << endl; } I(int a) : i(a) { cout << "lance " << i << endl; } I(const I & other) : i(other.i) { cout << "dagger " << i << endl; } ~I() { cout << "pistole" << endl; } int Get() { return i; } void operator+=(const I & op) { i+=op.i; } }; void f(I & x, I y) { y += 1000; x += y; } int main() { I i1; I i2(30); i2 += 700; f(i1, i2); cout << i1.Get() << ' ' << i2.Get() << endl; return 0; }

1.13. , :
struct mystr { int a, b; }; int i = sizeof(mystr); int f(mystr s) { return 0; }

mystr ( f ) , f . 1.14. smartstr, : (1) smartstr; (2) smartstr .

7


1.15. :
) class A {}; class B : public A {}; class C : public B {}; int main(){ C c; A a = c; struct D { B b; D(): b(5){} } d; } class A {}; class B : public A {}; class C : public B {}; int main(){ class D { B b; A a; public: D (): a(b){ } } d; C c; } class A {}; class B {}; class C : public A, public B {}; int main(){ C c; class D { C c; B b; public: D(): b(c){} } d; }

b)

c)

1.16. ?
struct S { int x; S (int n) { x = n; printf (" Cons "); } S (const S & a) { x = a.x; printf (" Copy ~S ( ) { printf ("Des "); } }; S f( S y ) { y = S(3); return y; } int main () { S s (1); f (s); printf ("%d return 0; }

"); }

",

s.x);

8


1.17. ?
struct S { int x; S(int n) { x = n; printf(" Cons "); } S(const S & a) { x = a.x; printf(" Copy ~S() { printf("Des "); } }; S f( S & y ) { y = S(3); return y;

"); }

} int main () { S s(1); f (s); printf("%d return 0; }

",

s.x);

1.18. ?
struct S { int x; S( int n ) { x = n; printf( " Cons " ); } S( const S & a ) { x = a.x; printf( " Copy ~S( ) { printf( "Des " ); } }; S & f( S y , S & z) { y = S (3); return z; } int main ( ) { S s(1); f( s, s ); printf( "%d return 0; }

" ); }

",

s.x );

1.19. ( !) B D , main () , 5535324242.
struct B { float x; B (float a) { x = a; ~B( ) { cout << 2; } }; struct D : B { D( ) { cout << 3; } ~D( ) { cout << 4; } };

cout << 5; }

9


int main ( ) { B * p1 = new B (1), delete p1; delete [ ] p2; return 0; }

* p2 = new D[2];

1.20. ( !) B D , main () , 11163343.
struct B { int x; B (int a) { x = a; ~B () { cout << 3; } };

cout << 1; }

struct D : B { D (int d ) : B (d) { cout << 6; } ~D () { cout << 4; } }; int main () B * p1 delete delete return } { = new B [2], [ ] p1; p2; 0;

* p2 = new D (1);

1.21. ( !) B D , main () , 776898.
struct B { int x; B() { ~B() { };

x = 7; cout << 7; } cout << 8; }

struct D : B { D( int d) { x = d ; ~D() { cout << 9; } }; int main () B * p1 delete delete return } { = new B [1], [ ] p1; [ ] p2; 0;

cout << 6; }

* p2 = new D[1];

1.22. ? , , .
a) int n; float f(float a, int t = 3, int d = n) { return a * (float) (t % d); } 10


b)

float f(float a = 2.7, int t, int d = 8) { return sa * (float) (t % d); } enum { myparam = 18 }; float f(float a, int t = myparam + 5, int d = t + 8) { return a * (float) (t % d); }

)

2. 2.1.

.
:
class Cls { int i; public: Cls() { i = 1; } }; void f(Cls * p, Cls * q) { *p = *q; }

Cls ( f ), f . 2.2. f , :
a) f f f f f f f f f f f f f f f f f (1); ('+', '+'); (2.3); (3, "str"); (); ("abc"); (2); ('+', 3); (0, 1); (1, 0); (0, "m"); ("n", 0); ("p"); (x, 0); (0, 0); (x, "q"); (1, "r"); // // // // struct X { X(int); operator int(); } x;

b)

c)

d)

11


e)

f f f f f f

(1000000000000); (1); (); (0, 0); ("t"); (1, "u");

2.3. , . )
int f(int a = 0) { return a; } int f(double a) { return a; } int main() { short int s; int i; bool b; enum e {A, B, C}; float f1 = 1.0f; f(); f(s); f(f1); f(b); f(A); }

b) int f(double a = 1.0){return a;}
int f(long double a = 5.0){return a;} int main() { float f1 = 1.0f; double d = 2.0; long double ld = 3.0l; f(); f(4); f(f1); f(d); f(ld); }

c)

int f(int a = 1){return a;} int f(long double a = 5.0){return a;} int main () { short int s; int i; bool b; float f1 = 1.0f; double d = 2.0; f(s); f(i); f(b); f(f1); f(d); }

12


3.

.

3.1. B::g () main (), , . ë::¨ , . ? a) int x = 0;
void f (int a, int b){x = a+b;} class A { int x; public: void f (){x = 2;} }; class B: public A { public: void f (int a){::x = a;} void g (); }; void B::g() { f(); f(1); f(5 , 1); x = 2; } B } int main () { B b; f(5); f('+', 6); b = ret (b, return 0; } ret (B & x, B & y) { return x;

b);

b)

double a = 0; void f (double x){a = x;} struct A { double a; void f (){a = 2;} };

13


class B : A { public: void f (int a){::a = a;} void g (); }; void B::g() { f(1.2); f(); a = 2; } void empty (B & a, B b) { }

int main () { B d; f(); f(6); empty (d, d); return 0; }

c)

int x = 0; void f(int a, int b) { x = a+b; } class A { int x; public: void f() { x = 2; } void f(char a1, char b1) { x = a1-b1; } }; class B: public A { public: void f(int a) { ::x = a; } void g () { f(); f(0); f(5.3 , 1); x = 1; } }; int main () { B b; f(2); f(3, 'a'); return 0; }

d)

double a = 0; void f(double x = 2) { a = x; } void f() { a = 1; }

14


struct BBase { double a; void f(){ a = 2; } }; class B: BBase { public: void f(int a) { ::a = a; } void g() { f('r'); f(); a = 2; } }; int main () { B d; f(); f(6); return 0; }

e)

float y = 0; void f(float a) { y = a; } class T { int y; public: void f() { y = 2; } }; class B public: void void void : public T { f(float n, float m) { ::y = n * m; } f(char c1, char c2) { ::y = c1 + c2; } g () { f(); f(1); f(-1 , 1); y = 2;

} }; int main () { B b; f(5); f('+', 6); return 0; }

15


3.2. main (), , ë::¨. ? a)
int x = 0; int f (int a, int b) { return x = a + b; } class A { int x; public: A (int n = 1) { x = n; } int f() { return ::x = x; } }; class B { int x; public: B (int n = 2) { x = n; } }; class C: public A, public B { int x; public: int f(int a) { return ::x = x; } void g (); }; void C::g() { x = f (); f (3); x = f (4, 5); x = 6; } int main () { C c; B b = c; A a = ; c.f (); c.f (7); x = f (`8', 9); return -1; }

b)

int x = 0; int f() { return x = 1; } class A { int x; public: A( int n = 2) { x = n; } int f() { return x = 3; } int f(int a, int b) { return x = a % b; } };

16


class B: int public: int int int }; int

public A { x; f(int a) { return x = a; } f(int a, int b) { return x = a + b; } g(A * a, B * b);

B::g (A * pa, B * pb) { x = f (); x = f (5) x = f (6, 6); x = A::f (5); return -1;

} int main () { B a; class C { B b; A a; public: C(): b(), a (b) { } }; C c; x = a.f (); x = a.f (7); return a.g (& a, & a); }

c)

int x = 0; int f (int a, int b) { return x = a + b; } class A { int x; public: A(int n = 1) { x = n; } int f() { return ::x = x; } }; class B { int x; public: B (int n = 2) { x = n; } }; class C: int public: int void }; void x f x x } public A, public B { x; f(int a) { return ::x = x; } g ();

C::g() { = f (); (3); = f (4, 5); = 6;

17


int main () { C c; B b; A a; c.f(); c.f(7); x = f(`8', 9); return -1; }

d)

int x = 0; int f() {return x = 1; } class A { int x; public: A( int n = 2) { x = n; } int f() { return x = 3; } int f(int a, int b) { return x = a % b; } }; class B: int public: int int int }; int public A { x; f(int a) { return x = a; } f (int a, int b) { return x = a + b; } g (A * a, B * b);

B::g (A * pa, B * pb) { x = f(); x = f(5); x = f(6, 6); x = A::f(5); return -1;

} int main () { B a; x = a.f(); x = a.f(7); class C { A a1; public: C(): a1(b) {} }; C c; return a.g(& a, & a); } e) int x = 4; class A { int x; public: A(int n = 1); int f(int a = 0, int b = 0); };

18


class B: public A { public: int x; B(int n = 2); int f(int a); }; class C: public B { int x; public: C(int n = 3); int f(int a, int b); int g(A * p); }; int main () { A * p; B b; C c; A a = c; struct D { B b; D(): b(5) {} } d; p = & b; x = c.g(& c); x = c.f(); x = c.f(x); x = c.f(x, 1); x = p -> f(); x = p -> f(x); x = p -> f(x, 1); return 1; } f) int x = 4; class A { int x; public: A(int n = 3); int f(int a = 0, int b = 0); }; class B: public A { int x; public: B(int n = 1); int f(int a = 0); }; class C: public B { int x; public: C(int n = 2); int f(int a, int b); int g(A * p); };

19


int main () { A * p; B b; C c; class D { B b; A a; public: D(): a(b) {} } d; p = & b; x = c.g(& b); x = c.f(); x = c.f(x); x = c.f(x, 1); x = p -> f(); x = p -> f(x); x = p -> f(x, 1); return 2; }

3.3. , ? , , 14 .
class A { public: int * n; int m; }; class B: public A { public: int * p; }; class C: public A { public: int * c; }; class D: public B, public C { public: int * e; }; int main () { D fA, * f = new D; fA.m =0; return *((* f).e = & fA.m); }

3.4. , ? , , 12 .
class S { public: int s; void sp(int si) { s = si; } }; 20


class T: S { public: int t; void tp(int ti) { t = ti; s = ti; } }; class U: T { public: int u; void up(int ui) { u = ui; t = ui; s = ui; } }; void g() { U * pu = new U; T * pt = pu; S * ps = pu; }

3.5. , ? , , 29 .
class W { public: int * w; void wf (int * wp) { w = wp; } }; class X: W { public: int * x; void xf (int * xp) { x = xp; w = xp; } }; class Y: W { public: int * y; void yf (int * yp) { y = yp; w = yp; } }; class Z: X, Y { public: int * z; void zf (int * zp) { z = zp; x = zp; y = zp; } }; void h () { int hi; W * pw; X * px; Y * py; Z * pz; pz = new W; (*pz).w = & hi; pz -> xf ((*pz).X::w); }

21


4. 4.1. 4.2.

.
. . ? , , . . ?
class X { public: virtual int g (double x) { h (); cout << "X::g" << endl; return 1; } void h () { t (); cout << "X::h" << endl;} virtual void t () { cout << "X::t" << endl;} }; class Z: public X { public: int g (double y) { h (); cout << "Z::g" << endl; return 3; } virtual void h () { t (1); cout << "Z::h" << endl; } virtual void t (int k) { cout << "Z::t" << endl; } }; int main(){ X a; Z b; p -> g(1.5); p -> h(); p -> t(5); }

X * p = &b;

4.3.

? , , . . ?
class T { public: virtual int f (int cout << "T::f" return 0; } void g () { f (1); cout << "T::g" } virtual void h () g (); cout << "T::h" } };

x) { << endl;

<< endl; { << endl;

22


class S: public T { public: int f (double y){ cout << "S::f" return 2; } virtual void g () { f (1); cout << "S::g" } virtual void h () { g(); cout << "S::h" } }; int main(){ T t; S s; T *p = p -> f (1.5); p -> g (); p -> h (); }

<< endl;

<< endl;

<< endl;

&s;

4.4.

? , , . . ?

class K { public: virtual int f (int x) { cout << "K::f" << endl; return 0; } void g () { f (1); cout << "K::g" << endl; } virtual void h () { g (); cout << "K::h" << endl; } }; class P: public K { public: int f (double y) { cout << "P::f" << endl; return 2; } virtual void g () { f (1); cout << "P::g" << endl; } virtual void h () { g(); cout << "P::h" << endl; } }; 23


int main(){ K k; P p; K *t = &p; t -> f (0.7); t -> g (); t -> h (); }

4.5.

? , , . . ?
class A { public: virtual void f (int x) { h (x); cout << "A::f," << x << endl; }

void g () { h (0); cout << "A::g" << endl; } virtual void h (int k) { cout << "A::h," << k << endl; } }; class B: virtual public A { public: void f (int y) { h (y); cout << "B::f," << y << endl; } void g () { h (1); cout << "B::g" << endl; } void h (int k) { cout << "B::h," << k << endl; } }; int main(){ A a; B p -> f p -> g p -> h p -> h }

b; A * p = & b; (2); (); (); (3);

4.6.

? , , . . ?
class C { public: virtual void f (int x) { h (x); cout << "C::f," << x << endl; } 24


virtual void g () { h (0); cout << "C::g" << endl; } virtual void h () { cout << "C::h" << endl; } virtual void h (int k) { h (); cout << "C::h," << k << endl; } }; class D: public C { public: virtual void f (int y) { h (y); cout << "D::f," << y << endl; } virtual void g () { h (1); cout << "D::g" << endl; } virtual void h () { cout << "D::h" << endl; } virtual void h (int k) { h (); }; int main(){ C c; D d; C * p = & d; p -> f (2); p -> g (); p -> h (); p -> h (3); }

cout << "D::h," << k << endl; }

4.7.

? , , . . ?
class T { public: virtual void f (int x) { h (); cout << "T::f," << x << endl; } void g () { h (); cout << "T::g" << endl; } virtual void h () { cout << "T::h" << endl; } }; class U: virtual public T { public: void f (int y) { h (y); cout << "U::f," << y << endl; } virtual void g () { h (0); cout << "U::g" << endl; }

25


void h (int k) { cout << "U::h," << k << endl; } }; int main(){ T t; U u; T * p = & u; p -> f (1); p -> g (); p -> h (); p -> h (2); }

4.8.

?
class A { int i; public: A(int x) virtual int f() virtual int h() };

{ i = x; cout ~A() { cout << const { return int g() const const { return

<< "first" << endl; } "second" << endl; } i + g() + h(); } { return i; } 39; }

class B : public A { public: B() : A(70) { cout <<"third" << endl; } ~B() { cout << "fourth" << endl; } int f() const { return g() - 2; } virtual int g() const { return 4; } int h() const { return 6; } }; int main() { B b; A* p = &b; Cout << "result = (" << p->f() <<';'<< b.f() << ')' << endl; return 0; }

4.9.

?
class A { int i; public: A(int x) virtual int f() virtual int h() };

{ i = x; cout << "mercury" << endl; } ~A() { cout << "venus" << endl; } const { return 96; } int g() const { return i; } const { return i - f() - g(); }

class B : public A { public: B(int x) : A(x+20) { cout << "earth" << endl; } ~B() { cout << "mars" << endl; } int f() const { return 8; } virtual int g() const { return 3; } int h() const { return f() + g(); } };

26


int main() { B b(17); A* p = &b; Cout << "result = (" << p->h() << ';'<< b.h() <<')' << endl; return 0; }

4.10. ?
class A { int i; public: A(int x) virtual int f() virtual int h() };

{ i = x; cout ~A() { cout << const { return int g() const const { return

<< "dog" "cat" << i + g() { return 5; }

<< endl; } endl; } + h(); } i; }

class B : public A { public: B() : A(21) { cout << "sheep" << endl; } ~B() { cout << "horse" << endl; } int f() const { return g() - 3; } virtual int g() const { return 7; } int h() const { return 9; } }; int main() { B b; A* p = &b; Cout << "result = (" << return 0; }

p->f() << ';' << b.f() <<')' << endl;

4.11. :
struct A { int i; virtual void f() = 0; virtual ~A() {} }; int g(A a) { return a.i * 5; }

? , ? 4.12. :
struct S { virtual void f() const = 0; virtual ~S() {} };

27


struct A { S s; int i; };

? , ? 4.13. :
class B { public: virtual int g() virtual }; int h(B b) {

int f() = 0; { return f() * 10; } ~B() {} return b.g() + 2; }

? , ?

4.14. ?
struct B { virtual void f (int n) { cout << "f (int) from B" << endl; } static int i; }; struct D: B { virtual void f (char n) { cout << "f (char) from D" << endl; } }; int B::i = 1; int main () { D d; B b1, b2, *pb = &d; pb -> f ( `a'); b1.i += 2; b2.i += 3; d.i += 4; cout << b1.i << ' ' << b2.i << ' ' << d.i << ' ' << return 0; }

B::i << endl;

4.15. ?
struct K { virtual void add_st ( K * n ) { st ++; cout << "add_st (K*) from K" << endl; } static int st; }; struct L: K { virtual void add_st ( L * a ) { st++; cout << "add_st (L*) from L" << endl; } }; 28


int K::st = 2; int main () { L ob, ob2; K k, *pl = &ob; pl -> add_st (& ob2); k.st ++; ++ob.st ; cout << k.st << ' ' << ob.st << ' ' << return 0; }

K::st << endl;

4.16. ?
struct S { static double d; virtual S & g () { cout << "g ( ) from S" << endl; } }; struct T: S { virtual T & g ( ) { cout << "g ( ) from T" << endl; } }; double S::d = 1.5; int main () { T t; S s , *ps = &t; ps -> g (); s.d = 5; t.d = 7; cout << s.d << ' ' << t.d << ' ' << return 0; }

S::d << endl;

5. 5.1.


?
class X; void F(X & x, int n); class X { public: X() { try { F(*this, -2); cout << 1 << endl; } catch (X) { cout << 2 << endl; } catch (int) { cout << 3 << endl; } } X(X &) { cout << 12 << endl; } }; class Y: public X { public: Y () {cout << 4 << endl;} Y (Y & a) {cout << 5 << endl;} ~Y () {cout << 6 << endl;} };

29


void F(X & x, int n) { try { if (n < 0) throw x; if (n > 10) throw 1; cout << 7 << endl; } catch (int) { cout << 8 << endl; } catch (X&) { cout << 9 << endl; throw; } } int main() { try { Y a; } catch (...) { cout << 10 << endl; cout << 11 << endl; }

}

5.2.

?
class A { public: A () { cout << 1 << endl;} }; class B: public A { public: B (int n) { try { if (n == 0) throw *this; if (n > 11) throw 11; } catch (int) { cout << 2 << endl; } catch (B&) { cout << 3 << endl; throw; } cout << 4 << endl; } B (B&) {cout << 5 << endl;} ~B () {cout << 6 << endl;} }; int main() { try { B b(0); B c (3); } catch (...) { cout << 7 << endl; cout << 8 << endl; }

}

5.3.

?
class X; void F(X & x, int n); class X { public: X() { try { F(*this, -2); cout << 1 << endl; } catch (X){ cout << 2 << endl; } catch (int) { cout << 3 << endl; } } X (X &) { cout << 12 << endl; } }; 30


class Y: public X { public: Y () {cout << 4 << endl;} Y (Y & a) {cout << 5 << endl;} ~Y () {cout << 6 << endl;} }; void F(X & x, int n) { try { if (n < 0) throw x; if (n > 10) throw 1; cout << 7 << endl; } catch (int) { cout << 8 << endl; } catch (X&) { cout << 9 << endl; throw; } } int main() { try { Y a; } catch (...) { cout << 10 << endl; cout << 11 << endl; }

}

5.4.

?
struct X; void f(X & x, int n); int const P = 1; int const Q struct X { X() { try { f(*this, catch (X) { catch (int) { } X (X &) { cout << 4 ~X () { cout << 5 }; struct Y: X { Y () { f(*this, cout Y (Y &) { cout ~Y () { cout };

= 1; int const R = 1; -1); cout << cout << cout << << endl; } << endl; } 1 << endl; } 2 << endl; } 3 << endl; }

-1); << 6 << endl; } << 7 << endl; } << 8 << endl; }

void f(X & x, int n) { try { if (n < 0) throw x; if (n > 0) throw 1; cout << 9 << endl; } catch (int) { cout << 10 << endl; } catch (X& a) { cout << 11 << endl; f(a, 1); cout << 12 << endl; throw; } }

31


int main() { try { Y a; } catch (...) { cout << 13 << endl; return 0; } cout << 14 << endl; return 0; }

5.5.

?
struct X; void f(X & x, int n); int const P = 1; int const Q = 1; int const R = 1; struct X { X(){ try { f(*this, 0); cout << 1 << endl; } catch (X) { cout << 2 << endl; } catch (int) { cout << 3 << endl; } } X (X &) { cout << 4 << endl; } ~X () { cout << 5 << endl; } }; struct Y: X { Y () { f(*this, -1); cout << 6 << endl; } Y (Y &) { cout << 7 << endl; } ~Y () { cout << 8 << endl; } }; void f(X & x, int n) { try { if (n < 0) throw x; if (n > 0) throw 1; cout << 9 << endl; } catch (int) { cout << 10 << endl; } catch (X& a) { cout << 11 << endl; f(a, 1); cout << 12 << endl; throw; } } int main() { try { Y a; } catch (...) { cout << 13 << endl; return 0; } cout << 14 << endl; return 0; } 32


5.6.

?
struct X; void f(X & x, int n); int const P = 1; int const struct X { X(){ try { f(*this, cout << } catch (X) { cout catch (int) { } X (X &) { cout << ~X () { cout << }; struct Y: X { Y () { f(*this, cout << } Y (Y &) { cout ~Y () { cout };

Q = 1; int const R = 1;

-1); 1 << endl; << 2 << endl; } cout << 3 << endl; } 4 << endl; } 5 << endl; }

1); 6 << endl; << << 7 << endl; } 8 << endl; }

void f(X & x, int n) { try { if (n < 0) throw x; if (n > 0) throw 1; cout << 9 << endl; } catch (int) { cout << 10 << endl; } catch (X& a) { cout << 11 << endl; f(a, 0); cout << 12 << endl; throw; } } int main() { try { Y a; } catch (...) { cout << 13 << endl; return 0; } cout << 14 << endl; return 0; }

5.7.

?
class Ex { int code; public: Ex(int i) : code(i) {} 33


Ex(const Ex& ex) : code(ex.code) {} int Get() const { return code; } }; struct Ex90 : Ex { Ex90() : Ex(90) {} }; void f() { throw Ex90(); cout << "dog" << endl; } void t() { try { f(); } catch(Ex90 &x) { cout<< "cat" << endl; throw Ex(x.Get() + 1); cout << "sheep" << endl; } catch(Ex &) { cout << "horse" << endl; } cout <<"cow" << endl; } int main() { try { t(); } catch(Ex &x) { cout << "elephant " << x.Get() << endl; } catch(...) { cout << "wolf" << endl; } return 0; }

5.8.

?
class Ex { int code; public: Ex(int i) : code(i) {} Ex(const Ex& ex) : code(ex.code) {} int Get() const { return code; } }; struct Ex60 : Ex { Ex60() : Ex(60) {} }; void f() { throw Ex60(); cout << "sword" << endl; } void t() { try { f(); } catch(Ex60 &x) { cout << "lance" << endl; throw Ex(x.Get() + 1); cout << "dagger" << endl; } catch(Ex &) { cout << "knife" << endl; } cout << "hammer" << endl; } 34


int main() { try { t(); } catch(Ex &x) { cout << "arche " << x.Get() << endl; } catch(...) { cout << "pistole" << endl; } return 0; }

5.9.

?
class Ex { int code; public: Ex(int i) : code(i) {} Ex(const Ex& ex) : code(ex.code) {} int Get() const { return code; } };

struct Ex51 : Ex { Ex51() : Ex(51) {} }; void f() { throw Ex51(); cout << "train" << endl; } void t() { try { f(); } catch(Ex51 &x) { cout << "plane" << endl; throw Ex(x.Get() + 1); cout << "helicopter" << endl; } catch(Ex &) { cout << "car" << endl; } cout << "truck" << endl; } int main() { try { t(); } catch(Ex &x) { cout << "boat " << x.Get() << endl; } catch(...) { cout << "rocket" << endl; } return 0; }

5.10. ?
void f(X & x, int n); struct X { X () { try { f(*this, -1); cout << "a"; } catch (X){ cout << "b"; } catch (int){ cout << "c"; } } X (X &) { cout << "d"; } 35


virtual ~X () { cout << "e"; } }; struct Y: X { Y () { try { f(*this, 0); cout << "f"; } catch (Y) { cout << "g"; } catch (int){ cout << "h"; } cout << "i"; } Y (Y &){ cout << "j"; } ~Y (){ cout << "k"; } }; void f(X & x, int n) { try { if (n < 0) else if (n else throw catch (int){ cout throw -n; == 0) throw x; n;} << "l"; }

} int main() { try { Y a; } catch (...){ cout << "m"; return 1; } cout << "n"; return 0; }

5.11. ?
void f(X & x, int n); struct X { X () { try { f(*this, 0); cout << "a"; } catch (X){ cout << "b"; } catch (int){ cout << "c"; } } X (X &) { cout << "d"; } virtual ~X () { cout << "e"; } }; struct Y: X { Y () { try { f (*this, 0); cout << "f"; } catch (Y) { cout << "g"; } catch (int) { cout << "h"; } cout << "i"; } Y (Y &) { cout << "j"; } ~Y () { cout << "k"; } }; void f(X & x, int n) { try { if (n < 0) else if (n else throw } catch (int) { cout }

throw -n; == 0) throw x; n; << "l"; }

36


int main() { try { Y a; } catch (...) { cout << "m"; return 1; } cout << "n"; return 0; }

5.12. ?
void f(X & x, int n); struct X { X () { try { f(*this, 1); cout << "a"; } catch (X){ cout << "b"; } catch (int){ cout << "c"; } } X (X &){ cout << "d"; } virtual ~X () { cout << "e"; } };

struct Y: X { Y () { try { f (*this, 1); cout << "f"; } catch (Y){ cout << "g"; } catch (int){ cout << "h"; } cout << "i"; } Y (Y &){ cout << "j"; } ~Y (){ cout << "k"; } }; void f(X & x, int n) { try { if (n < 0) throw -n; else if (n == 0) throw x; else throw n; } catch (int){ cout << "l"; } } int main() { try { Y a; } catch (...){ cout << "m"; return 1; } cout << "n"; return 0; }

5.13. ?
struct S { S ( int a) { try { if (a > 0) throw *this; else if (a < 0) throw 0; } catch ( S & ) { cout << "SCatch_S&" << endl; } catch (int) { throw; } cout << "SConstr" << endl; } S (const S & a) { cout << "Copy" << endl; } ~S ( ) { cout << "Destr" << endl; } }; 37


int

main ( ) {

try

{S s1(1), s2(-2); cout << "Main" << endl; cout << "MainCatch_S&" << endl; } { cout << "MainCatch_..." << endl; }

} catch (S &) { catch ( ... ) return 0; }

5.14. ?
struct S { S ( int a) { try { if (a > 0) throw *this; else if (a < 0) throw 0; } catch ( S & ) { cout << "SCatch_S&" << endl; } catch (int) { throw; } cout << "SConstr" << endl; } S (const S & a) { cout << "Copy" << endl; } ~S ( ) { cout << "Destr" << endl; } }; int main ( ) { try { S s1( 0 ), s2 ( 5 ); cout << "Main" << endl; } catch (S &) { cout << "MainCatch_S&" << endl; } catch ( ... ) { cout << "MainCatch_..." << endl; } return 0;

}

5.15. ?
struct S { S (int a) { try { if (a > 0) throw *this; else if (a < 0) throw 0;

} catch ( S & ) { cout << "SCatch_S&" << endl; throw;} catch (int) { cout << "SCatch_int" << endl; } cout << "SConstr" << endl; } S (const S & a) { cout << "Copy" << endl; } ~S ( ) { cout << "Destr" << endl; } }; int main ( ) { try { S s1(-3), s2(25); cout << "Main" << endl; } catch (S &) { cout << "MainCatch_S&" << endl; } catch ( ... ) { cout << "MainCatch_..." << endl; } return 0;

}

38


6. 6.1.

.
? , , ? , ?
class A { public: int y; void f() {cout << "f" << endl;} }; int A::y; int main () { A::y = 1; const A a; a.f(); return 0; }

6.2.

? , , ? , ?
class X { public: void g () {cout << "g" << endl;} int h (int n) {cout << "f" << endl; return n} }; int main () { int k; const X x; X::g(); k = x.h(5); return 0; }

6.3. ? , , ? , ? a)
class A { static int i; static void f() { g(); cout << "f()" << endl; } void g() { if (i >= 0) i = -1, f(); cout << "g ()" << endl; } }; int A::i = 1; 39


int main () { A::i = 1; A a; a.f(); a.i = 0; return 0; }

b)

class A { static int i; void f() { if (i >= 0) i = -1, g(); cout << "f()" << endl; } void g() { f(); cout << "g()" << endl; } }; int A::i = 1; int main () { A::i = 1; const A a; a.f(); a.i = 0; return 0; }

c)

class A { static int i; void f() const if (i < 0) g(i); cout << "f } void g(int & n) i = n; f(); cout << "g } }; int A::i = 1; int main () { const A a; a.g(2); return 0; }

{

()" << endl; {

()" << endl;

6.4. , :
a) int A::x; int main () { const A a; 40


a.x = 1; a.get_0(); return 0; } b) const char A::a = '+'; int main () { A ob; A::f(); return 0; } int main () { const A x; A::g(); x.h(); return 0; }

c)

6.5. , , . ( ). ?
class A { public: static void f(int x) { h (x); cout << "A::f," << x << } void g() { cout << "A::g" << endl; } void h(int x) { g (); cout << "A::h," << x << } }; class B: virtual public A { public: static void f(int x) { h (x); cout << "B::f," << x << } void g() { cout << "B::g" << endl; } void h (int x) { g (); cout << "B::h," << x << } }; int main() { B::f(0); B b; A * p = & b; p -> f(1); p -> g(); 41

endl;

endl;

endl;

endl;


p -> h(2); A::f(3); return 0; }

6.6. C D ? , , , main (). ?
class C { public: C(int x = 0) {} virtual int f(int x) { cout << "C::f," << x << endl; return h (x); }

virtual int g() { cout << "C::g" << endl; return 1; } virtual int h (int x) { cout << "C::h," << x << endl; return x; } virtual operator int () { return 99; } }; class D: public C { public: int f(int x) { cout << "D::f," << x return h (x); } int g(int x) { cout << "D::g" return 1; } int h(int x) { cout << "D::h," << x return x; } D(int x = 0) {} operator int () { return }; int main() { const D d; C const * const t = & d; t -> f(3); t -> f(d); t -> g(); t -> h(5); return 0; } 42

<< endl;

<< endl;

<< endl;

100; }


6.7. ( ) const, , .
a) class A { int i; public: A(int x) { i = x; } A(A & y) { i = y.i; } const A f(const A & z) { cout << endl; return *this; } }; const A t1( ) { const A a = 5; return a.f( a ); }

b)

class A { int i; public: A(int x) { i = x; } A(A & y) { i = y.i; } const A f(A & c) const { cout << c. i << endl; return *this; } }; const A t1(const A a) { A b = A(5); return b.f( a ); } class A { int i; public: A(int x) { i = x; } A(A & y) { i = y.i; } const A f( const A c) { cout << c.i << endl; return *this; } }; const A t1(const A * a) { A b = A(3); return a -> f( b ); }

c)

6.8. A. , , ++. .

43


a) class A {
int i; int f(int & x) { return g(x); } int g(int & x) { if (x >= 0) f(-i); return i; } }; int A::i = 2013; int main () { const A a; A::i = 201; a.f(20); return a.i = 1; }

b) class A {
int x; int y; int p() { return y >= 0 ? y = -1 : q(); } int q() const { return p(); } int r() { return x = y; } A(int z) { x = y > 0 ? z % y : -y; } }; int A::y = 13; int main() { A::y = 1; A::p(); const A b1(2013), b2(b1); b1.q(); return b2.x = 2; }

c) class A {
int void int void }; int A::m = 1; int main() { A::m3 (2013); A mm; return mm.m2 (3); } m; m1() { if (m < 0) m2(m); } m2(int & n) const { return m1(),n; } m3(int & n) { m = m2(n); }

44


7.



7.1. , main (). .
class class class class class class K L: M: P: Q: R: public public public public public K K L M P { { { { { { : void g () : void f () : virtual void h () : void f () : virtual void h () : virtual void f () virtual void h () { public: virtual void f () virtual void h () public public public public public public { { { { { { { { { cout cout cout cout cout cout cout cout cout << << << << << << << << << "K::g"; "L::f"; "M::h"; "P::f"; "Q::h"; "R::f"; "R::h"; "S::f"; "S::h"; } } } } } } } } } }; }; }; }; }; }; };

class S: public Q

int main (){ S os, * s = & os; int a, b; k = dynamic_cast l = dynamic_cast p = dynamic_cast r = dynamic_cast return 0; }

K * k; L * l; M * m; P * p; Q * q; R * r; (s); *>(k); *>(l); *>(q); s m q s = = = = dynamic_cast dynamic_cast dynamic_cast dynamic_cast (k); *>(s); *>(m); *>(p);

7.2. f9 () , .
struct B struct D: void f9 (B D * pd strcpy } { virtual void g () {} }; B { char y [100]; }; & b, D & d, int n) { = (n > 0) ? & d : (D *) & b; (pd -> y, "one_variant\n");

7.3. putnull () , .
struct B { virtual void empty () {} }; struct D: B { int mas [30]; }; void putnull (B * pb){ D * pd = (D *) pb; if (! pb) return; for (int i = 0; i < 30; i++) pd -> mas [i] = 0; }

7.4. puthi () , , .
struct B { virtual void hi () { cout << "Hi!" << endl; } }; struct D: B { char txt [10] [4]; }; void puthi (B * pb, D * pd) { int i = 10; if (! pb) return; pd = (D *) pb; while (i) strcpy ((pd -> txt) [--i], "Hi!"); } 45


7.5. ++? , . 7.6. f (), , A*, , B*, .
struct struct B * f (A int main try A { virtual void z () {} }; B: A { int x; B (int y = 5) { x = y; } }; * pa); (){ { B b, * pb = f (& b); cout << pb -> x << endl; A a; pb = f (& a); cout << pb -> x << endl;

} catch (...) { } return 0; }

7.7. f (), , A, C, , .
struct A { virtual void z () {} }; struct B: A { }; struct C: B { int x; C (int n = 3) { x = n; } }; C & f (A & ra); int main () { C c, & pc = f (& c); cout << pc.x << endl; return 0; }

7.8. f (), , void *, B, , .
struct A { virtual void z () {} }; struct B: A { int x; B (int n = 7) { x = n; } }; B * f (void * p); int main () { B b, * pb = f (& b); cout << pb -> x << endl; return 0; }

46


7.9. f (), , B, C, , .
struct A struct B: A struct C: B C * f (B & rb); int main () { C c, * pc = cout << pc return 0; } { int x; virtual void z () {} }; { int x; }; { int x; C (int n = 4) { x = n; } };

f (c); -> x << endl;

7.10. ++? ++ ? ? ? , .

8.



8.1. ++. 8.2. ? , , .
template void funcA (double d = f) { /*...*/ } template class A { /*...*/ }; template class B { /*...*/ }; template void funcB (int t = n) { /*...*/ } template class C { /*...*/ }; template struct D { /*...*/ }; template void funcC (const Cs& ref) { /*...*/ } class myclass { public: myclass() {} }; template class E { /*...*/ }; struct mystruct { int a, b; }; template void funcD (mystruct *p = 0) { /*...*/ } template void funcE (mystruct *p = &a) { /*...*/ } template void funcF (mystruct *p = 0) { /*...*/ }

8.3. ++? , , . . main ()?

47


class complex { double re, im; public: complex (double r = 0, double i = 0) { re = r; im = i; cout << "constr" << endl; } operator double () { cout << "operator double " << endl; return re; } double get_re () { return re; } void print() const { cout << "re=" << re << " im=" << im << endl; } }; template T f (T& x, T& y) { cout << "template f" << endl; return x > y ? x : y; } double f (double x, double y) { cout << "ordinary f" << endl; return x > y ? íx : íy; }

int main () { complex a(2, 5), b(2, 7), c; double x = 3.5, y = 1.1; int i, j = 8, k = 10; c = f (a, b); cout << "c = "; c.print (); x = f (a, y); cout << "x = " << x <
8.4. ? , , . . main ()? max () , ( ).
template T max (T& x, T& y) { return x > y ? x : y; } 48


int main () { double x = 1.5, y = 2.8, z; int i = 5, j = 12, k; char * s1 = "abft"; char * s2 = "abxde", * s3; z = max (x, y); cout << "z = "<< z << endl; k = max (i, j); cout << "k = "<< k << endl; z = max (x, (double &) i); cout << "z = "<< z << endl; z = max (y, (double &) j); cout << "z = "<< z << endl; s3 = max (s1, s2); cout << "s3 = "<< s3 << endl; cout << " !" << endl; }

return 0;

8.5. , -- ? , .

8.6.
template class fr { T n; T d; ... };

( - ) , , - : '+', , ( ë+¨). '+', , . '-', ( ëí¨). '-', , . '*', , . '/', , . '=', , . '+=', , .
49




'-=', , . '*=', , . '/=', , . '==', , . '!= ', , . '<', , . '>', , . '<=', , . '>=', , . '++', 1 . '--', 1 . '<<', ë/¨. , .

9.

STL

9.1. . ? 9.2. STL. 9.3. vector list? 9.4. ? 9.5. . 9.6. STL? ? 9.7. ? . ?

50


9.8. ? . 9.9. , . 9.10. STL, . , - . - . 9.11. : ë , , , , 1¨? , , .
#include #include using namespace std; int main () int g (list & lst){ int S=0; list :: iterator p = lst.begin(); while (p != lst.end()) { S += * p; p += 2; } return S; };

9.12. , - list , , . 9.13. , - list , ( cout). 9.14. , , , vector , cout. 9.15. , ëYes¨ ëNo¨ , x - list . 9.16. , - vector , .

51


9.17. , ( ) - list , . 9.18. g () : vector , - list , -- . , , 1, , , . , . . . 9.19. g () : - vector , - list , í . , , ( ), . . 9.20. g () : list , - vector , -- . , 1, -, , , . . 9.21. g () : - vector , - list , -- . , , ( ), . . 9.22. g () : vector , - list , í . , , 1, , , . , . . , . 9.23. g () , - . , ( , 52


. .). . 9.24. g () , - . , , , , , , , . . 9.25. g () , - . , , , , , , , , . . 9.26. g () , - - . , , , ( , ). . 9.27. g () , - . , , , . . 9.28. g () , - . , , . . 9.29. - ( , STL, , ), STL , , main(), - 5 -. 9.30. - ( , STL, , ), STL , , main(), - 5 -.

53


9.31. - ( , STL, , ), STL , , main (), - 5 -. 9.32. :
typedef vector V; struct Weight_t { V::size_type Index; float Weight; }; typedef list L; // //

g L, . 9.33. :

(), V , ( ), -

typedef vector V; struct Signif_t { V::size_type Index; // bool Signif; // (true í ) }; typedef list S;

g (), V S, , , . 9.34. :
typedef vector B; struct Value_t { B::size_type Index; int Value; }; typedef list T; // //

g (), B T, , , , . 9.35. :
struct Value_t { bool Signif; int Value; }; typedef vector< Value_t> B; typedef list< B::size_type> T; 54 // (true í ) //


g (), B T, , , , , .

II.



1. (). . 1.1. 2, 3 4 f (). :
class A { int a, b; public: A (const A & x) { a = x.a; b = x.b; cout << 1; } A (int a = 0, int b = o) { this -> a = a; b = a; cout << 2; } };

: : : 1.2. 1.4.
A ( int a = 0,

2221211 22221

int b = 1 ) {...}

class A int public: A( int int };

{ x; int y ) { x = y; } get () { return x; } operator *= ( int y ) { return

x=

x*y; }

1.16. Cons Copy Cons Des Copy Des Des 1 Des 1.17. Cons Cons Des Copy Des 3 Des 1.18. Cons Copy Cons Des Des 1 Des

55


1.19. 1) virtual. 2) (float a = 0), D ( ) : B (0) {...} 1.20. 1) virtual. 2) (int a = 0) {...} 1.21. 1) virtual. 2) D (int d = 0) {...}

2. . 2.1. ë Cls¨, : class Cls { void operator= (const Cls &) {} public: Cls () {} }; 2.2
a) void g (double a, int b = 0 ); void g (int, const char * = 0);

double, char... b)
void f (const char * a = 0 ); void f (int a, int b = 0);

char... e) 2.3. a)
f(); f(s); f(f); f(b); f(A); // // // // // // f(int), f(int), f(double f(int), f(int), (a) , - ; () ),() () () void f (double d = 0, const char * s = 0); void f (const char *); void f (const char *, int i = 0);

b)

f(); // () f(4); // () f(f); // f(double), () f(d); // f(double), () f(ld);// f(long double),() f(s); f(i); f(b); f(f); f(d); // // // // // f(int), () f(int), () f(int), () () () 56

c)


3. 3.1.

. a) void B::g() {

f(); f(1); f(5 , 1); x = 2;

// // // //

, f B::f(1); f , int x private

} int main () { B b; // A(), B() f(5); //, f f('+', 6); //::f('+', 6); b = ret (b, b); // A(const A &), B(const B &) return 0; }

:
A(), B(), A(const A &), B(const B &), ~B(), ~A(), ~B(), ~A().

b) void B::g() {
f(1.2); f(); a = 2; } int main () { B d; f(); f(6); empty return } // B::f // f // A::a = 2

// //::f (d, d); 0;

:
A(), B(), A(const A &), B(const B &), ~B(), ~A(), ~B(), ~A().

3.2. )
C::x C::f C::x C::x = A::f (); (3); = ::f (4, 5); = 6;

c.A::f (); c.C::f (7); ::x =::f (`8', 9);

() . :
A(), B(), C(), B(const B &), A(const A &), ~A(), ~B(), ~C(), ~B(), ~A()

b)

x= x= x=

A:: f (); B:: f (5); B:: f (6, 6); 57


x= B:: f (5); x = a.A:: f (); x = a.B:: f (7); return a.B::g (& a, & a);

() . :
A(), B(), A(), B(), A(const A &), C(), ~C(), ~A(), ~B(), ~A(), ~B(), ~A()

c)

C::x = A::f (); C::f (3); C::x = ::f (4, 5); C::x = 6; //main() c.A::f (); c.C::f (7); ::x =::f (`8', 9);

() . :
A(), B(), C(), B(const B &), A(const A &), ~A(), ~B(), ~C(), ~B(), ~A().

3.3. m ë::¨ B , , :
fA.C::m = 0; return *((* f).e = & fA.C::m);

( 4 , B -- ):
class B: virtual public A { public: int * p; }; class C: virtual public A { public: int * c; };

3.5. . W h () Z , :
1 pz = (Z *)(X *)(new W); 2. W X X Z: class X: public W 3 { ... }; class Z: public X,Y { ... };

. w h ():
(*pz).X::w=& hi;

4. , class struct.

58


4. . 4.1. 1. , -- . 2. virtual. 3. , (, ) . 4. ( ) .
class class void void B { public: virtual void print (); }; P: public B { public: void print (); }; B::print () { ... } P::print () { ... } ps -> print ();

B * ps = new P; ...

4.2.

int main( ){ X a; Z b; X * p = &b; p -> g(1.5); // Z::t // Z::h // Z::g p -> h(); // X::t // X::h p -> t(5); // , X::t í }

4.4.

int main( ){ K k; P p;

K *t = &p; K::f K::f K::g P::f P::g P::h

t -> f (0.7); // t -> g (); // // t -> h(); // // // }

4.6. . : D::h
D::g

D::h,2 D::h

D::f,2 D::h

D::h D::h,3

D::h,1

4.7. p->h (2) ( h (int) h () , ). : U::h,1 U::f,1 T::h T::g T::h 4.10. dog
sheep result = (33 ; 4) horse cat 59


4.11. g () , , . 4.12. S, , S . 4.13. B g () B, , .
4.14. f (int) from B 10 10 10 10

4.15. add_st (K*) from K 5 5 5

4.16. g ( ) from T 7 7 7

5. 5.1. :
12 ì 9 ì 2 ì 4 ì 6 ì 11

5.6. 5.7. 5.8. 5.9.

4 ì 11 ì 9 ì 12 ì cat elephant 91 lance arche 61 plane boat 52

2 ì 5 ì 10 ì 6 ì 8 ì 5 ì 14

6. . 6.1. :
class A { public: static int y; void f() const {cout << "f" << endl;} };

6.3. a) 1. class struct, public:. 2. C g () .
60


b) 1. class struct, public:. 2. f () g () const. 6.4. a)

class A { public: static int x; void get_0() const {return;} }; class A { public: static const char a; static void f(){} };

b)

6.5. , f () . f () h (): :
B::f,0 A::f,1 A::g A::g A::h,2 A::f,3

7. 7.1.

. .
k = s; // s = dynamic_cast // l = dynamic_cast m = s; // p = dynamic_cast q = dynamic_cast
(k); // í (k); // í //

(l); *>(m); *>(q); (p); // L í



// P í

7.2.

f9 () typeid ( dynamic_cast):
void f9 (B & b, D { D * pd = (n > if (typeid (* strcpy (pd -> } } & d, int n) 0) ? & d : (D *) & b; pd) == typeid (d)) y, "one_variant\n");

61


7.6. f ():
B * f (A * p) { B* pb = dynamic_cast (p); if (pb) return pb; else exit (0); }

8. 8.2. ( class, typename, struct), , , , , ë -¨. double, float, myclass, mystruct, struct . :
template template template template template void funcA (double d = f) { /*...*/ } class A { /*...*/ }; class E { /*...*/ }; void funcE (mystruct *p = &a) { /*...*/ } void funcF (mystruct *p = 0) { /*...*/ }

8.4. :

z = 2.8 k = 12 z = 1.5 z = 2.8 s3 = abft !

9. STL

9.15.
void f (const int x, const list & l) { list::const_iterator p = l.begin (); while (p != l.end ()) if (* p == x) { printf ("Yes\n"); return; } printf ("No\n"); }

62


III.
1. Standard for the C++ Programming Language ISO/IEC 14882, 1998. 2. . C++. ./.

. -- .: ë¨, 2005. 3. ., .., .. . ++. -- .: , , 2011.í112.

63


I. 1. (). 2. . 3. . 4. . 5. 6. 7. 8. 9. STL 1. (). 2. . 3. . 4. . 5. 6. 7. 8. 9. STL

1 11 13 22 29 39 45 47 50

II.

55 56 57 59 60 60 61 62 62 63

III.

64