Gtrinh oop

Download Gtrinh oop

Post on 28-Nov-2014

328 views

Category:

Education

0 download

Embed Size (px)

DESCRIPTION

Gio trnh lp trnh hng i tng

TRANSCRIPT

<ul><li> 1. I HC NNG TRNG I HC K THUT KHOA CNG NGH THNG TIN - IN T VIN THNG GIO TRNH MN HCLP TRNH HNG I TNG BIN SON: L TH M HNH NNG, 09/2002 </li> <li> 2. Gio trnh mn Lp trnh hng i tng Trang 2 MC LCCHNG 1: GII THIU V LP TRNH HNG I TNG.......................... 5 I. LP TRNH HNG I TNG (OOP) L G ? .............................................. 5 I.1. Lp trnh tuyn tnh ............................................................................................ 5 I.2. Lp trnh cu trc................................................................................................ 5 I.3. S tru tng ha d liu................................................................................... 6 I.4. Lp trnh hng i tng ................................................................................. 6 II. MT S KHI NIM MI TRONG LP TRNH HNG I TNG........... 8 II.1. S ng gi (Encapsulation) .............................................................................. 8 II.2. Tnh k tha (Inheritance) .................................................................................. 9 II.3. Tnh a hnh (Polymorphism) .......................................................................... 10 III. CC NGN NG V VI NG DNG CA OOP............................................ 11CHNG 2: CC M RNG CA C++ ...................................................................... 12 I. LCH S CA C++ ................................................................................................. 12 II. CC M RNG CA C++..................................................................................... 12 II.1. Cc t kha mi ca C++................................................................................. 12 II.2. Cch ghi ch thch ............................................................................................ 12 II.3. Dng nhp/xut chun...................................................................................... 13 II.4. Cch chuyn i kiu d liu ........................................................................... 14 II.5. V tr khai bo bin ........................................................................................... 14 II.6. Cc bin const................................................................................................... 15 II.7. V struct, union v enum.................................................................................. 16 II.8. Ton t nh phm vi ....................................................................................... 16 II.9. Ton t new v delete....................................................................................... 17 II.10. Hm inline ........................................................................................................ 23 II.11. Cc gi tr tham s mc nh ............................................................................ 24 II.12. Php tham chiu ............................................................................................... 25 II.13. Php a nng ha (Overloading) ...................................................................... 29CHNG 3: LP V I TNG .............................................................................. 39 I. DN NHP.............................................................................................................. 39 II. CI T MT KIU DO NGI DNG NH NGHA VI MT STRUCT. 39 III. CI T MT KIU D LIU TRU TNG VI MT LP...................... 41 IV. PHM VI LP V TRUY CP CC THNH VIN LP .................................. 45 V. IU KHIN TRUY CP TI CC THNH VIN ............................................ 47 VI. CC HM TRUY CP V CC HM TIN CH............................................... 48 VII. KHI NG CC I TNG CA LP : CONSTRUCTOR......................... 49 VIII.S DNG DESTRUCTOR..................................................................................... 51 IX. KHI NO CC CONSTRUTOR V DESTRUCTOR C GI ? .................. 53 X. S DNG CC THNH VIN D LIU V CC HM THNH VIN ........ 54 XI. TR V MT THAM CHIU TI MT THNH VIN D LIU PRIVATE.. 57 XII. PHP GN BI TON T SAO CHP THNH VIN MC NH ................. 59 XIII.CC I TNG HNG V CC HMTHNH VIN CONST..................... 60 XIV.LP NH L CC THNH VIN CA CC LP KHC ............................... 64 XV. CC HM V CC LP FRIEND........................................................................ 67Bin son: L Th M Hnh </li> <li> 3. Gio trnh mn Lp trnh hng i tng Trang 3 XVI.CON TR THIS ...................................................................................................... 68 XVII.CC I TNG C CP PHT NG ................................................... 71 XVIII.CC THNH VIN TNH CA LP................................................................. 72CHNG 4: A NNG HA TON T...................................................................... 76 I. DN NHP.............................................................................................................. 76 II. CC NGUYN TC C BN CA A NNG HA TON T ...................... 76 III. CC GII HN CA A NNG HA TON T.............................................. 76 IV. CC HM TON T C TH L CC THNH VIN CA LP HOC KHNG L CC THNH VIN........................................................................... 77 V. A NNG HO CC TON T HAI NGI ....................................................... 80 VI. A NNG HA CC TON T MT NGI ..................................................... 87 VII. A NNG HA MT S TON T C BIT ................................................ 90 VII.1.Ton t [] ............................................................................................................ 91 VII.2.Ton t () ............................................................................................................ 92 VIII.TON T CHUYN I KIU............................................................................ 94 IX. TON T NEW V DELETE................................................................................ 95 IX.1.a nng ha ton t new v delete ton cc........................................................ 96 IX.2.a nng ha ton t new v delete cho mt lp .................................................. 97 X. A NNG HA CC TON T CHN DNG &gt; ...... 98 XI. MT S V D ....................................................................................................... 99 XI.1.Lp String............................................................................................................. 99 XI.2.Lp Date............................................................................................................. 103CHNG 5: TNH K THA ...................................................................................... 107 I. DN NHP............................................................................................................ 107 II. K THA N ..................................................................................................... 107 II.1.Cc lp c s v cc lp dn xut ....................................................................... 107 II.2.Cc thnh vin protected...................................................................................... 109 II.3.p kiu cc con tr lp c s ti cc con tr lp dn xut.................................. 109 II.4.nh ngha li cc thnh vin lp c s trong mt lp dn xut:........................ 113 II.5.Cc lp c s public, protected v private........................................................... 113 II.6.Cc contructor v destructor lp dn xut ........................................................... 113 II.7.Chuyn i ngm nh i tng lp dn xut sang i tng lp c s........... 116 III. A K THA (MULTIPLE INHERITANCE)..................................................... 116 IV. CC LP C S O (VIRTUAL BASE CLASSES) ......................................... 119CHNG 6: TNH A HNH ....................................................................................... 122 I. DN NHP............................................................................................................ 122 II. PHNG THC O (VIRTUAL FUNCTION) .................................................. 122 III. LP TRU TNG (ABSTRACT CLASS) ....................................................... 125 IV. CC THNH VIN O CA MT LP............................................................ 127 IV.1.Ton t o........................................................................................................... 127 IV.2.C constructor v destructor o hay khng? ...................................................... 129CHNG 7: THIT K CHNG TRNH THEO HNG I TNG ......... 132 I. DN NHP............................................................................................................ 132 II. CC GIAI ON PHT TRIN H THNG..................................................... 132 III. CCH TM LP .................................................................................................... 133 IV. CC BC CN THIT THIT K CHNG TRNH............................. 133 V. CC V D ............................................................................................................ 134Bin son: L Th M Hnh </li> <li> 4. Gio trnh mn Lp trnh hng i tng Trang 4CHNG 8: CC DNG NHP/XUT...................................................................... 143 I. DN NHP............................................................................................................ 143 II. CC DNG(STREAMS) ...................................................................................... 143 II.1.Cc file header ca th vin iostream.................................................................. 143 II.2.Cc lp v cc i tng ca dng nhp/xut ..................................................... 144 III. DNG XUT......................................................................................................... 145 III.1.Ton t chn dng .............................................................................................. 145 III.2.Ni cc ton t chn dng v trch dng............................................................ 146 III.3.Xut k t vi hm thnh vin put(); Ni vi nhau hm put() .......................... 147 IV. DNG NHP ......................................................................................................... 148 IV.1.Ton t trch dng:............................................................................................. 148 IV.2.Cc hm thnh vin get() v getline() ................................................................ 149 IV.3.Cc hm thnh vin khc ca istream ................................................................ 151 IV.4.Nhp/xut kiu an ton....................................................................................... 151 V. NHP/XUT KHNG NH DNG VI READ(),GCOUNT() V WRITE() 151 VI. DNG NHP/ XUT FILE .................................................................................. 152 VI.1.Nhp/xut file vn bn ....................................................................................... 154CHNG 9: HM V LP TEMPLATE................................................................... 159 I. CC HM TEMPLATE ........................................................................................ 159 II. CC LP TEMPLATE........................................................................................... 161Bin son: L Th M Hnh </li> <li> 5. Gio trnh mn Lp trnh hng i tng Trang 5CHNG 1 GII THIU V LP TRNH HNG I TNGI. LP TRNH HNG I TNG (OOP) L G ? Lp trnh hng i tng (Object-Oriented Programming, vit tt l OOP) l mt phng php mitrn bc ng tin ha ca vic lp trnh my tnh, nhm lm cho chng trnh tr nn linh hot, tin cyv d pht trin. Tuy nhin hiu c OOP l g, chng ta hy bt u t lch s ca qu trnh lp trnh xem xt OOP tin ha nh th no. I.1. Lp trnh tuyn tnh My tnh u tin c lp trnh bng m nh phn, s dng cc cng tt c kh np chng trnh.Cng vi s xut hin ca cc thit b lu tr ln v b nh my tnh c dung lng ln nn cc ngn nglp trnh cp cao u tin c a vo s dng . Thay v phi suy ngh trn mt dy cc bit v byte, lptrnh vin c th vit mt lot lnh gn vi ting Anh v sau chng trnh dch thnh ngn ng my. Cc ngn ng lp trnh cp cao u tin c thit k lp cc chng trnh lm cc cng vic tngi n gin nh tnh ton. Cc chng trnh ban u ch yu lin quan n tnh ton v khng i hi gnhiu ngn ng lp trnh. Hn na phn ln cc chng trnh ny tng i ngn, thng t hn 100 dng. Khi kh nng ca my tnh tng ln th kh nng trin khai cc chng trnh phc tp hn cng tngln. Cc ngn ng lp trnh ngy trc khng cn thch hp i vi vic lp trnh i hi cao hn. Ccphng tin cn thit s dng li cc phn m chng trnh vit hu nh khng c trong ngn ng lptrnh tuyn tnh. Tht ra, mt on lnh thng phi c chp lp li mi khi chng ta dng trong nhiuchng trnh do chng trnh di dng, logic ca chng trnh kh hiu. Chng trnh c iu khin nhy n nhiu ch m thng khng c s gii thch r rng, lm th no chng trnh n ch cnthit hoc ti sao nh vy. Ngn ng lp trnh tuyn tnh khng c kh nng kim sot phm vi nhn thy ca cc d liu. Mi dliu trong chng trnh u l d liu ton cc ngha l chng c th b sa i bt k phn no cachng trnh. Vic d tm cc thay i khng mong mun ca cc phn t d liu trong mt dy m lnhdi v vng vo tng lm cho cc lp trnh vin rt mt thi gian. I.2. Lp trnh cu trc R rng l cc ngn ng mi vi cc tnh nng mi cn phi c pht trin c th to ra cc ngdng tinh vi hn. Vo cui cc nm trong 1960 v 1970, ngn ng lp trnh c cu trc ra i. Cc chngtrnh c cu trc c t chc theo cc cng vic m chng thc hin. V bn cht, chng trnh chia nh thnh cc chng trnh con ring r (cn gi l hm hay th tc)thc hin cc cng vic ri rc trong qu trnh ln hn, phc tp hn. Cc hm ny c gi cng c lpvi nhau cng nhiu cng tt, mi hm c d liu v logic ring.Thng tin c chuyn giao gia cc hmthng qua cc tham s, cc hm c th c cc bin cc b m khng mt ai nm bn ngoi phm vi ca hmli c th truy xut c chng. Nh vy, cc hm c th c xem l cc chng trnh con c t chungvi nhau xy dng nn mt ng dng. Mc tiu l lm sao cho vic trin khai cc phn mm d dng hn i vi cc lp trnh vin m vn cithin c tnh tin cy v d bo qun chng trnh. Mt chng trnh c cu trc c hnh...</li></ul>