Sie sind auf Seite 1von 85

C++

C++
:
E_mail:
xyzhong@seu.edu.cn
2.1
C++










C++

2.2 C++

#include<iostream.h>
void main()

{
C++
cout<<"hello,world\n";
// hello,world
/* cout << */
}



#include<iostream.h> C++
C++ # C+
+
#include
<> iostream.h

C++

#include<iostream.h>

#include <iostream>
using namespace std;
2.3






2.3.1

Keyword

C++
C++
ISO14882 74
C++

2-1 C++

bool char wchar_t class const double


enum float int long short signed struct
union unsigned void volatile

auto extern inline register static



friend private protected public

asm explicit namespace operator template this

typedef typename using virtual

break case catch continue default do else

for goto if return switch throw try while

delete false new sizeof true



2.3.2
Identifier ID

C++ VC++
1 247

MyFile 94Salary
Salary 94 _94Salary
amount $amount
void
f3.5
Num_of_Student
2.3.3


# ( ) {}

2.3.4
C++

C++
Tab
2.4



int

char


float


bool
C++ double
void
type []

type *
struct
union
enum
class
2.6






2.6.1

123 -24
0123 -025
0x123 -0x36
-
123L 123U 123UL
U u-
L l
2.6.2
10

(1) 09

( 78. .06,.0)
0.23 -125.76 0.0 .
46 -35.
C++
8
F f
1234F,-43f 4
(2)
10 E e
E e

123E12 1.43E3.5
E4 -.34e-2
2.6.3

1
ASCII

1.
a // a 97
@ // @
4 // 4
//

e E
ASCII
4 4 4
ASCII 52
0 \0 0
ASCII 48 \0 ASCII
0
2.
\





\E E


\a
\b Backspce

\f ( )
\n
\r
\t (Tab
)
\v
\\
\'
3.
\
ASCII

x
0~255 0
'\021' '\x78' '\0' '\56'

\101 A A
2.6.5

:I am a Chinese.,123, a



a a
\0 0x6100

a a
0x61
( )
( )
2.6.6
const
const

const float PI=3.14159;


const int Number_of_Student=100;



C++

2.6.7
C++ #define
( )

# define

# define PI 3.1415926
2.5

int(
4Byte) float( 4Byte)
double( 8Byte) char(
1Byte)

2.5.1
C++
[ ] 1[, 2,
, n]
i,j,k


int i, j, k; x, y, z
float x, y, z;

char c1, c2; c1,c2
double dx;

1
int a=3, b=4, c=5;
float x=3.0
2
float x, e;
x=3.5; e=2.71828;
: int a=3;
int a(3);

int a, b, c;
a=b=c=5
int a=5,b=5,c=5;

int a=b=c=5;
:
int a=10;

float a;

char c=A;
char c=A;

C++

bool flag;

false true
2.5.2
C++



& =

#include <iostream.h>
void main()
{ refi i

int i=10;
int &refi=i;
refi=20;
cout<<"i="<<i<<'\n';
}
2.7
C++

C++



C++


2.7.1
C++ 2-5
C++


> > > >


C++
2.7.2

a+b 3+12 a+55


a-b 34-c d-100
a*b 11*c f*10
a/b 100/4 30.0/4.0
( )
2%5 10%3 6%3
+ - * /

+ - * /
float double
double




4/5 0
4.0/5 4.0/5 4.0/5.0
0.8
5/4 1
5.0/4 5/4.0 1.25
2.7.4

= =
=


true( ) false( )

5<x<10 int
x=3; : 5<x<10 // C+
+ ?
5
x 3 < F0

10
< T1

5<x&&x<10
x 5 10

== =( )
2.7.5




2 !
, ;

12 &&
,
;
13 ||
, ;
2.7.5



C++ 0 0
void


21&&0 // 21 0 0
21||0 // 21 0 1
!21 // 21 0

2x-1 (0<x<10)
y=
4x2+2x-1 (x0,x10)
0<x<10
0<x && x<10
x0,x10
x<=0 || x>=10

0
0
1 0

C++




( )


C++

int a=0,b=4,c=5;
a&&b++&&--c

a=0 0
b++ c,
a,b,c
: b||a++||c- -

2.7.3


a=b=3;
b a
2).
C++

a=100;
3.1415926=pi;
x+y=z; b=32+a*3

const int N=30; ;
N=40;
(3)
C++

10
+ = = * = / = % = <<= >>=
&= | = ^ =

=
x+=5 x=x+5

a+=a-=a*a
a 12

"a-=a*a" :
a=a-a*a=12-144=-132
a+ -132
a=a+(-132)=-132-132 -264
a 12, a+=++a
26 a-=++a 0
2.7.7
C++

1 2 n

a=(x=3 x+=6 5+6)


//a=11 x=9


++ --
1 1 ++ --
( )
( )
++i --i i
( )1 i++ i--
i
i ( )1
int a=5,b=4,c;
c=(++a)+b; c=(a++)+b;

2.6

#include <iostream.h>

void main() k=8 i=6
{ n=7 j=6
int i=5,j=5;
int k,n;
k=++i +2; //A
cout<<"k="<<k<<" i="<<i<<'\n';
n=j++ +2; //B
cout<<"n="<<n<<" j="<<j<<'\n';
}
2.7.6
C++

1 ~
2 <<
3 >>
4 &
5 |
6 ^
1 ~
1
0 0 1 a
10011011
~a 01100100
2 <<
a<<n
a n
a
n 0 n
2 n

3 >>
a>>n


0
n 2 n
VC++6.0

4 5
& |


1
1 1
0 0
1, 0 a 01001101
a 01001101 b 00001111
a | b 01001111
b 00001111
6 ^


1
0
a 01001101
b 00001111
a ^ b 01000010
2.7.7
C++


1 2

( 0) 1
2 1 2
a>b?(max=a):(max=b)

a b max=a
max=b a b
max
max=a>b a:b
sizeof
sizeof

sizeof( )
sizeof( )

sizeof(int) 4
int
4



char short
int char short
unsigned int float

long double, double, unsigned long


long, long long, unsigned long, long


doublefloat

long

unsigned

int char, short

10+'a'+1.5



( )
( )( )

double f=3.6;int
i=(int)f; i 3, f
3.6

#include<iostream.h>
void main()
{ double f;
cout<<" ";
cin>>f;
int i=f+0.5;
cout<<"
"<<i;
}



:
double x=2.3,y; int i=2,j;
(1) j=x; (2) y=i;
2 2.3
(1) j 2 2.3 x

(2) y 2.0 2 i
2.0 2

2.8 C++
C++ :

1. C++

int a;
2.
a=10;
3.

break;
4.

about();
5.

6.
C++

2.9




C++ /
C++
cin cout
cin cout iostream.h

C++
cin

cin>> 1[>> n];


cout

cout<< 1[<< n];

# include <iostream.h>
1.

int i, j; float x, y;
cout<<Input i, j, x, y:<<endl;
cin>>i>>j; //D
cin>>x>>y;
cout<<i=<<i<<\t<<j=<<j<<endl;
cout<<x+y=<<x+y<<endl;
10 20 4.5 8.6 <CR>

i=10 j=20
x+y=13.1
cin

TAB

cin


2.
1 cin
char c1, c2, c3;
cin>>c1 //A
cin>>c2>>c3;
A

A b c <CR>
cin A b c
c1 c2 c3
cin
backspace
cin

cin.get()

2 cin.get()
cin.get(< >)
char c1, c2, c3, c4;
cin.get (c1); //B
cin.get (c2);
cin.get (c3);
B A
b<CR>
A b
3. cout

cout<< 1[<< <
n>]
<<

cout ( )
( )


c1 c2 c3 a b
c
cout<<c1<<\t<<c2<<\n;
cout<<c3<<endl;
cout<<O<<k<<endl;

a b
c
Ok


char c1, c2, c3;
cout<<Input c1, c2, c3:<<endl;
cin>>c1>>c2>>c3; //C
cout<<c1 is <<c1<<\t
<<c2 is <<c2<<\t
<<c3 is <<c3<<endl;
Input c1, c2, c3:
C
a b c <CR>

c1 is a c2 is b c3 is c

4.
1

cin cout
oct hex
dec
int i, j, k, l;
cout<<Input i(oct), j(hex), k(hex),
l(dec):<<endl;
cin>> oct>> i;//
cin>> hex>> j; //
cin>> k; //
cin>> dec>> l; //
cout<< hex:<< i=<< hex<< i<< endl;
cout<< dec:<< j=<< dec<< j<<\t;
cout<< k=<< k<< endl;
cout<< oct:<< l=<< oct<< l;
cout<< dec << endl; //

0x
0x
0 0

cin cout


C++
setw()

int i=2, j=3;


float x=2.6, y=1.8;
cout<< setw(6) << i << setw(10) << j
<< endl;
cout<< setw(10) << i*j << endl;
cout<< setw(8) << x << setw(8) << y
<< endl;
2 3
6
setw()
1
iomanip.h
#include <iomanip.h>
2

setprecision()

cout setprecision ()
:
3.14159
float pi=3.1415926; 3.14
cout<<pi<<'\n'; 3.1416
cout<<setprecision(3)<<pi<<'\n';
cout<<setprecision(5)<<pi<<'\n';


.cpp

.obj


.exe

85

Das könnte Ihnen auch gefallen