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

..

2014

004.657(075.8)
??
: . . , .
??

- :
/ .
. . ; . -. , 2014. 40.

.
. ,
,
.
II
230100
(
).

.

. . . , . .

004.657(075.8)


______________
__.__.2014. 6084/16. .
. Times New Roman. . . . _,__.
.-. . _,_. 100 . ____.

428015 ,

1.
++.
++ .
,
.
-. :
- ;
- ,
;
- (),
.
,
.
++ .
++
, , .
.
, , ,
, .
.
()
,
.
: int (), char
(), wchar_t ( ), bool
(), float (), double (
).
, ..
.
. ,
,
.
,

: short (), long (), signed (),
unsigned ().

(int). int
,
. ,
. 16-
2 (16
), 32- 4 .
short
, 2 ,
. long
4 . , 32-
int 4 , short int 2 , long int 4 .
signed ( )
,
(0- , 1- ).
unsigned
.
int .
31


. 1. int, signed int long
32-

31
0

. 2. unsigned int 32-

16

0

. 3. sort int

16
0

. 4. unsigned short int


, , unsigned long long int.
(char).
char 1 . 256
ASCII. char,
.
-128 127.
unsigned,
0 255.
char
.
(wchar_t). wchar_t
, 1
, , Unicode.
short ( 2 ).
(bool).
true (1) false (0).
true.
true 1, false 0.
1 .
(float, double, long double).

, . float 4
, , 8
23 .
, 1 2.
double 8 .
long double 10 .

double.


1.
1.



,
char ()

128 127

128 127

0 255

16

32768 32767

16

0 65535 (16-
)
0 4294967295 (32 )

signed char (
)
unsigned char
( )
short int (
)
unsigned int
( )

16
32
int ()

32768 32767 (16 )


2147483648
2147483647 (32-
)

long ( )

32

2147483648
2147483647

unsigned long
( )

32

0 4294967295

long long int (C99)

64

(2631) 2631

unsigned long long


int (C99)

64

0 2641

float ()

32

3.4E38 3.4E38

double (
)

64

1.7E308 1.7E308

long double (

80

3.4E4932

)
bool (C++)

3.4E4932
true(1), false(0)

<limits.h>
(<climits>),

<floath> (cfloat>),
numeric_limits.
void. ,
void,
. ,
,
.
.
.
,
, , , ,
.
,
.
.
(typedef).
.
.
:
typedef _ [ ];


.

,
:
typedef unsigned int UINT;

UINT unsigned int.
,
:

typedef struct
{
char *Name;
int Age;
float Ball;
} Students;

, :
UINT _x, _y;
// unsigned
int
Students group[20];
// 20
(enum).
,
.
.
:
enum [_] {_};
,
.
,
.

.
,
1, .
:
enum error{ERR_READ, ERR_WRITE, ERR_CONVERT};
// error
error err; // err
ERR_READ, ERR_WRITE, ERR_CONVERT
0, 1, 2. err
, . . 0, 1, 2.

:
enum error{ERR_READ = 3, ERR_WRITE = 38,
ERR_CONVERT = 99};


,
.
(struct). ,
, ,
.
++
.
:
struct [ _ ]
{ _1 _1;
_2 _2;

_n _n;
} [ _ ];
(,
) ,
.
.
.
,
, .
, ,
, ,
.
,
( ,
) .
.
struct Students
{
char *Name;
int Age;
bool Sex;
float Ball;
};
Students
. (

) char
*, int, bool float.

, ,

.
.
,
.
.

(. - )

- >,
. ,
Student st, *ptr_st = &st;
st.Name = Ivan;
st.Age = 20;
ptr_st->Sex = 1;
ptr_st->Ball = 4.75;

, . .
.

.
.
,

, . ++, , ,

() - .
, ++
:

10

- sizeof,
,
;
- ()
typedef;
- ;
- (RTTI);
sizeof.
sizeof- ,
, .
:
float var_float = 3.14;
//
cout << =
<< sizeof(var_float) << endl;
// ..
cout << =
<< sizeof(int);

, , ,
.

,
, , 16 32.
- .
sizeof
, Students:
cout << = << sizeof(st.Age)
<< endl;

11

. ++

, .
,
, : ()
(). ,
(float)var_int;
var_int
.
, ..
.
, , (int)var_float
.

, ,
var_int+var_float, var_int
, ..,

(float)var_int+var_float.

.
.

++
const_cast, static_cast, dynamic_cast, reinterpret_cast,
.

(RTTI). ,
.
RTTI typeid
type_info. <typeinfo>.
typeid,
. name
, , before
.
:
tpeid(T1).before(typeid(T2)).

1
2,
.
:

12

cout << = << typeid(var_int).name() << endl;


// int
cout << = << typeid(2+3.4).name() << endl;
// double
cout << typeid(Base).before(typeid(Derved)) << endl;
// true

.
.

Visual Studio, ,
, ,
3-4 .
1. ,

.

.
2.
,
.
3.
.
.
,
.
4. . ,
.

1. ,
.
5.
,
.
.

13

6.
,
.
2.
.
,
, ,
.
. ,

,
.
-
().
.
if.
.
if :
if(expr) operator_1; else operator_2;
expr,
.
( true),
operator_1, operator_2.
, .
, else,
. ,
.
switch. switch
.
:
switch(expr)
{
case __1: __1;
case __2: __2;

case __N: __N;

14

default: ;
}

( )
,
.
,
. break.
, ,
default.
while. while
:
while(expr) ;
expr
,
.
0 (false).
.
do while.
:
do while (expr);
while,
, .
, 0.
do while .
for.
:
for(; ; ) ;

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

: goto, break, continue, return.

15


,
x x dx c .
.
,
.
.
1. = 2 = |x| > 1
2. = = - |x|<
3. ln(x+1) = = x- -1 < x 1
4. ln = 2 = 2 |x| < 1
5. - = -1 x < 1
6. arcctg x = + = - |x| 1
7. arctg x = /2 + = + x > 1
8. arctg x = = + |x| 1
9. arth x = = |x| 1
10. arth x = = + |x| > 1
11. cos x = = + |x| <
12. = = - |x| <
13. ln x = 2 = x > 0
14. ln x = = 0 < x 2
15. ln x = = + x >
16. arcsin x = x + =
= x + + + + + |x|<1
17. arcos x = - (+ + + ) |x| < 1
3.
.
++
,
.
:
_ [] = {};

16


,
:
_ [_1] [_2] =
{};

.
( ),



.
,
, .

.
,
,
.

.

(. 4),

.
.
1.
) , n
:
1. ;
2. ,
;
3. ;
) . :
1. ,
:

17

2. ,
.
2.
) , n
:
1. ;
2. ,

;
3. .
) .
,
.

.
,
.
3.
) , n
:
1. ;
2. ,
;
3. ,
,
;
) . :
1. ,
:
2. ,
.
4.
) , n
:

18

1. ;
2. ,
;
3. , ,
1.
.
) . :
1. ,
;
2. ,
.
5.
) , n
:
1. ;
2. ,
;
3. , ,
[a,b].
.
) . :
1. ,
;
2. ,
.
6.
) , n
:
1. ;
2. ,
;
3. ,
,
;
) . :

19

1. ,
;
2. c .
A Aij , Aij
i- j-
.
7.
) , n
:
1. ;
2. ,
;
3. ,
, ,
, .
) 8 8, k, k-
k- .
,
.
8.
) , n
:
1. ;
2. ,
;
3. ,
, 1,
.
)

. ,
.
9.
) , n
:

20

1. ;
2. ,
;
3. ,
, .
) Aij Akl i1 <= k <= i+1, j-1 <= l <= j+1, (k,l) != (i,j).
,

.
10 10.
10.
) , n
:
1. ;
2. ,
, ;
3. ,
, ,
,
.
) ,
.

10 10.
11.
) , n
:
1. ;
2. ,
;
3. , ,
[a,b].
.
)
.

21

.
,
.
12.
) , n
:
1. ;
2. ,
;
3. ,
,
[a.b], .
) ,
, . ,
.
13.
) , n
:
1. , ;
2. ,
;
3.
.
)
n .
14.
) , n
:
1. , ;
2. ,
;
3. .
)
M*N k
: 1-

22

, ,
, ;
.
15.
) , n
:
1. , 0;
2. ,
;
3. ,
,
(0 ).
)
.
,
.
16.
) , n
:
1. ;
2. ,
:
3.
.
) ,

, (2,2)
(3,3) . .,
.
,
.
4.
.

23

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

:
_ *_ = ;

, .
- &_,

. :
int var_int = 38;
//
int *ptr_int_1 = &var_int;
//

int *ptr_int_2 = ptr_int_1;
//

,
, (),
* , ,
cout << = <<
*ptr_int_1 << endl;
(. . )
,
,
cout << = << ptr_int_1
<< endl;
1. ,
,
. ,
.
2.
.
.
,
,

24

->,
.
3. ,
1, .
.
4. ,
.
,
++
.

. ,
,
.
int array_int[10] = {23, 4 -378, 0, -64, 5 ,11};
//
for(int i=0; i<10; i+)
cout << [ << i << ] = << *(array_int+i) <<
;
cout << endl;
//



.
5. ,
3, ,
.
6.
3 ,
, new delete.

.
:
_
_(_
_[]);
_
_(_
_[], const int );

25

_
_(_
*_, const int );
7. ) 3
1,
.
.
.
,
.

.
//
int fun(int arg)
{
return arg*2;
}
// ,
//
int
typedef int (*PF)(int);
// ,

void exemple(PF ptr_fun, int number)
{
cout << <<
ptr_fun(number) << endl;
}
//
//

exemple(fun, 10);
//

26

8. ,
) 2,
.
,
.
void.
.
( , !), ,
.
void,
.
void.
int var_int = -38;
double var_double = 3.64;
int *ptr_int = &var_int;
double *ptr_double = &var_double;
void *ptr_void;
//
ptr_void = ptr_int;
cout << void =
<< *(int *)ptr_void << endl;
ptr_void = ptr_double;
cout << .
void =
<< *(double *)ptr_void << endl;
// int *ptr_int= ptr_void; ,
// int *ptr_int= (int *)ptr_void; , ?
// ptr_int?
9.
, ,
,
void.
.

27

5.
. . .

, .
++

.
, .. ,
, .
,
.
, ,
. (, )
,
.
, ,
.
:
[]
_
_
([
])[throw]
{
//
//
}

, extern static:
- extern
( );
- static .
,
.
( ), void.

. ,
.

void ().

28


, throw,
.
.
#include<iostream>
using namespace std;
//
int summa(int,int);
//

int main()
{
int var_1, var_2;
cout << : ; cin >>
var_1;
cout << : ; cin >>
var_2;
//
cout << = << summa(var_1, var_2) <<
endl;
return 0;
}
//
int summa(int arg_1, int arg_2)
{ return arg_1+arg_2; }

inline. ,
inline-
, ,
.
, ,
. inline .
, ,
.

29

,
. .

,
static, :
void function()
{
static int count = 0;
while(exp)
{
//
cout << = <<
count++ << endl
}
//
}
count
.
.
,
, , , .

. ,
,

.

, ,
int *function()
{
int var = 38;
return &var;
}
,
, ,
.

30


.
:
.

.
.
,
.

.
.

.
//
void function(int, int *, int &);
int main()
{
int i = 10, j = 20, k = 30;
cout << : i= << i << << j= << j <<
<< k= << k << endl;
function(i, &j, k);
cout << : i= << i << << j= << j <<
<< k= << k << endl;
return 0;
}
//
void function(int a, int *b, int &c)
{
a++; (*b)++; c++;
}
;
(++ )
() i.
, ,

31

, .
,
.

j k .
.
,
,
.

.
int summa(const int *arr, const int size)
{
// int summa(int arr[], const int size)
// int summa(int arr[size])

int sum =0;


for(int i = 0; i<size; i++)
sum += arr[i];
return sum;
}

, ,
int summa(const int *arr, const int nstr, const int nstb);,

int summa(const int **arr, const int nstr, const int nstb);
int summa(const int arr[][], const int nstr, const int nstb);

, ,
.
++
,
. .
.
//
int summa(int, int);

32

//
typedef int ( *ptr_fun)(int,int);
// ,

void function(ptr_fun pf)
{
cout << = << pf(38,64) << endl;
}
,
.
.
long factorial(long n)
{
If(n==0||n++1) return 1;
return (n + factorial (n -1));
}

.
-

,
.
, ,
++,
.

.
,

.
int summa(int arg_1, int arg_2)
{
return arg_1+arg_2;
}

33

double summa(double arg_1, double arg_2)


{
return arg_1+arg_2;
}
, , ,

. ,

, , bool char int,
float, double, void * . .
, .
,
.

:
-
;
- ,

;
- ,

const
;
- ,
.

.
,
,
.
,
. :
template <class Type> __
{
//
}

34

Type
.
<class Type>,

, , <class T1, class T2, class T3>. class
typename. :
template<class Type> Type summa(Type arg_1, Type arg_2)
{
return arg_1+arg_2;
}

, , cout << = <<
summa(20,30).
, ,
.
,
,
, cout << = <<
summa<int>(20,30); cout << =
<< summa<float>(20.5,30.6);.
.

.
.
1.
.
2 ,
,
.
.
2.
. 1,

.
3. .
,

35

3, ,
.
4. .
, (
100 ), ,
.
5.
. ,
3, ,
.
6. .
,
.
7. .
, .
8. .
3 ,
int double.
9. .
, 1 ( )
3
.
.

36