TR
:
:
: 17209
TRÌNH :
DÙNG CHO SV NGÀNH :
- 2008
i
Mô
: Lp trình h tng và C++. : 2
: Khoa hc máy tính. : CNTT
: 17209 C: 4
:
K thut lp trình Pascal, K thut lp trình C.
:
c phng pháp lp trình h tng
lp trình .
- rng c lp trình h tng.
- tng và l.
- gói, th k, a hình.
- B m
TS
LT
BT
KT
:
++
3
3
1.1 c l h tng
1.2 Gi thi ngôn ng C++
:
9
6
3
2.1 Cài t ngôn ng C++
2.2 C trúc mt chng trình C++
2.3 Ki d li c s
2.4 Quy t s dng t khóa, tên chu, tên t
t.
2.5 Các ch th gán, so sánh, i kin n thì
,
6
3
3
3.1 Khai báo hàm con, hàm chính.
3.2 Quy t ki d li, ki tr.
3.3 ngh ch hàm, tham s ng
3.5 .
++
9
5
3
1
4.1 tng vào ra cout, cin
4.2 dòng d li lên màn hình
4.3 Nhp dòng d li t bàn phím
4.4 d dòng d li hi th
4.5 Vào ra v tp
18
8
9
1
75
45
30
0
0
0
ii
5.1 ngh tng
5.2 Khai báo l
5.3 Hàm thi lp, hu b
5.4 Thành ph t, hàm bn, l bn
5.5 ngh ch toán t
Chng 6: Th k
9
6
3
6.1 L c s, l dn xu
6.2 Quy t tha k
6.3 Tng thích l c s và l d xu
6.4 n tha k, a tha k
:
8
5
3
7.1 Hàm , ràng buc t, ng
7.2 a th
:
13
6
6
1
8.1 Hàm b m
8.2 u khuy i c hàm b m
8.3 L b m
:
2. Ph Vn . K thut lp trình h tng. NXB KHKT. 1998
3 liên quan.
:
- thc hành.
-
.
B
/ /20
iii
CHNG I:
C++ 1
1. S 1
1.1
(
) 1
1.2
1
1.3
3
1.4
ng 4
2.
ng 5
2.1
u tng ADT(Astract Data Type) 5
2.2 ng (Objects) p (Classes) 5
2.3 a (Inheritance) 6
2.4 Dynamic Binding () (
) 6
3. Ngôn ng
++ . 7
3.1 S
ng 7
3.2 Ngôn ng
++. 8
8
CHNG II: NH
9
1. Ch
9
1.1 Qu
++ 9
1.2 Ch
. 13
2. ,
15
2.1 (variable declaration) 15
2.2
16
2.3
16
2.4 17
2.5
18
2.6 18
3. ++ 19
4. 20
4.1
-else 20
4.2 20
4.3 while 21
4.4 21
4.5 22
4.6
22
4.7
23
23
iv
5.
++ 23
6.
trong C++ 25
6.1 (assignment operator) 25
6.2 25
6.3
25
6.4 logic 26
6.5 bitwise 26
6.6 26
6.7
26
6.8 3 ngôi 26
6.9
27
6.10
27
6.11
27
6.12 sizeof. 28
7.
28
31
CHNG III:
,
. 34
1. ++ 34
1.1
34
1.2 34
1.3 34
1.4
(overload) 35
1.5 36
2.
, 37
3. 38
4. Bài t 38
CHNG IV: ++ 40
1.
++ 40
2. 41
3.
41
4.
(Redirection) 41
5.
i cin 42
6. C
42
7.
i cout 45
8. 46
9.
46
10.
48
v
11. 48
12. stringstream 49
52
CHNG V:
P. 54
1. Tru tng d
54
2.
ng? 54
3.
ng 55
4.
ng 58
5.
p v
59
6.
ng th
p 61
7.
62
8.
66
9. Kh
67
10. 72
11. copy 76
12.
78
13. p 79
14. S ng trong vai 80
15. :
. 82
16.
84
91
CHNG VI:
A (INHERITANCE) 94
1. S
94
2. S
94
3. a 94
4.
96
5.
98
6. a 101
6.1 a public 101
6.2. a private 101
102
7.
102
8. a 103
9. a 103
10.
105
11. a 107
12.
p c s
p c s 108
vi
13.
ng 109
14.
a 116
117
CH 118
1.
118
118
- Polymorphism 118
2. C
119
3.
120
4. 122
4.1
122
124
5.
124
6. p tru tng abstract class 128
7. c s 129
7.1 1 129
7.2 2 131
8. 135
9. 136
p 139
CHNG VIII: (TEMPLATE) 140
1. u 140
1.1 141
1.2 146
1.3
p tr 147
1.4 149
1.5
151
1.6 p cha ()
154
2. 154
2.1 154
2.2 th
157
157
O 158
159
1
NG I:
++
1.
.
.
:
+
+
+
+
.
1.1
(
)
.
(
).
:
+
.
.
+
,
.
+
.
.
,
.
1.2
.
2
.
. Sau khi
.
.
,
.
.
,
.
.
,
.
,
:
.
,
,
.
,
,
module.
.
.
3
1.3
.
.
.
.
.
.
:
+
.
.
.
.
.
+
.
.
,
.
+
.
.
.
4
1.4
(object) ,
(class) au,
.
(instance) ,
.
(struct hay record)
.
:
+
.
,
.
70% .
+
,
.
+
.
+
.
(
) (
, U
+
.
.
.
.
5
2.
2.1
(Astract Data Type)
.
.
:
()
.
.
.
.
cho
(data type)
(data structure).
(
)
ADT.
.
: nguyên.
: ADT Integer:
:
+
N.
:
constructor:
sub(k): k.
add(k): + k.
End
2.2 (Objects) (Classes)
.
.
.
.
.
(instance) .
.
.
,
. ,
.
6
2.3
(Inheritance)
.
t
.
.
( )
(
base class).
.
.
.
2.4 Dynamic Binding () (
)
.
,
.
().
. :
int main()
{
Shape shape_list[4];
int choose;
int i;
for(i=0;i<4;i++)
{
cin >> choose;
if(choose==0)
{
shape_list[i] = new Circle();
}else{
shape_list[i] = new Rectange();
}
}
for(i=0;i<4;i++)
{
7
shape_list[i]->draw();
}
}
(file .exe)
_
tange
.
,
binding.
static
binding.
(polymorphism).
.
.
trên O
.
3.
++ .
,
, .
.
.
:
4
:
1: 1954 1958 (Fortran I)
2: 1959 1961 (Fortran II, Cobol)
3: 1962 1970 (Pascal, Simula)
,
4:
.
.
3.1
1967 Simula
1970 to 1983 Smalltalk
8
1979 Common LISP Object System
1980 Stroustrup starts on C++
1981 Byte Smalltalk issue
1983 Objective C
1986 C++
1987 Actor, Eiffel
1991 C++ release 3.0
1995 Java
1983 to 1989 Language books with OO concepts
1989 to 1992 Object-oriented design books
1992 to present Object-oriented methodology books
Java
Self
Python
Perl
Prograph
Modula 3
Oberon
Smalltalk Venders
ParcPlace, Digitalk, Quasar
Prolog++
Ada 9X
Object Pascal (Delphi)
Object X, X = fortran, cobal, etc.
C#.
++ .
++
.
3.2
++.
C++ (AT & T
Bell Lab) ( 1994) C.
C++
: ,
, ,
. Tuy nhiên
++ ,
++
.
9
:
1.
1.1
++
(
cao)
( ).
(interpreter) (compiler).
:
(activity),
.
.
.
. BASIC
.
,
.
( ).
,
.H
.
.
:
.
.
.
.
T ,
,
.
,
.
(
)
c
.
. separate compilation (
). Biên
.
. .
.
.
.
10
. ,
.
.
.
(tracing) .
(in-memory compilation).
. ,
.
++
.
(
++)
.
(
: preprocessor directives).
.
.
. .
.
. : +
,
+,
.
(global
optimizer)
,
.
,
.
(
.
.obj).
(peep-hole)
assembly t
.
.
.
,
.
.
,
;
.
.
.
.
librarian.
K
11
.
.
.
(Java ch)
(dynamic type checking).
. C++
.
, .
++
:
nhanh,
, .
++
- .
.
++,
.
.
++
,
,
.
,
(
1 ).
, ,
.
,
.
.
,
.
, ++
.
.
(declaration). Khi
.
.
,
++
.
1
;
.h,
.
.hxx.
.
.
#include.
12
#
.
#
< >
.
: #include <header.h>
,
.
.
< >.
<>,
.
++
.
.
8
.
++
,
++.
(
.o
.obj),
,
.
.
.
,
.
.
.
,
.
:
+
+
+
.
++.
13
++, ,
,
sau:
,
.
, t
.
.
.
,
,
. ,
.
a
.
.
. Tuy
.
Kh
++
,
.
(startup),
++
.
.
. i
(
).
1.2
.
.
++,
.
// File hello.cpp
//
#include <iostream.h> //
int main() {
cout << "Hello, World! I am "
14
return 0;
}
++ ,
.
.
, : , World!.
++
.
,
. C++
:
/*
*/
:
// ,
++
++.
.
#include.
++.
,
g
.
()
.
++
main()
0
.
: int main()
.
cout (console output) <<.
<<
++
cout,
++ .
, C++
. ++
endl
cout .
<<.
,
(:
)
.
15
,
(
)
0
.
:
Tcc eHello hello.cpp.
.exe.
2.
,
2.1
(variable declaration)
,
.
.
(
) .
++ :
<
> ;
.
: int a;
,
( )
( ).
(
).
extern,
:
extern int a;
.
:
// file: Declare.cpp
//
extern int i; //
float b; //
int i; //
int main() {
b = 1.0;
16
i = 2;
}
,
.
2.2
(
)
.
cho
}
{
.
{
}
.
n nhau.
2.3
++
.
.
.
.
(tên,
,
) .
++
for, while, do
if,
switch. :
for(int i=0;i<10;i++){
}
}
}
switch(int i=cin.get()){
}
17
. :
}
. !=
=
.
2.4
(global variable)
ng
(
). (
).
(
)
.
(
, local)
,
.
.
.
(register variable)
.
register.
.
.
.
.
, ,
.
(static variable)
.
,
.
,
.
,
.
.
18
, .
t
.
.
.
2.5
++
.
:
.
( )
.
,
.
.
.
.
kh . (
const) .
extern. ,
.
.
2.6
, :
#define PI 3.14159
3.14159.
++
.
,
( ).
.
PI.
C++
,
. ++
.
-
, ,
(
)
.
. 0
, 0
.
: 1e4, 1.4e4.
, F, L,
.
,
\
.