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

1

,

C C++ 
.
, 
. 
. , 
. 
,
, .


C89 .

char

int

float

double

void

C99 
.

, (/)

_Bool

_Complex

_Imaginary

C++ .

, (true/false)

bool

char

24

int

float

double

void

wchar_t

, C C++
: char, int, float, double void.
, , 
C99 C++, 
: _Bool (C99) bool (C++). C89
.

.
signed
unsigned
short
long
, 
.
C C++ , 
,
.
. ,
( 
),
,
, 
. , int 
32 76832 767.
, char: 
.

char

127127 0255

unsigned char

0255

signed char

127127

int

32 76732 767

25

unsigned int

065 535

signed int

int

short int

int

unsigned short int

065 535

signed short int

short int

long int

2 147 483 6472 147 483 647

signed long int

long int

unsigned long int

04 294 967 295

long long int

63
63
(2 1)2 1 ( C99)

signed long long int

long long
int ( C99)

unsigned long long int

02641 ( C99)

float

double

10

long double

10

wchar_t

unsigned int


( ), 
int. , 
, unsigned.
, .
unsigned int i; // int
unsigned i;
// int



. :
_;
, x float, y
ch , 
:
float x;
int y;
char ch;

26
,
, . ,
.
int a, b, c;


,
. , 
count 
100:
int count = 100;
,
. 
. C
(static) 

.

, , 
, . C C++ 
,
, .
( .)
, 
. :
.
. 
,
. 
,
. , , 
, ,
. C89
6 31 
. C99 : 
31 ,
63 . C++ 1 024 
.

(, first_time)

27
(, _count). , 
, 1
. , test TEST 
. C C++ , 

, .

C++.
class.
C .
,
. ,
, . 
.
class _ : _ {
// .
protected:
// , .
public:
// .
} _;
_ .
_
,
. _
_, . 
.
. _
.
, . (.
.)

. ( .) 
, 
.
. ,
,
(~). , 
, . 

, .

28
,
. 
, 
,

public, .
class myclass {
int a, b; // myclass
public:
// , .
void setab(int i, int j) { a = i; b = j; }
void showab() { cout << a << ' ' << b << endl; }
} ;
myclass ob1, ob2;
myclass, 
: a b. 
setab() showab(). 

myclass ob1 ob2.
,
() , ,
.. protected.
, 
.

(.). 
(>) 
. , 
putinfo() ob 
, show() 
.
struct cl_type {

int x;
float f;
public:
void putinfo(int a, float t) { x = a; f = t; }
void show() { cout << a << ' ' << f << endl; }
} ;
cl_type ob, *p;
// ...
ob.putinfo(10, 0.23);

29

p = &ob; // ob p.
p>show(); // ob.
template
, , . (. template
5.)

C++ .
,
 . 
, .
:
class _ : _
__ {
// . . .
} ;
_

: private, public protected. 
, 
public,
(struct), private,
class. 
, .
_ 
public, public protected
public protected
. _ 
private, public protected
private
. (protected) public
protected protected
.
derived 
base , .. 
private. , i 
derived.
class base {
public:
int i;

30
};
class derived : private base {
int j;
public:
derived(int a) { j = i = a; }
int getj() { return j; }
int geti() { return i; } // derived
// i.
};
derived ob(9); // derived.
cout << ob.geti() << " " << ob.getj(); // OK
// ob.i = 10; // : i
// derived!

struct.
C++ .
class struct ,
. 
,
private. 
:
struct _: _ {
// .
protected:
// ,
// .
private:
// .
} _;
C 
. ; 
. C 
. , ,
public, protected private
.

31

, 
. C++ 
, .
. 

private. :
union _ {
// .
private:
// .
} _;
C ,
private .
. ,

union tom {
char ch;
int x;
} t;
tom,
( ).

, ,
, .

.

. 
. .
. 
. 
. 
, (=). 
, ,

32

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

. , ,
, 
, . ,

, 
.
.
union { // .
int a;
// a f
float f; // .
};
// ...
a = 10;
// a.
cout << f; // f.
, a f, .
, 
, .


C++ C (C
), ,
struct.
class, , , 
. ,
C, POD (Plain
Old Data ).
, 
, . ,

 . 

33
private protected.
, ,  1
,
(namespace), 
static.

, 
.
. ,
, 
.

enum. :
enum _ {_} _;
_ .
, , 
.
, 
, cities, 
c cities, c 
Houston.
enum cities {Houston, Austin, Amarillo} c;
c = Houston;
( )
0, 1,
2 .. , 
,
. , 
. ,
Austin 10.
enum cities {Houston, Austin=10, Amarillo };
Amarillo 11, 
, 
.

C
C, C++, ,
. 

34
,
C++, C.
struct s_type {
int i;
double d;
};
// ...
s_type x; // OK C++, C
C++ s_type
,
. C s_type (). 
C 
(struct, union
enum), .
struct s_type x; // C
C++, 
.


extern, auto, register,
static mutable 
C C++. 
, .

extern
extern 
, ,
. ,
. , extern 
, 
. extern 
,
.

auto
auto , 
, ,
. 
, , 
, auto
.

35

register
C , register

, 
,
, . 
.
register . 
register, 
.
 ,
, , 
.
,
register ,
. .
,

. 
register.

static
static


. ,

.
static
.
, . ,
.
,
.
C++ static 

, .

mutable
mutable C++.

, .. , mutable 
, 

36
const, const 
.


const volatile 
, 
.

const
const
. , , 
, const,
.
, (read
only memory ROM). , const,
,
 . 
,
const int a = 10;
a 
10, .

.

volatile
volatile ,
, 
. , 

.

.
, 
,
.
.
volatile 
, 
.

37


 
const, , 
.  
, const 
, .
class MyClass {
int i;
public:
// const.
void f1(int a) const {
i = a; // ! const
// ,
// .
}
void f2(int a) {
i = a; // OK, const.
}
};
, f1()
const 
, .

restrict
C99 , 
restrict. . 
,
restrict, 
, . 
,
. ,
,
restrict . , 
restrict,

, malloc(). 
restrict .
C++ restrict .

38


. 
:
_[];

, . 
, x
100 , :
int x[100];
, 100 , 
0, 99. ,
x 
0 99.
for(t=0; t<100; t++) x[t] = t;
,
, .


. ,
10 20, :
int x[10][20];

, ,
.
int count[5] = { 1, 2, 3, 4, 5 };
C89 C++ 
. , C89 C++ 

. C99 

, , 
.
.
, 
, .

typedef 39


...


typedef
typedef
. :
typedef __;
,
, feet int.
typedef int feet;

distance.
feet distance;

, ,
,
. &
. &
. .
, 'a' '+' . &
.
, 10 100 . &
, &
, 11,123.
&
.
: float double.
, ,
.

. &
&
( ) ,
double.
. &

.
,
.

40
: 
F, float,
L, long double.
U unsigned, 
L long. .

int

1, 123, 21000, 234

long int

35000L, 34L

unsigned int

10000U, 987U

float

123.23F, 4.34e3F

double

123.23, 12312333, 0.9876324

long double

1001.2L


. 
8,
0 7.
10 , 8 . 
16
0 9 A F,
10, 11, 12, 13, 14 15. ,
10 16 
. (
) ,
C C++ 
, . 
0x
( x) 0X, .
.
int hex = 0x80; // 128
int oct = 012; // 10


C C++ 
, . , 
, " ". 
. 
, 'a'. "a"

41
, . 
 1
. , C++ string,
.

()
C++ : true
false.
C99, C _Bool,
, .
, <stdbool.h>,
true false. ,
<stdbool.h>
bool _Bool.
, C99, 
C++. , C89
.


C99
<complex.h>, 
, .
_Complex_I

(const float _Complex)i

_Imaginary_I

(const float _Imaginary)i

_Imaginary_I ( _Complex_I,
)

i , 
1.

()


,
, (, 
),
. C C++

( ),
.
.

42

\b

\f

(
)

\n

\r

\t

\"

\'

()

\\

\v

\a

()

\N

( N 
)

\xN

( N 
)

\?

, 
. , 
, 
, " ".
cout << "\n\t ";