1
2
3
4
Khoa Công
5
9/14/2014
Môn tiên
:
môn
Thông tin
2
Tên:
trình
viên
tín
:
45
TH:
30
14/09/2014
:4
:
LT:
Liên
trình
:
Ngôn
3
14/09/2014
lý
;
hành
hành: C++
4
Trang
cho sinh viên
và
trình
quan
, các nguyên
,
lý
và
các
cài
cao trong
nâng
các
sau cây
hình, các tính
và phân
thông
. Cách
các
trao
và
.
và
.
14/09/2014
5
[1] C++ và
trình
Khoa
GS
14/09/2014
Bài
2000
6
: 20%
hành: 30%
[2] A Complete Guide to Programming in C++, Ulla
Kirch-Prinz and Peter Prinz, Jones and Bartlett
Thi
: 50%
Publishers, 2002
[3] The C++ Programming Language, The 3rd
Edition,
Bjarne
Stroustrup,
Addison-Wesley
Professional, 2003
14/09/2014
7
14/09/2014
8
1: Các
2:
3:
C++
quan
trình
và
4:
hàm
5: Tái
toán
6: Tính
7: Tính
8:
14/09/2014
hình
khác
9
1
2
3
4
5
Khoa Công
6
9/14/2014
Inline Functions
2
ý
tên
phong cách
trình:
nguyên và
các giá
hàm,…)
Tab
Có bao nhiêu cách
Khai báo prototype
{}
14/09/2014
3
1. Dùng 4
cách dài
2. Dùng
khai báo
14/09/2014
4
5. Dùng
vòng
for
code
không tách riêng
1
2
thay cho
3. Dùng
và vòng
code
do while
1
6. Dùng hàm
thay cho
code có
tách riêng
tái
7. Dùng file
4. Dùng
và vòng
for
14/09/2014
for
code
file thay cho
bàn phím và
ra màn hình
vòng while
5
14/09/2014
6
Cách 1: Dùng 4
Cách 2: Dùng
void main(){
int a1, a2, a3, a4;
printf("\nNhap a1 = ");
scanf("%d", &a1);
printf("\nNhap a2 = ");
scanf("%d", &a2);
printf("\nNhap a3 = ");
scanf("%d", &a3);
printf("\nNhap a4 = ");
scanf("%d", &a4);
printf("\nBan vua nhap 4 so: %d %d %d %d\n", a1, a2, a3, a4);
}
void main(){
int a[4];
printf("\nNhap a1 = ");
scanf("%d", &a[0]);
printf("\nNhap a2 = ");
scanf("%d", &a[1]);
printf("\nNhap a3 = ");
scanf("%d", &a[2]);
printf("\nNhap a4 = ");
scanf("%d", &a[3]);
printf("\nBan nhap 4 so:%d %d %d %d\n", a[0], a[1], a[2], a[3]);
}
14/09/2014
14/09/2014
7
Cách 3: Dùng
và vòng
while
14/09/2014
void main(){
int a[4], i;
i = 0;
do{
printf("\nNhap a%d = ", i);
scanf("%d", &a[i]);
i++;
}while(i<4);
i = 0;
printf("\nBan vua nhap 4 so:");
do{
printf("%d ", a[i]);
i++;
}while(i<4);
}
Cách 4: Dùng
8
và vòng
for
void main()
{
int a[4], i;
for (i=0; i<4; i++){
printf("\nNhap a%d = ", i);
scanf("%d", &a[i]);
}
printf("\nBan vua nhap 4 so:");
for (i=0; i<4; i++){
printf("%d ", a[i]);
}
}
9
14/09/2014
10
Cách 5: Dùng
và vòng
for
Cách 6: Dùng hàm
void main()
{
int a[4], i;
for (i=0; i<4; i++)
{
printf("\nNhap a%d = ", i);
scanf("%d", &a[i]);
printf("%d ", a[i]);
}
}
14/09/2014
11
14/09/2014
12
13
14/09/2014
14
Cách 7: Dùng file
14/09/2014
C
niên 1980: Bjarne Stroustrup
(Bell Laboratories)
Cung
và
trình
Edit
Preprocess
Compile
Link
Load
Execute
trình
Ngôn
Biên
thi
C++:
lai
Disk
Program is created in
the editor and stored
on disk.
Preprocessor
Disk
Preprocessor program
processes the code.
Compiler
Disk
Compiler creates
object code and stores
it on disk.
Linker
Disk
Editor
Linker links the object
code with the libraries,
creates a.out and
stores it on disk
Primary
Memory
Loader
Loader puts program
in memory.
Disk
..
..
..
Primary
Memory
CPU
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.
16
..
..
..
14/09/2014
15
14/09/2014
Chú thích
vi và khai báo:
Các
Không
C, chúng ta có
trí
trong
tra
vi và khai báo
có
khai báo.
C
Không gian tên
Do
xác
trong
C++ cung
toán
rõ
nào
tên
lý
nó
vi (::)
khi
ra tình
trong
con.
Tham
14/09/2014
tác
khai báo
trình.
17
14/09/2014
18
Toán
vi (::)
dùng
trong
Ví
truy
các
có
toàn
trùng tên
:
y = ::x + 3;
14/09/2014
19
12
// display values of local and global PI constants
13
cout << setprecision( 20 )
14
<< " Local float value of PI = " << PI
15
<< "\nGlobal double value of PI = " << ::PI<< endl;
16
return 0; // indicates successful termination
17 } // end main
1
2
3
4
5
6
7
8
9
10
11
// Using the unary scope resolution operator.
Access the global PI with
#include <iostream>
::PI.
#include <iomanip>
Cast the global PI to a
using namespace std;
float for the local PI.
This example will show
// define global constant PI
the difference between
float and double.
const double PI = 3.14159265358979;
int main()
{
//define local constant PI
const float PI = static_cast< float >( ::PI );
14/09/2014
20
cin
cout
cerr
Borland C++ command-line compiler output:
Local float value of PI = 3.141592741012573242
Global double value of PI = 3.141592653589790007
thông báo
Microsoft Visual C++ compiler output:
Local float value of PI = 3.1415927410125732
Global double value of PI = 3.14159265358979
14/09/2014
21
14/09/2014
22
1
2
3
4
5
6
7
8
9
10
11
12
cin and cout (and #include <iostream>):
cout << "hey";
char name[10];
cin >> name;
cout<<"Hey "<
cout << endl;
// Fig. 1.2: fig01_02.cpp Function Single-line
Preprocessor
main
returns
an directive to include
comments.
// A first program in C++. integer value.input/output stream header file
<iostream>.
Left brace { begins
#include <iostream>
Function main appears exactly
function body.
using namespace std; once in every C++ program..
// function main begins program execution
Statements end with
int main()
a semicolon ;.
Corresponding right brace }
{
ends function body.Stream insertion operator.
cout << "Welcome to C++!\n";
Name cout belongs to namespace std.
return 0; // indicate that program
successfully
Keywordended
return
is one of several means to
exit function; value 0 indicates program
terminated successfully.
} // end function main
Welcome to C++!
14/09/2014
23
1 #include <iostream>
2 using namespace std;
Declare integer variables.
3 // function main begins program execution
4 int main(){
extraction
operator with
5
int integer1; // first number toUse
be stream
input by
user
standard
input
stream
to
6
int integer2; // second number to be input by user obtain user input.
7
int sum;
// variable in which sum will be stored
Calculations
can be performed in output statements:
8
cout << "Enter first integer\n";
// prompt
alternative
for
lines 12 and 13:
9
cin >> integer1;
// read an integer
std::cout << "Sum is " << integer1 + integer2 << std::endl;
10
cout << "Enter second integer\n"; // prompt
Stream manipulator std::endl
11
cin >> integer2;
// read an integer
outputs
a newline, then “flushes
12
sum = integer1 + integer2; // assign result
to sum
output buffer.”
13
cout << "Sum is " << sum << endl; // print sum
12
return 0; // indicate that program ended
successfully
Concatenating,
chaining or cascading
stream insertion operations.
15 } // end function main
14/09/2014
25
14/09/2014
24
#include <iostream>
using namespace std;
void main() {
int n;
double d;
char s[100];
cout << “Input an int,
string.”;
cin >> n >> d >> s;
cout << “n = “ << n <<
cout << “d = “ << d <<
cout << “s = “ << s <<
a double and a
“\n”;
“\n”;
“\n”;
}
14/09/2014
26
Ví
simple
integral
enum
1: Hàm
thông báo
trong Visual C++
structured
array struct union class
floating
char short int long bool
float double long double
address
pointer
14/09/2014
Ví
reference
27
14/09/2014
2:
28
:
Gán các giá
nhiên cho các tham
Khai báo tham
nhiên:
các tham
nhiên
vào khai báo, không
hàm có tham
cung
không
14/09/2014
29
14/09/2014
hàm.
hàm.
trong
.
nhiên:
tham
tham
dùng tham
dùng tham
vào.
nhiên.
30
Funtions overloading
Qui
int abs(int i);
long labs(long l);
long abs(long l);
double fabs(double d);
double abs(double d);
các hàm trùng
tên.
Ví
14/09/2014
Qui
31
1:
Tìm hàm có
tham
:
tham
phù
Dùng phép ép
14/09/2014
Ví
33
khác nhau
hàm?
Tìm hàm
14/09/2014
:
Các hàm trùng tên
int abs(int i);
C++ cho phép
tái
14/09/2014
(phù
32
2:
34
Toán
phát
new
theo giá
int *x;
Giá
x = new int;
//x = (int*)malloc(sizeof(int));
theo
y = new char[100];
Toán
//y = (char*)malloc(100);
phóng vùng
// free(x);
delete y;
// free(y);
14/09/2014
phát cho
vùng
hàm
xác
chúng
tham
(tham
khi ra
hàm có
thay
.
1:
36
2:
int arrget(int *a, int i) { return a[i]; }
arrget(a, 1) = 1;
// a[1] = 1;
cin >> arrget(a,1); // cin >> a[1];
Ví
3:
void swap1(int x, int y) { int t = x; x = y; y = t; }
int x = 10, *px = &x, &y = x;
*px = 20;
y = 30;
14/09/2014
không thay
14/09/2014
Ví
.
&
tham
Ví
Giá
35
là
hàm
delete
delete x;
Tham
khi ra
.
char *y;
Ký
tham
(tham
void swap2(int *x, int *y) { int *t = x; x = y; y = t; }
void swap3(int &x, int &y) { int t = x; x = y; y = t; }
37
14/09/2014
38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Comparing pass-by-value and pass-by-reference
// with references.
Notice the
#include <iostream>
indicating
using namespace std;
& operator,
pass-by-
reference.
int squareByValue( int );
// function prototype
void squareByReference( int & ); // function prototype
int main(){
int x = 2, z = 4;
// demonstrate squareByValue
cout << "x = " << x << " before squareByValue\n";
cout << "Value returned by squareByValue: "
<< squareByValue( x ) << endl;
cout << "x = " << x << " after squareByValue\n" << endl;
14/09/2014
1
2
3
4
5
6
7
8
9
10
11
12
13
39
15
16
17
18
19
20
21
22
23
24
25
26
27
28
14/09/2014
1
2
3
4
5
6
7
8
9
10
// References must be initialized.
#include <iostream>
using std::cout;
using std::endl;
y declared as a reference to x.
int main(){
int x = 3;
int &y = x;
cout << "x = " << x << endl << "y = " << y << endl;
y = 7;
cout << "x = " << x << endl << "y = " << y << endl;
return 0; // indicates successful termination x = 3
y = 3
} // end main
x = 7
// demonstrate squareByReference
Changes
but
cout << "z = " << z << " before squareByReference"
<< number,
endl;
original parameter (x)
squareByReference( z );
cout << "z = " << z << " after squareByReference"
endl;
is not <<
modified.
return 0; // indicates successful termination
} // end main
Changes numberRef,
// squareByValue multiplies number by itself,an
stores
aliasthe
for the original
// result in number and returns the new value
of
number
parameter. Thus, z is
int squareByValue( int number ) {
changed.
return number *= number; // caller's argument not modified
} // end function squareByValue
void squareByReference( int &numberRef ) {
numberRef *= numberRef; // caller's argument modified
} // end function squareByReference
#include <iostream>
using namespace std;
Uninitialized reference
int main(){
– compiler error.
int x = 3;
int &y;
cout << "x = " << x << endl << "y = " << y << endl;
y = 7;
cout << "x = " << x << endl << "y = " << y << endl;
return 0; // indicates successful termination
}
Borland C++ command-line compiler error message:
Error E2304 Fig03_22.cpp 11: Reference variable 'y' must be
initialized in function main()
Microsoft Visual C++ compiler error message:
D:\cpphtp4_examples\ch03\Fig03_22.cpp(11) : error C2530: 'y' :
references must be initialized
y = 7
14/09/2014
40
41
14/09/2014
42
Hàm inline hay còn
là hàm
.
Ví
:
khóa inline
Yêu
trình biên
copy code vào trong
trình thay vì
gian
kích
nên
14/09/2014
Tìm
hàm:
thi
mã
trình
thi
inline khi hàm có kích
43
sai cho các khai báo prototype hàm
(các hàm này trong cùng
trình):
int func1 (int);
float func1 (int);
int func1 (float);
void func1 (int = 0, int);
void func2 (int, int = 0);
void func2 (int);
void func2 (float);
45
14/09/2014
Cho
44
trình sau:
void func (int i, int j = 0 ){
cout << “So nguyen: ” << i << “ ” << j << endl;
}
void func (float i = 0, float j = 0){
cout << “So thuc:” << i << “ ” << j <
}
void main(){
int i = 1, j = 2;
float f = 1.5, g = 2.5;
func();
func(i);
func(f);
func(i, j);
func(f, g);
}
46
a.
trình
phân
và
rút
b.
vào
trình
tích,
.
b.
vào hai phân .
chúng
47
Cho
danh sách
thông tin
và tên
10 ký
-
nguyên)
nguyên)
-
Hãy
lãnh
nguyên, trong
các công
a.Tính
lãnh =
+
)
sau:
lãnh tháng
b.In danh sách
c.
:
là 20 ký
- Phòng ban
-
công ty, thông tin
là 8 ký
- Mã nhân viên
-
các nhân viên trong
nhân viên trong công ty.
nhân viên có
nhân viên có
d.In danh sách các nhân viên
.
>= 1200000.
theo phòng ban,
phòng ban trùng nhau thì
theo mã nhân viên.
14/09/2014
trình
và
Tìm ngày
vào hai phân
và
.
tìm phân
Tính
và
a.
.
trình
c.
phân
49
trình
trung bình và
vào
ngày.
.
tên,
toán,
sinh. Tính
.
48
1
2
3
4
Khoa Công
5
9/14/2014
tiêu
2
trình:
ra
và
cách có
ngày càng cao:
Unix
công
MS Windows
4M dòng
hàng
dòng
dùng ngày càng
là
thông minh
luôn
…
14/09/2014
3
14/09/2014
4
soát chi phí:
(mã
:
Chi phí phát
Chi phí
trì
coi là chính xác
pháp chính là
Có giao
(tái
Tính module hóa
:
chi phí và
Không yêu
gian phát
14/09/2014
Tính tái
thành
5
(reusability):
có
các
trong
khác nhau
Tính
(extensibility)
Tính
(flexibility):
Có
hay tính
Các thay
toàn
thay
khi
trong
trình
Nâng cao
14/09/2014
rõ ràng
dàng thay
.
14/09/2014
6
hóa
các
pháp
trình:
trình không có
trình có
khi thêm
trúc
trúc
trình
trình
không làm
7
14/09/2014
8
Là
pháp
Các ngôn
Ví
tiên:
10 k =1
20 gosub 100
30 if y > 120 goto 60
40 k = k+1
50 goto 20
60 print k, y
70 stop
100 y = 3*k*k + 7*k-3
110 return
Assembly, Basic
các
toàn
GOTO
?
Khó
khó
trì,
Không
kém, Chi phí cao
phát
các
không
14/09/2014
9
thành các
:
14/09/2014
10
trình con (hay các
module)
các
trúc: for, do, while, if
then else...
trình con
hay
lý
nhóm công
công
Các ngôn
trong toàn
các
?
trình con này
thành các
: Pascal, C,...
trình là
.
trình con
có
chia
trình
.
module hóa, do
trì
dàng
14/09/2014
có
11
14/09/2014
ra các
12
Ví
:
và mã
struct Date {
lý là tách
trình
int year, mon, day;
Khi thay
};
trúc
trúc
toán
thay
theo
//...
Khó
void print_date(Date d) {
tính
Không
printf(“%d / %d / %d\n”, d.day, d.mon, d.year);
hay
Không mô
phóng
trung
trong
}
14/09/2014
13
Trong
14/09/2014
chung quanh chúng ta là
là các
có
(class)
quan
và
nhau.
Ví
14
: Các phòng trong
(object)
công ty
trình
(Object Oriented
Programming –
Là
pháp
xây
14/09/2014
trình
xây
làm
trình.
15
14/09/2014
16
(object)
(object):
Trong
(class)
là
:
…
trong
trong
và có ý
.
giúp
trên
máy tính
bao
thao tác (hành
14/09/2014
17
Ví
:
tính: tên,
nói,
…
và
14/09/2014
tính và
.
18
(class):
…
Các hành
:
14/09/2014
:
có các
màu
2 thành
Các
có các
gom chung thành
các
(hành vi, thao tác).
tính
tính (Attribute):
có giá
cho
trong
.
thành
Thao tác (Operation):
tác
qua
chính nó.
19
14/09/2014
nhau
.
tính, và các hành
hành vi
các
khác
20
Class A
thao tác trên
cài
khác nhau.
là
Cùng
cài
(method).
Member methods
Public:
Constructor
Destructor
là
Other
public methods
.
Private:
methods
14/09/2014
21
Ta dùng
.
mô
(
.
TB
trong
3
Other
public methods
22
Mã
hình
Destructor
14/09/2014
và
các
Message passing
Member methods
Public:
Constructor
Private:
methods
các
bao
mô
Private:
data members
Private:
data members
có
áp
cho
khác nhau,
thao tác
là có tính
hình (polymorphism).
(instance)
Class B
)
1984
0610234T
9.2
:
tên
14/09/2014
2 mô
các
tính
3 mô
các thao tác
các
trong
23
14/09/2014
24
hóa
và các
Các
- Classes
liên quan.
gói - Encapsulation
Chia
ra thành các
.
có các tính
hành
Các
chuyên
- Inheritance
và
hình - Polymorphism
.
có
ra
.
14/09/2014
25
hàm
14/09/2014
26
Cách nhìn khái quát hóa
các
có chung các
quan tâm (và
qua
14/09/2014
27
14/09/2014
chi
không
.
28
gói: Nhóm
gì có liên quan
nhau vào làm
sau này có
Che
dùng
cái tên
thông tin:
thông tin và chi
gói
che
cài
bên
ngoài không nhìn
Các hàm/
Che
Che
gói
các câu
Các
gì mà
dùng không
.
gì mà mình
bí
.
gói
chúng và
có liên quan
các
14/09/2014
29
Là
cho phép
tính và thao tác
D có
C,
các
quan
30
Là
tính
cho phép
tên thao tác
tính có
D.
và thao tác
Cho phép cài
các
14/09/2014
và có
các
có
trong các
:
cài
khác nhau
.
hóa (“là”)
Khái quát hóa
14/09/2014
31
14/09/2014
32
Nguyên lý
: tránh
tái
Nguyên lý
gói – che
.
trình
.
thông tin:
trình an toàn không
thay
chia thành các
Các
trúc
trình khác
sao cho
.
nâng
Các hàm thao tác trên các vùng
Mô
trúc
.
.
14/09/2014
33
gói
không cho phép các hàm
do.
Các
tác
nhau qua các hàm.
Có
dàng
vào
và trao
sung
nào
khi
trình
lên (bottom-up).
14/09/2014
che
lai truy
và
thông tin
và các hàm
.
theo cách
35
14/09/2014
34
OOM (Object Oriented Methodology):
pháp
OOA (Object Oriented Analysis): Phân tích
.
OOD: Object Oriented Design
.
OOP: Object Oriented Programming
.
Inheritance:
Polymorphism:
hình
Encapsulation: Tính
gói.
14/09/2014
36
Cung
trình
.
Cung
soát truy
K
a hình
14/09/2014
37
14/09/2014
Khoa Công
38