Professional Documents
Culture Documents
03 Huongdoituong
03 Huongdoituong
NI DUNG TRNH BY
Mt s ngn ng lp trnh hng i tng ph bin hin nay.
Gii thiu ngn ng lp trnh C++
4/17/2012
Phn I:
GII THIU V MT S NGN NG LP TRNH HNG I TNG PH BIN HIN NAY
4/17/2012
4/17/2012
Bng so snh cc ngn ng thng dng trong lp trnh hng i tng [7]
Eiffel Smalltalk Ruby Java C# C++ Lai (Hybrid) / a m hnh (MultiParadigm) Python Perl Visual Basic H tr mt phn (Partial Suppor) Tnh hng i tng (Object Orientation) Loi h thng tnh (Static Typing) / ng (Dynamic Typing) LpGeneric Classes Thun tu (Pure) Thun tu (Pure) Thun tu (Pure) Lai ghp (Hybrid) Lai ghp (Hybrid) Lai ghp (Hybrid) Add-On / Lai ghp (Hybrid)
Static
Dynamic
Dynamic
Static
Static
Static
Dynamic
Dynamic
Static
Yes
N/A
N/A
No
No
Yes
N/A
N/A
No
a tha k (Multiple)
n tha k (Single)
Multiple
Multiple
Multiple
None
Yes No
No No
Yes No
No Yes
No Yes
No Yes
No No
No No
No No
4/17/2012
Agents (with version 5) Yes (inline agents) Mark and Sweep or Generation al
Blocks
No
No
No
Lambda Expressions
Yes (since 2.1)
Yes
No
No
Yes
No
None
Reference Counting
Reference Counting
Reference Counting
Yes
N/A
Yes
No
No
No
No
No
Yes
No
Yes
Yes
Yes
Yes
Yes
No
No
No
Visual Basic
No
Selective Export.
Name Mangling
None
Public, Private
Yes
No
Add-on
No
No
No
No
No
Implement ationDependent
Implement ationDependent
Yes
Yes
Yes
Libraries
Yes
No
No
4/17/2012
No
No
No
No
Yes
Yes
No
No
No
C, C++, Java
C, C++, Java
C, some C++
C, Assembler
C, C++, Java
C, C++
C (via DCOM)
No
No?
Yes
Yes
Yes
No
No?
Yes (perlsec) 15
No
15
15
N/A
N/A
N/A
11
4/17/2012
10
Yes
Yes
Yes
Yes
Yes
Yes
No
Yes?
Yes?
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes?
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes?
No No Yes
No No Yes
No No No
Yes No Yes
No No No
4/17/2012
11
PHN II: GII THIU NGN NG LP TRNH C++ BNG CC CHNG TRNH MU
4/17/2012
12
Ch thch Cc kiu d liu Kim tra kiu, i kiu Cnh bo ca trnh bin dch Phm vi v khai bo Hng Qun l b nh Tham chiu
Lp trnh hng i tng 14
Hng (Const)
Khai bo hng i vi:
Cc i tng m ta khng nh sa i const double PI = 3.14; const Date openDate(18,8,2003); Cc tham s ca hm m ta khng nh cho hm sa i void printHeight(const LargeObj &LO) { cout << LO.height; } Cc hm thnh vin khng thay i i tng ch int Date::getDay() const { return day; }
4/17/2012 Lp trnh hng i tng 16
4/17/2012
17
Chng trnh mu s 1
// Hello world in C++
#include <iostream> using namespace std;
int main() { int i; double x; cout << "\nEnter a double: "; cin >> x; cout << "Enter a positive integer: "; cin >> i; while (i < 1){ cerr << "error i = " << i << endl; cout << "Enter a positive integer: "; cin >> i; }; cout << "i * x = " << i * x << endl; return 0; }
4/17/2012 Lp trnh hng i tng
18
// Hello world in C++ k t // l k t ghi ch trn 1 dng ca C++ #include <iostream> l khai bo th vin. Trong C++ khng cn khai bo ui .h using namespace std; trong mt s trnh bin dch C++, nhng tp tin tiu chun ca C++ c gi trong namespace std. Tuy nhin, c th khng cn cu lnh ny, nu khai bo th vin nh sau: #include <iostream.h> Hu ht cc chng trnh u cung cp khai bo th vin theo kiu c, c ui .h. Khi , khng cn cu lnh using namespace std; cout << "i * x = " << i * x << endl; endl l du hiu xung dng
4/17/2012
19
Tham chiu
Tham chiu (Reference) c th hiu l mt tn khc (alias) ca mt bin.
V d: #include <iostream> void main() { int x; int &y=x; x=3; cout << x << endl; cout << y << endl; cout << &x << endl; cout << &y << endl; } Kt qu in ra: 3 3 0x11ff2307 0x11ff2307
Ngha l y v x thc cht l mt. Ni cch khc y l tn khc ca x. Do , ta ni y l tham chiu ca x. Cu lnh: int &y=x; chnh l khi to gi tr ca y. Nh vy khi khai bo tham chiu bt buc phi khi to gi tr cho n. Trong trng hp ny ton t & c gi l ton t tham chiu, khng phi l ton t a ch.
4/17/2012 Lp trnh hng i tng 20
4/17/2012
21
Chng trnh mu s 2
#include <iostream> using namespace std; void hoanvi(int &, int &); Kt qu v d: int main() { int a,b; a=20; b=30; cout<<Truoc hoan vi a=<<a<< b=<<b; hoanvi(a,b); cout<<Sau hoan vi a=<<a<< b=<<b; } void hoanvi(int &x, int &y) { int t = x; x = y; y = t; }
4/17/2012 Lp trnh hng i tng 22
Overloading
ly gi tr tuyt i ca mt s thng thng s khai bo ba hm khc nhau tng ng vi ba kiu d liu khc nhau v mi khi s dng th phi kim tra kiu d liu ngi dng nhp vo quyt nh s dng hm no cho ph hp. Nu s dng function overloading th ch c mt tn hm duy nht chung cho c ba kiu d liu. Khi hm c gi n, khng cn quan tm ci no s c thc hin v chng trnh s tm hiu tham s truyn cho hm l loi no gi (chn ra) phng n thc hin ng.
4/17/2012 Lp trnh hng i tng 23
Overloading
int abs(int i); long labs(long l); double fabs(double d); int abs(int i); long abs(long l); double abs(double d);
int abs(int i) { return abs(i);} long abs(long l) { return labs(l);} double abs(double d) { return fabs(d);} void test_abs() { int i = abs(10); long l = abs(-10l); double = abs(0.1l); }
4/17/2012
24
Chng trnh mu s 3
#include <iostream> using namespace std; int abs(int); long abs(long); double abs(double); int main() { int a=-100; long b=-100000; double c=-100.15; cout<<abs(a)=<<abs(a)<<endl; cout<<abs(a)=<<abs(a)<<endl; cout<<abs(a)=<<abs(a)<<endl; return 0; } int abs(int i) { return abs(i);} long abs(long l) { return labs(l);} double abs(double d) { return fabs(d);}
4/17/2012 Lp trnh hng i tng 25
4/17/2012
26
Chng trnh mu s 4
#include <iostream> using namespace std; void inc(int &, int) Kt qu v d: int main() {int x = 5, y = 10; inc(x, 10); inc(y); } void inc(int &a, int b = 1) { a = a + b; } x=15 y=11
// x = x + 10 // y = y + 1
4/17/2012
27
Ton t qun l b nh ng
Ton t cp pht b nh ng new int *x; x = new int; // x = (int*)malloc(sizeof(int)); char *y; y = new char[100]; // y = (char*)malloc(100); Ton t gii phng b nh ng delete delete x; // free(x); delete y; // free(y);
4/17/2012 Lp trnh hng i tng 28
[2] http://en.wikipedia.org/wiki/C%2B%2B
[3] http://en.wikipedia.org/wiki/Java_(programming_language) [4] http://en.wikipedia.org/wiki/PHP [5] http://en.wikipedia.org/wiki/Python_(programming_language) [6] http://en.wikipedia.org/wiki/Ruby_(programming_language) [7] http://www.jvoegele.com/software/langcomp.html
4/17/2012
29
4/17/2012
30
CM N THY V CC BN CH THEO DI