Вы находитесь на странице: 1из 94

Programiranje na C++

C++
: ..

1.

:
"" , ,
C++.
2.

, , ,
, , -,

3.
.
: , ,
, , : ,
, , ,
4.

. . .
. , .
.
5.

- .
. : , , this, ,
, . .
6.
.
.
. . .

-
http://www.INTUIT.ru

C++
1. : : PDA
:
"" ,
, C++.


"C/C++.
" [18], 2001
.
,
. , ,
. 2010
- "
" -
.
C/C++ [19] C#
[20, 17], .
. 250
20
( [18]) 1000
. . http://ips.ifmo.ru

.

++ ISO/IEC 14882 (2003).
, , gcc
Microsoft Visual C++ 2005 Express Edition.
.
,
pta-ipm@yandex.ru.

.
:
, , .
, (
), - , - .

. C++ :

(_),
;
0 9;

, +, *, { &;

- , ,
.

. ,
2

" " - ,
.
.

.
. .

();
;

().

, ,
.
.
, 128 ( 12), Vasia,
goto +.
.

. ,
a + b a b.
.

, .
.
,
. , int a; -
a.
, - ,
. .
.
,
.

.

, , 3

. .
.
"" (, ),
# (""), .
,
.
( ++
" ") ,
.
() ,
, .
, .
()
.
.
,
(, , ).
, .
, : Windows
.NET. Windows, . .NET
- CIL.
.
Windows ( - , )
,
,
, (,
). ,
.
.exe .
.NET
CLR. .NET
, .

,

, , , ,
. :
[ { void | int } ] ();
,
4

.
++.
()
- .
, .
.
.
.
, , ..,
.
:


;
;
, ,
.

.
, ,
, , , ,
, 1, l I (, L i).

- ,
.
, . ++.
asm
auto
bool
break
case
catch
char
class
const
const_cast
continue
default

do
double
dynamic_cast
else
enum
explicit
export
extern
false
float
for
friend

if
inline
int
long
mutable
namespace
new
operator
private
protected
public
register

return
short
signed
sizeof
static
static_cast
struct
switch
template
this
throw
true

typedef
typeid
typename
union
unsigned
using
virtual
void
volatile
wchar_t
while


- ,
. . ,
, , , &&, | <,
, reinterpret_cast new.
,
.
().

. , ,
, . ,
, .
.
, , 1.1.
1.1. ++

true false

:
, ,

: ,
(0, 1, 2, 3, 4, 5, 6, 7)
: 0 0,
(0, 1, 2, 3,
4, 5, 6, 7, 8, 9, A, B, C, D, E, F)
: [].[]
: [][.][]{E|e}[+|-]
[]

true, false
8, 0, 199226

01, 020, 07155


0xA, 0x1B8, 0X00FF,
0X00ff
5.7, .001, 35
0.2E6, .11e-3, 5E10,
1.22E-10
'A', '', '*', 'db', L'A',
'\n', '\012', '\x07\x07'
" Vasia",
"\t =\xF5\n",
L" Vasia"


1.2.
1.2. IBM PC


()
bool
true false
1
signed char
-128 127
1
unsigned char
0 255
1
6

signed short int


unsigned short int
signed int
unsigned int
signed long int
unsigned long int
float
double
long double

-32 768 32 767


0 65 535
-2 147 483 648 2 147 483 647
0 4 294 967 295
-2 147 483 648 2 147 483 647
0 4 294 967 295
3.4e-38 3.4e+38
1.7e-308 1.7e+308
3.4e-4932 3.4e+4932

2
2
4
4
4
4
4
8
10

,
(-), , -218.

.
10 (, 1.3e12 1,3 *
1012). ,
, , , . .
- ,
().
. .
L,
. L , , .
char, - wchar_t ( -
).
.

.
(escape sequence)
- , \ (backslash).
, ' (), " (), ? ()

.
:
\ooo
\xhhh
ooo
, hhh - ,
.

(, ).
ooo (hhh) .
. '\0' (L'\0' )
7

,
.

, . ,
, ,
, :
" \"\""
, , :
" ''"
, ,
.
,
,
. ,
. ,
" \
, \
"

" , "
.
, , ()
.


. #.
. ,

#include "myfile"

myfile. ,
.
. ++
: .
8

// (" ") .
/* */.
.
.
++
. ,
, .
, ,
.
. , ,
.
:

(
);
( ).

, ,
, .
,
.
, .
, , .
++ .
++ (, ) .
,
, .
. - ,
. ,
, , , , .
,
.
1.1 ,
++.

. 1.1. ++

, void,
, .
:

bool ();
char ();

wchar_t ( );

int ();

float ();

double ( ).

(), -
. ,
, .
,
:

10

short ();
long ();

signed ();

unsigned ().

14
, . 1.1. , char, signed char unsigned char -
.


. ++
.
signed char unsigned char :
. .

true false,
.
. true
1, false - .
.

: char, signed char
unsigned char. - .
- :
sizeof(char) = sizeof(signed char) = sizeof(unsigned char) = 1
, ,

. char :
signed char, unsigned char.
,
,
, .

++ 8 :
(signed char, short int, int, long int) (unsigned char,
unsigned short int, unsigned int, unsigned long int).
, signed .
unsigned .

11


short int, long int, signed int unsigned int short, long, signed
unsigned .
, ,
signed int. - ,
L, l (long) U, u (unsigned).
, 32L long 4 .
L U , , 0x22UL 05Lu.
. ,
-
, :
sizeof(char)

sizeof(short)

sizeof(int)

sizeof(long).

, int ,
. IBM PC 1.2.
Visual ++.NET 2005 .
, long long -9 223 372 036 854
775 808 +9 223 372 036 854 775 807 8 .

++ :
float, double long double. .
.
, - .
double.
F, f (float) L, l (long). ,
2E+6L long double.

.
void
void , ,
,
.
2. : : PDA
, ,
, , ,
-,

12


.
++ , .
. - ,
( ).
. ,
, , ,
: y = sin(x);.
( ) main.
.
.
:
, , , . .
.
(one definition rule, odr).
, .

:
(, ),
.
, , , :

int main()
{

}
,
.

.
. - ,
, . ,
,
. .
.
a :
x:
int a; float x;

13

,
, , , .
, .
- ,
.
.
( ),
, - ,
. ,
,
, .
.

.

, .
:
"", , - .
,
. - ,
.
. ,
, .
, .
, , ,
::.
:
[_] [const] [];
.
( ).
- :
=
:
( )
, :
short int a = 1;
short int b( a + 1 );

14

,
- .
, (
- ).
const , ,
.
. const , .
,
. :
const char C = 'C',
X = '|'; // X
int const y = 56;
// y
char s, sf = 'f';
// sf
char t (54);
float c = 0.22, x(3), sum;
.
, ( ,
main), .
,
.
, . , ,
"" "" . -
.
static .
( ),
, .

.
, .
extern ,
. ,
extern, ( ,
). ,
. ,
- .
.
extern ,
, :
extern double PI = 3.1415926; // PI
,
. .
int a;

// 1 a
15

int main()
// 2
{ int b;
// 3 b
static int c; // 4 c
a = 1;
// 5
int a;
// 6 a
a = 2;
// 7
::a = 3;
// 8
extern int x; // 9 ;
...
return 0; // 10
}
// 11
int x = 4;
// 12 x
a ,
.
. -
, 6-10,
,
.
b c - , ( ) - ,
: b "" ""
, c, a,
, .
main.
- , .
, extern
( 9).
,

. ()
.

/
- .
: .
++ / -
, . .
//
#include <iostream>
// 1
using namespace std;
// 2
int main()
// 3
{ const double course = 35.8;
// 4
double sum;
// 5
cout << " :\n"; // 6
cin >> sum;
// 7
double euro = sum / course;
// 8
cout << " : " << sum
<< " : " << euro
<< "\n";
// 9
16

return 0;
// 10
}
2.1.
, .
.
#include <_>
( )
, .
<iostream>,
/.
2 ,
, <iostream>. ,

( ) std.
,
, .
,
, ,
, . .

,
std, " " .h, ,
, - ,
<iostream.h>, <stdio.h>. , ,
c.
.
3 ( ) ,
4 5 : course
sum. 6 .
" :"

\n. cout <<
, , .
, 9.
, ,
.
, .
( 7) :
, ,
,
sum. ,
: ,
. ,
>>.
17

8 euro,
sum course. 10
0 - .
return 0 ,
0 .
.
2.2.
// /
#include <stdio>
// 1
using namespace std;
// 2
int main()
// 3
{ const double course = 36.5;
// 4
double sum;
// 5
printf( " \n" ); // 6
scanf( "%lf", &sum ); // 7
double euro = sum / course;
// 8
printf( " : %lf : %lf\n", sum, euro ); // 9
}
2.2.
, ,
<cstdio>,
/. printf 6
" :"
\n. scanf 7
sum. &
sum. scanf
.
-, :
int scanf ( _, )
//
int printf( _, )
//

. ,
,
, %,
.
.
%d , %f
float, %lf - double. 9
, %lf
sum euro.
(
, ),
. /
, , -
18



. ,
, , , ,
.
, ,
.
().
MS DOS Windows.
Windows , MS DOS,
MS DOS
Windows. Visual Studio.NET 2005
Windows, .
, , ,
"".
. -
. - ,
setlocale, (
<clocale>):
setlocale( LC_ALL, "Russian" );
setlocale( LC_ALL, "rus" );
,
- printf, cout.
, ,
Windows
.

.
, , .

. :
( a + 0.12 ) / 6
x && y || ! z
( t * sin( x ) - 1.05e4 ) / ( ( 2 * k + 2 ) * (2 * k + 3 ) )
a=b=c
b++
cout << a
i++, j++
. ,
. , ,
19

, , .
.
. .
++ :
, , , , ,
, . . 2.1 ,
.
2.1. ++



++

-
sizeof

-
++

!

( )
+

(<>)


*

%

+

<

<=

>

>=

==

!=

&&

||

?:
()
=

*=

/=

%=

+=

-=

,

,
20

. ++
,
. , ,
: , ,
, , ,
.
,
,
, - . , a = b = c a = (b = c), a + b + c
(a + b) + c.
: ,
, (sin(x + 2) + cos(y + 1))
, .
: ,, ?:, && ||.
. ,
a b:
int a = 2, b = 5;
a + b 7 int. ++
L- (L-value) R- (R-value).
:

, L- ( Left - ),
, ;
, R- ( Right - ),
,
.

L-, R-.
- L-, - R-.
R- . L ,
, . ,
L-.
.
(=, +=, -=, *= . .).
(=):
_1 = _2
L-, - .
, ,
,
( : " - """).

, . ,
21

, , .
, ,
, ( -
).

. , .
L-,
.
, a = b = c:
b = c,
a.

. .
:
p>#include <iostream>
using namespace std;
int main()
{ int a = 3, b = 5, = 7; //
a = b; b = a; = + 1; //
cout << "a = " << a
<< "\t b = " << b
<< "\t c = " << c;
= a + ( b = 5 );
cout << "\t c = " << c;
}
:
a=5

b=5

c=8

= 10

(+=, *=, /= . .) ,
, L- . ,

, . , a +=
b a = a + b.
1 (++ --). ,
, - ,
, .
, .
,
:
#include <cstdio>
using namespace std;
int main()
{ int x = 3, y = 3;
22

printf( " : %d \n", ++x );


printf( " : %d \n", y++ );
printf( " : %d \n", x );
printf( " : %d \n", );
}
:
: 4
: 3
: 4
: 4
,
, .
(-, !). ( -)
.
(!) false, true,
true, false. .
bool.
(/) (%).
. ,
( ). , 5/2
2. ,

, .
.
.
#include <cstdio>
using namespace std;
int main()
{int x = 11, y = 4;
float z = 4;
printf( " : %d %f \n", x / y, x / z );
printf( ": %d \n", x % y );
}
:
: 2 2.750000
: 3
(<, <=, >, >=, ==, !=) .
.
true false.
, .

(==)
(=), , .

23

(&& ||). (&&) (||)


, bool,
. true false.
true true.
true,
true. .
, , .
(?:). ++ ( ),
:
_1 ? _2 : _3
.
, bool. true,
, -
. .
: , 1,
n, 1.
:
i = ( i < n ) ? i + 1 : 1;
"" (,). :
_1, _2
:
( ),
.
, .
, .
, j = 2, i = 1 1, j = 2, j++ 2,
j = 2, ++j 3.
,
"".
.
, , .


.
, . ,
.
, :
( char, short ) -> int -> unsigned int ->
long -> unsigned long -> float ->
double -> long double
, int double
, "" "". char short
int.

24

"" .
,
"" "", , float int.
( ),
().
.

bool. , ( ), true,
false. bool
true 1, false - 0.
. (
) :
( )
( )
, :
int a = 2;
float b = 6.8;
printf( "%lf %d", double ( a ), ( int ) b );
a double, b - int
( ).
++ .
, ,
.
++ ,
:
static_cast<>( )
reinterpret_cast<>( )
dynamic_cast<>( )
const_cast<>( )

. static_cast<T>(e)
. . reinterpret_cast
. ,
,
. const_cast
const . dynamic_cast

.
.
3. : . :
PDA
: , , ,
, : , , , ,


25

.
,
, , .
.
,
( ).
,
- .
.
,
(. . 3.1),
.

. 3.1.
.
, .

""
, , ,
.
; ( , ,
- ). :
i++;
a* = b + c;

//
//


if
if

26

. . 3.1. :
if ( ) _1; [else _2;]
,
. , , - .
, .
.
- ,
. ,
.
:
if (a<0) b = 1;
if (a<b && (a>d || a==0)) b++;
else {b* = a; a = 0;}
// 2
if (a<b) {if (a<c) m = a; else m = c;}
else
{if (b<c) m = b; else m = c;}

// 1

// 3

1 else. " ",


,
.
, .
, 2 ,
a<b . ,
, - .
3 .
.

. 3.2.
. , . 3.2.
.
#include <iostream>
using namespace std;
int main()
{float x, y; int kol;

27

cout << " \n";


cin >> x >> y;
if ( x*x + y*y < 1 ) kol = 2;
else if ( x*x + y*y < 4 ) kol = 1;
else kol = 0;
cout << "\n : " << kol;
}
switch
switch ()
. :
switch ( ){
case __1: _1;
case __2: 2;
...
case __n: n;
[default: ;]}
(
), ,
, ,
, .
,
. .
, , default (
switch ).
( 4 ):
#include <iostream>
using namespace std;
int main()
{
int a, b, res;
char op;
cout << "\n 1 : ";
cin >> a;
cout << "\n : ";
cin >> op;
cout << "\n 2 : ";
cin >> b;
bool f = true;
switch (op)
{
case '+': res = a + b; break;
case '-': res = a - b; break;
case '*': res = a * b; break;
case '/': res = a / b; break;
default : cout <<"\n ";
f = false;
}
if (f) cout << "\n : " << res;}
break return.

28


.
, ,
, ,
(. 3.3), (
), ( ).

. 3.3.
. ,
,
. , ,
.
.
, break, return .

(while)

29

, . 3.3 (), :
while ( ) ;
,
. 0 (), ,
. 0,
. .
( y=x2+1 ):
#include <cstdio>
using namespace std;
int main()
{
float Xn, Xk, Dx;
printf(" : ");
scanf("%f%f%f", &Xn, &Xk, &Dx);
printf("| X | Y |\n");
//
int X = Xn;
//
while (X<=Xk)
{
//
printf("| %5.2f | %5.2f |\n", X, X*X + 1); //
X += Dx;
//
}
}

(do while)
, . 3.3 (), :
do while ;
, ,
. 0 (), ,
, .
.
( ):
#include <iostream>
using namespace std;
int main(){
char answer;
do{
cout << "\n ! ";
cin >> answer;
}while (answer != 'y');}

(for)
:
for ( ; ; ) ;

30

:
for ( int i = 1; i < 6; ++i )
printf( " %d ", i );
printf( " " );
,
. ,
.
: 0 (),
.

. .
.
for ( !).
"" ( ), :
for (int i = 1, s = 0; i<=100; i++)
s += i; // 1 100
( y=x2+1 ):
#include <cstdio>
using namespace std;
int main()
{
float Xn, Xk, Dx;
printf(" : ");
scanf("%f%f%f", &Xn, &Xk, &Dx);
printf("| X | Y |\n");
for (int X = Xn; X<=Xk; X += Dx)
printf("| %5.2f | %5.2f |\n", X, X*X + 1);
}


++ , :

break;
continue;

return;

goto;

throw.

break
switch. .
continue ,
, (
for) .

31

return
. .
goto :
goto ;
:
: ;
goto . -
, ,
.
:


;
(,
- ).

.
( )
throw, . ,
- , ,
. , ++,
.
break.
eps
:
sh x = 1 + x3/3! + x5/5! + x7/7! +
, ,
, .
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
const int MaxIter = 500;
//
double x, eps;
cout << "\n : ";
cin >> x >> eps;
bool done = true;
//
double y = x, ch = x;
//
for (int n = 0; fabs(ch) > eps; n++)
{
ch *= x * x /(2 * n + 2)/(2 * n + 3); //
y += ch;
if (n > MaxIter)
{

32

cout << "\n !";


done = false; break;
}
}
if (done)
cout << "\n : " << y;}

.
, - .
++ , :
, .
/ .
, ,
.
. ,

.
.
- ,
. .
,
. " ,
":
(*) ( __ );
, :
int (*fun) (double, double);
fun , int
double.
,
. :
*;
, ,
, .
, ,
, . ,
int *a, b, *c;
a c, b.
. ,
, int **p;. -
. ( )

33

.
void ,
, -
, ,
.
,
. :
int i;
//
const int ci = 1;
//
int * p0 = &i;
// 1 -
// int * p1 = &ci;
// 2 - !
int * p2 = const_cast<int*>( &ci ); // 2.1
const int * pc0 = &ci;
// 3 -
int const * pc2 = &ci;
// 3.1 -
const int * pc1 = &i;
// 4
int const * pc3 = &i;
// 4.1
int * const cp0 = &i;
// 5 - -
// int * const cp1 = &ci; // 6 - !
const int * const cpc0 = &ci; // 7 - -
const int * const cpc1 = &i; // 8
,
.


.
, ,
.
-
.
++ .
malloc , new
delete.
:
1. :
o :
o int a = 5;
//
o int* p = &a; // a
o int* p (&a); //
o :
o

o int* r = p;
, :
o int b[10];
o int* t = b;
o ...

//
//

34

o void f(int a ){ /* */ } //
o void (*pf)(int); //
o pf = f;
//
2. :
3. char* vp = (char *)0xB8000000; //
4. :
5. int* suxx = NULL; //
6. int* rulez = 0; // -
7. :
o

new:

o int* n = new int;


o int* m = new int (10);
o int* q = new int [10];
o malloc:
o int* u = (int*)malloc(sizeof(int));

// 1
// 2
// 3
// 4

, new,
delete, , malloc - free.
- .
:
delete n; delete m; delete [] q; free (u);

- ,
. - .


: (*), ,
, , (++), (- -), ,
. (&).
, , ,
. ,
( ):
char a;
// char
char * p = new char;/* */
/* char */
//
*p = ''; a = *p;
.
.
:
, , :
unsigned long int A=0X77ffaa;
unsigned int* pint =(unsigned int *) &A;

35

,
.
++ static_cast reinterpret_cast, :
void *pointer = static_cast<void *>( &a );
pointer = reinterpret_cast<void *>( pa );

reinterpret_cast. static_cast
, "".

, void*.
bool.
void*
.
( ) .
( , ,
) , .

, ,
, .
, -
.
sizeof().
- , .
.
.
,
*p++ = 10;
:
*p = 10; p++;
(*p)++, , , .
& ,
. ,
.

. , -
, , . ,
. ,
.
, .

36


. ,
. :
& ;
- , , & - , ,
, :
int kol;
int& pal = kol;
// pal - kol
const char& CR = '\n';
//
, , :
const char& CR = '\n';

//

.
, .

- , ,
, extern
.
.

, .

,
.

, ,
, .


.
, , .
.
++
().
. .
, ,
.
.

,
():
float a [10];

// 10

- ++, ,

37

, void. .
. , ,
.
:
int b[5] = {3, 2, 1};

// b[0]=3, b[1]=2, b[2]=1, b[3]=0, b[4]=0


.
()
. .
#include <iostream>
using namespace std;
int main(){
const int n = 10;
int marks[n] = {3, 4, 5, 4, 4};
for (int i = 0, sum = 0; i<n; i++) sum += marks[i];
cout << " : " << sum;}
.
new,
, :
float *p = new float [100];
- float,
, 100 ,
p.
, .
,
.
, new [],
delete [], , malloc -
free, :
delete [] p; free (q);
, .
" ".
, :
float *p = new float [n]();

, ,
int matr [6][8];
6 8 .
.

38

, .
, , matr[i]
[j].
:
int mass2 [][] = { {1, 1}, {0, 2}, {1, 0} };
int mass2 [3][2] = {1, 1, 0, 2, 1, 0};

( ):
matr = new int [a] [b];
-
delete []. .
. ,
, .
#include <stdio>
using namespace std;
int main()
{
const int nstr = 4, nstb = 5; //
int b[nstr][nstb];
//
int i, j;
for ( i = 0; i < nstr; i++ ) //
for ( j = 0; j < nstb; j++ )
scanf( "%d", &b[i][j] );
int istr = -1, MaxKol = 0;
for ( i = 0; i < nstr; i++ ) //
{
int Kol = 0;
for ( j = 0; j < nstb; j++ )
if (b[i][j] == 0) Kol++;
if ( Kol > MaxKol )
{ istr = i; MaxKol = Kol; }
} printf( " :\n" );
for ( i = 0; i < nstr; i++ )
{
for ( j = 0; j < nstb; j++ )
printf( "%d ", b[i][j] );
printf( "\n" );
}
if ( istr == -1 ) printf( " " );
else printf( " : %d", istr );
}

:
#include <iostream>
using namespace std;
int main()
{

39

int i, j, n;
cout << " , B, R ";
cin >> n;
//
float **a = new float*[n]; //
for( j = 0; j < n; j++ )
a[j] = new float [n];
float **b = new float*[n]; //
for( j = 0; j < n; j++ )
b[j] = new float [n];
float **r = new float*[n]; // R
for( j = 0; j < n; j++ )
r[j] = new float [n];
//
for ( i = 0; i < n; i++ )
{
for ( j = 0; j < n; j++ )
cin >> a[i][j];
cout << endl;
}
cout << endl;
for ( i = 0; i < n; i++ )
{
for ( j = 0; j < n; j++ )
cin >> b[i][j];
cout << endl;
}
cout << endl;
for ( i = 0; i < n; i++ ) //
for ( j = 0; j < n; j++ )
{
float s = 0;
for ( int k = 0; k < n; k++ )
s += a[i][k] * b[k][j];
r[i][j] = s;
}
cout << "" << endl; // -
for ( i = 0; i < n; i++ )
{
for ( j = 0; j < n; j++ )
cout <<setw(6) << r[i][j];
cout << endl;
}
//
for ( i = 0; i < n; i++ ) delete[] a[i]; delete[]a;
for ( i = 0; i < n; i++ ) delete[] b[i]; delete[]b;
for ( i = 0; i < n; i++ ) delete[] r[i]; delete[]r;
}
.
, - .

, -. - , 0, '\0'.
- .
:

40

char str[10] = "Vasia";


10 , 5 ,
- -. ,
( ):
char str[] = "Vasia"; // 6

char *str = "Vasia"


, .

.
<cstring>.
- - <cstdio>.
, ,
, , ..,
.
( ):
#include <cstdio> // -
#include <cstring> //
#include <clocale> //
using namespace std;
int main()
{
setlocale( LC_ALL, "rus" );
char s[5],
passw[] = "kuku"; // passw -
bool ok = false;
for ( int i = 0; ! ok && i < 3; ++i )
{
printf( "\n (4 ):\n" );
gets( s );
// c
if ( strcmp( s, passw ) == 0 )
ok = true;
//
}
if ( ok )
printf( "\n \n" );
else
printf( "\n \n" );
}

,
. ,
, 4 , .
.
src dest:

41

#include <iostream>
using namespace std;
int main()
{ char src[10];
cin >> src;
//
char *dest = new char [10],
*d = dest, *s = src;
while ( *d++ = *s++ );
//
cout << dest;
}

(enum)

, , .
. :
enum [ _ ] { _ };
,
. ,
.
.
,
1 , :
enum Err { ERR_READ, ERR_WRITE, ERR_CONVERT};
Err error;
...
switch (error)
{
case ERR_READ:
/* */ break;
case ERR_WRITE: /* */ break;
case ERR_CONVERT:
/* */ break;}
ERR_READ, ERR_WRITE, ERR_CONVERT 0, 1 2
.

(struct)
, ,
. ++
, ,
:
struct [ _ ] {_1 _1;_2 _2;*_n _n;}
[ _ ];
,
, .
, ,
. :
struct
{
char fio[30];

42

int date, code;


float salary;
}stuff[100], *ps;

/* */

, ,
, :
struct Worker
{ // Worker
char fio[30];
int date, code;
float salary;
};
//
Worker stuff[100], *ps;
/* Worker */
/* Worker */

:
Struct
{
char fio[30];
int date, code;
float salary;
}worker = {"", 31, 215, 3400.55};
,
.
.
.
. ()
-> , :
Worker worker, stuff[100], *ps;
...
worker.fio = "";
stuff[8].code = 215;
ps->salary = 0.12;


- .
, , "/".
( ):
struct Options {
bool centerX:1;
bool centerY:1;
unsigned int shadow:2;
unsigned int palette:4;};
. .

43

(union)
(union) ,
. , ,
struct union.
.
,
.
,
:
struct Options {
bool centerX:1;
bool centerY:1;
unsigned int shadow:2;
unsigned int palette:4;
};
union {
unsigned char ch;
Options bit;
}option={0xC4};
cout << option.bit.palette;
option.ch &= 0xF0; //
:

;
;

, ,
;

(typedef)
,
typedef:
typedef _ [ ];
. :
typedef unsigned int UINT;
typedef char Msg[100];
typedef struct{
char fio[30];
int date, code;
float salary;} Worker;
,
:

44

UINT i, j;
Msg str[10];
Worker stuff[100];

// unsigned int
// 10 100
// 100

4. : : PDA
. . .
. , . .


.
- , (
).
. , ,
- .
.
,
,
. , ,
, .
,
.
++ , main (
).
.
. , ,
.
,
.
(, , ) ,
.
, , ,
:
[ ] ([ _ ])[throw ( )]{ }
.


, extern static:
o extern - ( );
o

static - , .

45

( ).
, void.

,
. .
, , (
).

,
.
.
inline,

. inline .
, .
inline
.
.
,
.
,
, .
void, , ,
.
, :
#include <iostream>
using namespace std;
int sum(int a, int b); //
int main(){
int a = 2, b = 3, c, d;
c = sum(a, b);
//
cin >> d;
cout << sum(c, d); //
}
int sum(int a, int b) //
{return (a+b);}
, :
#include <iostream>
using namespace std;
struct Worker
{
char fio[30];
int date, code;
float salary;
};
void print_worker(Worker);
int main()
{

//

46

Worker stuff[100];
...
/* stuff */
for (int i = 0; i<100; i++)
print_worker(stuff[i]); /*
*/
}
void print_worker(Worker w) //
{cout << w.fio << ' '
<< w.date << ' '
<< w.code << ' '
<< w.salary;}
, , , .
. , ,
.
,
.
,
static:
#include <iostream>
using namespace std;
void f(int a)
{
cout << "n m\n";
while (a--)
{
static int n = 0;
int m = 0;
cout << n++ << ' '
<< m++ << '\n';
}
}
int main(){ f(3);}
n
, . :
nm
00
10
20
.
,
.


,
,
. , ,
.
, ,
.

47



return [ ];
return. void,
. , return,
.
:
int f1(){ return 1; } //
void f2(){ return 1; } //, f2
double f3{ return 1; }//, 1 double

.
:
int* f()
{
int a = 5;
return &a;
}

// !



. ,
. ++
: .
.
:

. ,
.
, ,
:
=
; ,
( ) ,
. , ,
, .
,
, .

48

-, .

:
&
- : ,
. -
.
L-.
, -,
. - -.
, ,
, .
,
, . ,
:
void swap( int &a, int &b ) //
{
int t = a; a = b; b = t; }
// ...
int x = 5, y = 6;
swap( x, y );
//
swap x y.
.
- ,
.
.
:
#include <iostream>
using namespace std;
void f(int i, int* j, int& k);
int main()
{
int i = 1, j = 2, k = 3;
cout <<"i j k\n";
cout << i <<' '
<< j <<' '<< k
<<'\n';
f(i, &j, k);
cout << i <<' '
<< j <<' '<< k;
}
void f(int i, int* j, int& k)
{ i++; (*j)++; k++; }

49

:
ijk
123
134
(i) .
. (j) ,
,
.
(k) .
, const:
int f(const char*);
char* t(char* a, const int* b);

const ,
. . , const&
.
, , (, ,
, , , ), ,
.


.
, :
#include <iostream>
using namespace std;
int sum(const int* mas, const int n);
int const n = 10;
void main()
{
int marks[n] = {3, 4, 5, 4, 4};
cout << " : "
<< sum(marks, n);
}
int sum(const int* mas, const int n)
/*: int sum(int mas[], int n) */
/*int sum(int mas[n], int n) (n ) */
{ int s = 0;
for (int i = 0 ; i<n; i++) s += mas[i];
return s;}
,
, .
,
.
. b ,
a :

50

#include <cstdio> using namespace std;


int sum(const int *a, const int nstr, const int nstb);
void main()
{
int b[2][2] = {{2, 2}, {4, 3}};
/* - */
printf("b %d\n", sum(&b[0][0], 2, 2));
int i, j, nstr, nstb, *a;
printf(" : \n");
scanf("%d%d", &nstr, &nstb);
a = (int *)malloc( nstr*nstb*sizeof(int) );
for (i = 0; i<nstr; i++)
for (j = 0; j<nstb; j++)
scanf("%d", &a[i*nstb+j]);
printf("a %d\n", sum(a, nstr, nstb));
}
int sum(const int *a, const int nstr, const int nstb)
{int i, j, s = 0;
for (i = 0; i<nstr; i++)
for (j = 0; j<nstb; j++)s += a[i*nstb + j];
return s;
}
,
:
#include <iostream>
using namespace std;
int sum(const int **a, const int nstr, const int nstb);
void main()
{
int nstr, nstb;
cin >> nstr >> nstb;
int **a;
a = new int* [nstr];
for (int i = 0; i<nstr; i++)
a[i] = new int [nstb];
/* * a */
cout << sum(a, nstr, nstb);
}
int sum(const int **a, const int nstr, const int nstb)
{
int i, j, s = 0;
for (i = 0; i<nstr; i++)
for (j = 0; j<nstb; j++)s += a[i][j];
return s;
}
:
, .
.


.

:
void f(int a ){ /* * */ } //

51

void (*pf)(int);
//
...
pf = &f; /* */
/* ( pf = f;) */
pf(10);
/* f pf */
/*( (*pf)(10) ) */
,
(typedef).
( , , ):
/* PF int */
typedef void (*Pf)(int);
/* */
PF menu[]={&new, &open, &save}
menu[1](10);
// open
,
:
void fun(PF pf)
{* pf(10); *}

/* fun */
/* PF */
// ,

, ,
.


,
.
. ,
:
int f(int a, int b = 0);
void f1(int, int = 100, char* = 0);
void err(int errValue = errno); //
f(100); f(a, 1);
// f1
f1(a);
f1(a, 10);
f1(a, 10, "Vasia");
f1(a,,"Vasia")
// !

errno -
// f


++ ,
, , , .
, #include
,
.

. :
/, , , ,
..

52

/
/ ++ , ,
++.
.
/
<stdio.h> <cstdio>. /
. , ,
, .

. /
. :
FILE* fopen(const char* filename, const char* mode);

FILE, ,
NULL . - -,
- :
"r" - ;
"w" - ( , );
"a" - ;
"r+" - ( );
"w+" - ( , );
"a" - .
t ( ) b ( ),
.
, " "
" " (0x13 0x10)
( ).
.
:
FILE *f = fopen("d:\\cpp\\data.txt", "rb+");
f .
/ .
, .

. , ,
.

53

,
: stdin, stdout,
stderr, stdaux
stdprn.
/
/ :
, .
.
/ ,
.
( )
/.
/ .

fread fwrite.
- getc, fgetc, stdin - getchar.

- putc, fputc, stdout - putchar.

- fgets, stdin - gets.

- fputs, stdout - puts.

- fscanf, stdin - scanf,


- sscanf.

- fprintf, stdout - printf,


- sprintf.


,
fclose:
int fclose(FILE*);
.
, .


s, mon
. .
#include <cstdio>
#include <cstdlib>
#include <cconio>
#include <iostream>
using namespace std;
int main()

54

{
FILE *fi, *fo;
if((fi = fopen("d:\\c\\binfile.txt", "r"))==0)
{
cout << " ";
return 1;
};
if((fo = fopen("d:\\c\\binfile.out", "w+b"))==0)
{
cout << " ";
return 1;
};
const int dl = 80;
char
s[dl];
struct
{
char type[20];
int
opt, rozn;
char
comm[40];
}mon;
while (fgets(s, dl, fi))
{
// :
strncpy(mon.type, s, 19);
// strncpy . .
mon.opt = atoi(&s[20]);
// atoi . .
mon.rozn = atoi(&s[25]);
strncpy(mon.comm, &s[30], 40);
fwrite(&mon, sizeof mon, 1, fo);
}
fclose(fi);
int i; cin >> i;
//

// i
fseek(fo, (sizeof mon)*i, SEEK_SET);
fread(&mon, sizeof mon, 1, fo);
cout << "mon.type "
<< mon.type << " opt "
<< mon.opt << " rozn "
<< mon.rozn << endl;
fclose(fo);
}
, . ,
. :
20 , 5 ,
40 .


, -. ++
: ,
( <string.h> <cstring>), ++ string,
, .
(strcpy, strncpy), (strcmp,
strncmp), (strcat, strncat), (strstr),
(strchr, strrchr, strpbrk), (strlen) .
<stdlib.h> <cstdlib>
:

55

double atof(const char* p);


int atoi(const char* p);
long atol(const char* p);

// double
// int
// long

.
. ,
0.
( <ctype.h>
<cctype>) :
4.1.

isaln (A-Z, a-z, 0-9)


um
isalfa

(A-Z, a-z)

iscnt ( 0..31 127)


rl
isdigit

(0-9)

isgra , (isalfa | isdigit |


ph
ispunct)
islow (a-z)
er
isprint

ispunct
isspace -
isupp (A-Z)
er
isxdi (A-F, a-f, 0-9)
git


++ ,
<math.h> <cmath>.
(abs, fabs), (ceil, floor), (sqrt), (pow),
(sin, cos, tan, sinh, . .), (exp),
(log, log10), (modf), (fmod)
.

56


.
,
- (),
.

.
. ,
.
. ,
.
, .
.
++ ,
, , .


.
. #,
.
#include
#include <_>
, .
#include.
, ,
. (" ") -
, , .
#include
,
. .h :

, , , , ;
, , , ;

.
#define
#define .

57

. :
/* */
#define _
, ,
. :

#define ( ) _
, .
#ifdef #ifndef. :

#define

:
#define M 1000
#define Vasia " "
#define MAX(a,b) ((x)>(y)?(x):(y))
#define __cplusplus
, ,
y = MAX(sum1, sum2);,
y=((sum1)>(sum2)?(sum1):(sum2));
.


,
. : , , ,
, .
. , , .
, -
, - .
.
. , , ,
,
.
. , ,
. ,
.
. , ()
, .
. , ( )
.
.
. ++

58

namespace.
,
.
, .
, , ,
::. .
.
- , .
,
, :
struct Node
{
int Node;
int i;
}Node;
, ,
. ++
.

, , ,
(typedef)
. , ,
.
, ,
.
.

, .
,
.


.
, , static.
,
:

;
extern.

-
1).
.
one.cpp two.cpp
my_header.h:

59

// my_header.h -
extern int a;
extern double b;
...
// one.cpp
#include "my_header.h"
int a;
...
// two.cpp
#include "my_header.h"
double b;
...
static, ,
.

.
, .
, , ,
.
( ) :
namespace [ _ ]{ /* */}
,
.
,
, .
static.
.
.
namespace demo{
int i = 1;
int k = 0;
void func1(int);
void func2(int) { /* ... */}
}
namespace demo{
//
// int i = 2;
-
void func1(double); //
void func2(int);
// ( )
}
,
. ,
::, :
void demo::func1(int) { /* ... */}
.

60

.
, , .

::, :
demo::i = 100; demo::func2(10);
,
using:
using demo::i;
.
- ,
using namespace:
using namespace demo;
using using namespace
, .


std. ,
/ <stdio.h>
:
// stdio.h
namespace std
{
int printf(const char**);
...
}
using namespace std;
. ,
, <cstdio>:
// cstdio
namespace std{
int printf(const char**);
...
}
<cstdio>,
:
std::printf("*");
#include

.

61

, , , .
extern : ,
extern
1)

5. : : PDA
- . .
: , , this, , , .
.


.
.
, - ,
, ,
. ,
.
,
,
. .

,
, .
,
.
- ,
, . ,
, ,
.
.
,
.
, -
. ,
,
.

,
.
, , .
.
.
- .
-

62

, ,
. ,
,
.
, .
- ( )
.
, (,
, ,
, , ).
,
.
. , ,
.
- ().
Simula-67 Smalltalk,
-
. ++ ,

.
(
, ),
.
"" ,
. , .
- ,
. .
, "-
",
1).

- ,
.
.
(), :
(
, ) .


, .
,
.

(encapsulation).
:
,
. ,

63

, (,
).
,
,
.
- ,
, .
, .
- -
,
.
,
, - .
++ .
, - .
, , -

.
++ .
,
.
- ,
, .
.
, ,

.
, -
.
.
,
- . , ,
, .
,
- - ,
,
" , , "
(. ).


- .
, . ,
- :
, , . . ++
,
++.

64

( ), . .
:
class <>{
[ private: ]
< >
public:
< >
//

};

private public . ,
private, .
. public.

. private public,
.
:
1. , ,
(.. , ,
);
2. ( const),
( ) ;
3. static, auto, extern register.
.
( )
( , , ).
.
:

;
,
(static) (extern) ,
;

(inline);

, -
.

, .
(, , )
. , ,
.
class monster
{
int health, ammo;

65

public:
monster(int he = 100, int am = 10)
{ health = he; ammo = am;}
void draw(int x, int y, int scale, int position);
int get_health(){return health;}
int get_ammo(){return ammo;}};
- health ammo,
get_health() get_ammo().
, ,
,
. ,
, .
.
, , .
( draw).
, (inline). ,
.
() ,
:
void monster::draw(int x, int y, int scale, int position)
{ /* */}
inline (
, ):
inline int monster::get_ammo()
{return ammo;}
( ),
, ( ).
, .
.
.
, .
"".
struct union .


"" ,
.
++:
monster Vasia;
// monster
monster Super(200, 300);//
monster stado[100];
//
/* ( ) */
monster *beavis = new monster (10);
monster &butthead = Vasia; //
, ,

66

, .
. ,
( ).
(public) .
. ()
-> :
.
->
(*).
.( )
-> ( )
(*).( )
:
_[ ].
_[ ].( )
:
int n = Vasia.get_ammo();
stado[5].draw;
cout << beavis->get_health();
private
.
, .
:
class monster{
...
int get_health() const {return health;}
};
const monster Dead (0,0);
//
cout << Dead.get_health();
:

const ;
;

( ) .

,
.

this
.
.

67

, "",
.
, ,
: , . ++
this.
, (,
this -> num).
*this
. ,
(return *this;).
this monster
, ( health) ,
, (
public ):
monster & the_best(monster &M)
{
if( health > M.get_health())
return *this;
return M;
}
...
monster Vasia(50), Super(200);
// Best Super
monster Best = Vasia.the_best(Super);


. .

, void.
.

( ).

, ,
.

, .
.
.

,
( , ,
).
.

, (
const, virtual static).

main.

68

, .
(,
).
.
,
- (. ),
- (..
, ).
.
:

;
;

:
monster Super(200, 300), Vasia(50);
monster X = monster(1000);
,
health = 1000 (
).
.

monster, , (skin) (name):
enum color {red, green, blue}; //

class monster
{
int health, ammo;
color skin;
char *name;
public:
monster(int he = 100, int am = 10);
monster(color sk);
monster(char * nam);
...
};
//-------------------------------monster::monster(int he, int am)
{ health = he; ammo = am; skin = red; name = 0;}
//-------------------------------monster::monster(color sk)
{
switch (sk)
{

69

case red: health = 100; ammo = 10; skin = red; name = 0; break;
case green: health = 100;ammo = 20;skin = green;name = 0;break;
case blue: health = 100; ammo = 40; skin = blue;name = 0;break;
}
}
//-------------------------------monster::monster(char * nam)
{
/* 1 - */
name = new char [strlen(nam) + 1];
strcpy(name, nam);
health = 100; ammo = 10; skin = red;
}
//-------------------------------monster * m = new monster ("Ork");
monster Green (green);
,
. monster
,
. ,
-
.
(
) - ,
:
monster::monster(int he, int am):
health (he), ammo (am), skin (red), name (0){}
.
, .
-, - -.
, .


- ,
:
T::T(const T&) { ... /* */ }
T - . ,
:

;
;

,
. .
, , , , ,
.
monster. name,

70

,
:
monster::monster(const monster &M)
{
if (M.name)
{
name = new char [strlen(M.name) + 1];
strcpy(name, M.name);
}
else name = 0;
health = M.health; ammo = M.ammo; skin = M.skin;
}
...
monster Vasia (blue);
monster Super = Vasia;
//
monster *m = new monster ("Ork");
monster Green = *m;
//


static .


, ,
, .
,
.

( )
, :
class A
{
public:
static int count;
}
...
A::count = 0;
, :
/* */
A *a, b; * cout << A::count << a->count << b.count;
,
, private,
, .
, .
, ,
sizeof. ,
.

71

- .
,
.



,
this. ,
- , , ,
.
(const) (virtual).


,
.
.


, ,
friend.
, this . ""
.

. ,
.
, monster.
kill hero, steal_ammo
. monster.
class monster;
//
class hero
{
...
void kill(monster &);
};
class monster
{
...
friend int steal_ammo(monster &);
/* hero */
friend void hero::kill(monster &);
};
int steal_ammo(monster &M){return --M.ammo;}
void hero::kill(monster &M){M.health = 0; M.ammo = 0;}


- ,
friend.

72

mistress hero:
class hero{
... friend class mistress;}
class mistress{
... void f1();
void f1();}
f1 f2 hero (
friend) .
friend .
, , ,
.

- , ,
. , :

- , ;
- main;

, ,
delete (
).

.

delete. delete[]
.
(~), .
:

;
const static;

;
, new .

, .
,
, - ,
-, .
.
:
monster::~monster() {delete [] name;}

73


++ ,
.
, .
. ,
++, :
5.1.
. .* ?: :: # ## sizeof
() :

,
( )
;
;

- ;

- ( =);

- :
, , .
, ,
( : -, , ).
- operator,
:
operator ( ) { }


-, ,
,
, :
class monster
{...
monster & operator ++() {++health; return *this;}}
monster Vasia;
cout << (++Vasia).get_health();
, :
class monster
{... friend monster & operator ++( monster &M);};
monster& operator ++(monster &M) {++M.health; return M;}
,
( health ,
private,

74

, ).
int.
, :
class monster
{...
monster operator ++(int){monster M(*this); health++; return M;}};
monster Vasia;
cout << (Vasia++).get_health();


-, ,
,
:
class monster
{
...
bool operator >( const monster &M)
{
if( health > M.get_health()) return true;
return false;
}
};
, :
bool operator >(const monster &M1, const monster &M2)
{
if( M1.get_health() > M2.get_health()) return true;
return false;
}



. ,
.
, .
, - ,
, -
:
monster& operator = (const monster &M)
{
if (&M == this) return *this;
//
if (name) delete [] name;
if (M.name)
{
name = new char [strlen(M.name) + 1];
strcpy(name, M.name);
}
else name = 0;
health = M.health; ammo = M.ammo; skin = M.skin;
return *this;
}

75

. .


-,
. :
operator __ ();
.
.
:
monster::operator int(){return health;}
...
monster Vasia; cout << int(Vasia);


.
.* ->*. -.
:
_ (_::*_)();
, monster
int get_health() const {return health;}
:
int (monster:: *pget)();
.
:
void fun(int (monster:: *pget)())
{
(*this.*pget)();
// .*
(this->*pget)();
// ->*
}
:
pget = & monster::get_health;//
monster Vasia, *p;
p = new monster;
int Vasin_health = (Vasia.*pget)();// .*
int p_health = (p->*pget)();
// ->*
,
.
.

76

,
.

, ,
, .
,
. ,
.
.

, , . ,
, .
:
_(_::*_);
:
&_::*_;

// public

health public, :
int (monster::*phealth) = &monster::health;
cout << Vasia.*phealth;
// .*
cout << p->*phealth; // ->*
, ,
, .


, , ,
:

, , ;
(
const, , );

, ;

, ,
, ;

,
.

,
( ), ,
,
, :
namespace Staff{

77

class monster { /* * */};


class hero { /* * */};
void interact(hero, monster); ... }
-
( - Windows
API).
1)

6. : . : PDA
. .
. .

.
.

,
.
,
, ,
.
, ,
.
.
.


,
.
private, protected public:
class : [private | protected | public] _{ };
, .
. private, public.
public, .
protected , private . :
-. , .
private public,
.
protected, , , private.
.
:

6.1.

78

private

protected

public

private
protected
public
private
protected
public
private
protected
public

private
private

protected
protected

protected
public

, private
.
.
protected private
private, .
public .
private,
, public
:
class Base{...
public: void f();};
class Derived : private Base{...
public: Base::void f();};


, .
.
.
monster daemon,
:
enum color {red, green, blue};
// ------------- monster ------------class monster
{
// ------------- :
int health, ammo;
color skin;
char *name;
public:
// ------------- :
monster(int he = 100, int am = 10);
monster(color sk);
monster(char * nam);
monster(monster &M);
// ------------- :
~monster() {delete [] name;}
// ------------- :
monster& operator ++(){++health; return *this;}
monster operator ++(int)

79

{monster M(*this); health++; return M;}


operator int(){return health;}
bool operator >(monster &M)
{
if( health > M.get_health()) return true;
return false;
}
monster& operator = (monster &M)
{
if (&M == this) return *this;
if (name) delete [] name;
if (M.name)
{
name = new char [strlen(M.name) + 1];
strcpy(name, M.name);
}
else name = 0;
health = M.health; ammo = M.ammo; skin = M.skin;
return *this;
}
// ------------- :
int get_health()
const {return health;}
int get_ammo()
const {return ammo;}
// ------------- , :
void set_health(int he){ health = he;}
void draw(int x, int y, int scale, int position);
};
// ------------- monster ------------monster::monster(int he, int am):
health (he), ammo (am), skin (red), name (0){}
monster::monster(monster &M)
{
if (M.name)
{
name = new char [strlen(M.name) + 1];
strcpy(name, M.name);
}
else name = 0;
health = M.health; ammo = M.ammo; skin = M.skin;
}
monster::monster(color sk)
{
switch (sk)
{
case red:health = 100; ammo = 10; skin = red; name = 0; break;
case green:health = 100;ammo = 20;skin = green; name = 0; break;
case blue: health = 100; ammo = 40; skin = blue; name = 0;break;
}
}
monster::monster(char * nam)
{
name = new char [strlen(nam)+1];
strcpy(name, nam);
health = 100; ammo = 10; skin = red;
}
void monster::draw(int x, int y, int scale, int position)
{ /* ... monster */ }
// ------------- daemon ------------class daemon : public monster
{

80

int brain;
public:
// ------------- :
daemon(int br = 10){brain = br;};
daemon(color sk) : monster (sk) {brain = 10;}
daemon(char * nam) : monster (nam) {brain = 10;}
daemon(daemon &M) : monster (M) {brain = M.brain;}
// ------------- :
daemon& operator = (daemon &M)
{
if (&M == this) return *this;
brain = M.brain;
monster::operator = (M);
return *this;
}
// ------------- , :
void draw(int x, int y, int scale, int position);
void think();
};
// ------------- daemon ------------void daemon::draw(int x, int y, int scale, int position)
{ /* ... daemon */ }
void daemon:: think(){ /* ... */ }
daemon brain think,
, draw.
monster, ( ) get_health, get_ammo set_health
daemon, .
.
,
.


, (
, ).
daemon.
, ,
.
, , ,
.

.

,
(
).
,
daemon. -:
- .
, operator
-.

81


. ,
:
, .
:

,
,
.
,
,
.
, ,
, :
, - , .

, monster, ,
private. ,
daemon, ,
protected, monster,
daemon , .
,
. .
, , .

.
, , daemon
draw, monster (
, , -). ,
, .

.
- , ,
. ,
.
- .
.
,
. . -
,
. .


.
, :
monster *p;
p = new daemon;

//
/* */

82

,
, , , ,
p -> draw(1, 1, 1, 1);
monster, daemon,
. .
daemon,
:
((daemon * p)) -> draw(1, 1, 1, 1);
,
,
.
,
.
. -
, .
++ ,
,
. .
virtual:
virtual void draw(int x, int y, int scale, int position);
.

, ,
,
, - .
,
.
.

,

.

static,
.

,
.

= 0 , :
virtual void f(int) = 0;
(,
).
draw monster , ,

83

, ,
:
monster *r, *p;
r = new monster;
// monster
p = new daemon;
// daemon
r -> draw(1,1,1,1);
// monster::draw
p -> draw(1,1,1,1);
// daemon::draw
p -> monster::draw(1,1,1,1); //

daemon draw , (
, monster),
draw daemon.
, ,
( virtual - -
"", ).
.
( !), ,
(vtbl),
.
. vtbl
.
vtbl, vptr.
(
).
vtbl
vptr ,
. , ,
, .
,
.
,
- ,
, .
, , .
, ,
. ,
, ,
. ,
, ,
,
.
, draw
.
( ,
),
, . draw ,

84

- , .

. ,
, . ,

, .


, , .
,
.
-
,
.
:

,
;
,
.
, ,
, .

, ,
.
.
, .


, . ,
,
, :
class monster
{
public: int get_health();
...
};
class hero
{
public: int get_health();
...
};
class being: public monster, public hero{ ... };
int main(){
being A;
cout << A.monster::get_health();
cout << A.hero::get_health();}
A.get_health() ,

85

, .
, ,
,
. ,
:
class monster{ ... };
class daemon: virtual public monster{ ... };
class lady: virtual public monster{ ... };
class god: public daemon, public lady{ ... };
god monster.


- , ++ .
, , .
,
, . ,
"", "",
, .
, , .
, .
( , ) .
, ,
-,
, ,
. - ,
, .


(struct) (union) .
, ,
public.
, .
public, ,
.
. ,
. , .
.


, .
.
.
, -
.
, ,

86


.
, ,
,
.
. ,
. ,
. :
class Node
{
public:
int d;
//
Node *next, *prev; //
Node(int dat = 0)
{ d = dat; next = 0; prev = 0; } //
};
, ,
(public).
. List:
class List
{
class Node{ ... };
Node *pbeg, *pend; //
public:
List() { pbeg = 0; pend = 0; } //
~List();
//
void add(int d);
//
Node * find(int i);
//

Node * insert(int key, int d); /* d key */
bool remove(int key);
//
void print();
//
void print_back();
//
};
. add
Node , :
void List::add(int d)
{
Node *pv = new Node(d);
//
if (pbeg == 0)pbeg = pend = pv;
//
else
{
pv->prev = pend;
//
pend->next = pv;
pend = pv;
}
//
}
find

87

0 :
Node * List::find( int d )
{
Node *pv = pbeg;
while (pv)
{
if(pv->d == d)break;
pv=pv->next;
}
return pv;
}
insert key
. ,
0:
Node * List::insert(int key, int d)
{
if(Node *pkey = find(key))
{
// key
/* */
Node *pv = new Node(d);
/* */
pv->next = pkey->next;
//
pv->prev = pkey;
//
pkey->next = pv;
if( pkey != pend)
(pv->next)->prev = pv;
/* */
/* , */
else pend = pv;
return pv;
}
return 0;
}
remove true
false, :
bool List::remove(int key)
{
if(Node *pkey = find(key))
{
if (pkey == pbeg)
{
//
pbeg = pbeg->next; pbeg->prev = 0;
}
else if (pkey == pend)
{
//
pend = pend->prev; pend->next = 0;
}
else
{
//
(pkey->prev)->next = pkey->next;
(pkey->next)->prev = pkey->prev;

88

}
delete pkey; return true;}
return false;}
,
:
void List::print(){
Node *pv = pbeg;
cout << endl << "list: ";
while (pv){
cout << pv->d << ' ';
pv=pv->next;}
cout << endl;
}
void List::print_back(){
Node *pv = pend;
cout << endl << " list back: ";
while (pv){
cout << pv->d << ' ';
pv=pv->prev;}
cout << endl;}
- :
List::~List(){
if (pbeg != 0){
Node *pv = pbeg;
while (pv)
{pv = pv->next; delete pbeg; pbeg = pv;}
}}
, List.
5 , , ,
:
int main()
{
List L;
for (int i = 2; i<6; i++) L.add(i);
L.print(); L.print_back();
L.insert(2,200);
if (!L.remove(5))cout << "not found";
L.print(); L.print_back();}
List . ,
.
:
template <__> class { /* */ };
template.
.
: , .
, .
class.
<class T>. T -.

89

, T. -
, , :
template <class TData> class List
{
class Node{
public:
TData d;
Node *next;
Node *prev;
Node(TData dat = 0){d = dat; next = 0; prev = 0;}
}; ... }
TData ,
. List<TData>.
.
, :
template <__>
_ _ <_ >:: _ (_
)
:
template <class Data> void List <Data>::print()
{ /* */ }

.
.

,
.

, ,
, .

friend-.

, .
class , :
template <class T1, class T2>
struct Pair { T1 first; T2 second; };
,
"=". ,
, .
List.
template <class TData> class List
{

90

class Node
{
public:
TData d;
Node *next, *prev;
Node(TData dat = 0){d = dat; next = 0; prev = 0;}
};
Node *pbeg, *pend;
public:
List(){pbeg = 0; pend = 0;}
~List();
void add(TData d);
Node * find(TData i);
Node * insert(TData key, TData d);
bool remove(TData key);
void print();
void print_back();};
//------------------------template <class TData> List <TData>::~List()
{
if (pbeg !=0)
{
Node *pv = pbeg;
while (pv)
{pv = pv->next; delete pbeg; pbeg = pv;}
}
}
//------------------------template <class TData> void List <TData>::print()
{
Node *pv = pbeg;
cout << endl << "list: ";
while (pv)
{
cout << pv->d << ' ';
pv = pv->next;
}
cout << endl;
}
//------------------------template <class TData> void List <TData>::print_back()
{
Node *pv = pend;
cout << endl << " list back: ";
while (pv)
{
cout << pv->d << ' ';
pv = pv->prev;
}
cout << endl;
}
//------------------------template <class TData> void List <TData>::add(TData d)
{
Node *pv = new Node(d);
if (pbeg == 0)pbeg = pend = pv;
else
{
pv->prev = pend;
pend->next = pv;

91

pend = pv;
}
}
//------------------------template <class TData> Node * List <TData>::find( TData d)
{
Node *pv = pbeg;
while (pv)
{
if(pv->d == d)break;
pv = pv->next;
}
return pv;
}
//------------------------template <class TData> Node * List <TData>::insert(TData key, TData d)
{
if(Node *pkey = find(key))
{
Node *pv = new Node(d);
pv->next = pkey->next;
pv->prev = pkey;
pkey->next = pv;
if( pkey != pend)(pv->next)->prev = pv;
else pend = pv;
return pv;
}
return 0;
}
//------------------------template <class TData> bool List <TData>::remove(TData key)
{
if(Node *pkey = find(key))
{
if (pkey == pbeg)
{
pbeg = pbeg->next; pbeg->prev = 0;
}
else if (pkey == pend)
{
pend = pend->prev; pend->next = 0;
}
else
{
(pkey->prev)->next = pkey->next;
(pkey->next)->prev = pkey->prev;
}
delete pkey; return true;
}
return false;
}
List ,
,
,
, .
, , ,
. :

92

, , ;
;

,
.
,
:
template <class Type, int kol> class Block
{
public:
Block(){p = new Type [kol];}
~Block(){delete [] p;}
operator Type *();
protected:
Type * p;
};
template <class Type, int kol>
Block <Type, kol>:: operator Type *()
{
return p;
}
- , .
, - , .
, .
, , , -.
.


,
:
_ < > _ (_);

.
. :
List <int> List_int; //
List <double> List_double; //
List <monster> List_monster; // monster
Block <char, 128> buf;
//
Block <monstr, 100> stado; // monster
Pair<int, int> a;
//
Pair<int, double> b;
// ", "
Pair<int, double> b = { 1, 2.1 }; //
Pair<string, Date> d;
// -

93

, :
template<class T = char> class String;
String<>* p;
,
, .
. -
, ,
.
, ,
:
for (int i = 1; i<10; i++)List_double.add(i*0.08);
List_double.print();
//---------------------------------for (int i = 1; i<10; i++)List_monster.add(i);
List_monster.print();
//---------------------------------strcpy(buf, " ");
cout << buf << endl;

typedef:
typedef List <double> Ldbl;
Ldbl List_double;



, ,
, ,

. ,
. ,
, ,
. ,
, .
( [18]).
++
.
. ,
[18] [19]: , ,
, , ,
, ( , , ,
, , ).
,
.

94