Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
What to Upload to SlideShare
What to Upload to SlideShare
Loading in …3
×
1 of 8

Η ΓΛΩΣΣΑ C++ - ΜΑΘΗΜΑ 2 - ΕΙΣΑΓΩΓΗ ΣΤΙΣ ΚΛΑΣΕΙΣ (4 διαφ)

0

Share

Download to read offline

Α. Θεωρία
1. Κλάσεις
1.1 Γενικά
1.2 Ορισμός Κλάσης
1.3 Δημόσια (public) στοιχεία της κλάσης
1.4 Ιδιωτικά (private) στοιχεία της κλάσης
1.5 Παράδειγμα (προδιαγραφές)
2 Περισσότερα για τις κλάσεις
2.1 Ορισμός Συναρτήσεων έξω από την Κλάση
2.2 Παρουσίαση Ιδιωτικών – Δημόσιων Μέλων μιας κλάσης
2.3 Χωρισμός σε Αρχεία
3. Ειδικές Μεθόδοι Κλάσεων
3.1 Γενικά
3.2 Κατασκευαστής (constructor)
3.3 Καταστροφέας (destructor)
3.4 Ελεγκτές Πρόσβασης (accessors)
B. Ασκήσεις

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Η ΓΛΩΣΣΑ C++ - ΜΑΘΗΜΑ 2 - ΕΙΣΑΓΩΓΗ ΣΤΙΣ ΚΛΑΣΕΙΣ (4 διαφ)

  1. 1. !" # $% " !" #$ % # & '( # ) *+,-" #$ % # . / ( 0 1 $( 0 23 " / # 0 # 45 #6 37 1 # / $5 '( # 8 9 :3 ; % < ( 3 : = ($ # 5 #6 !>?@,) !,>)" # # $23 A-@,) !,>)" & < 0 #3 / B +!!-@@>)@" C 6 &' ( ) *' *' " + ! : D , " - ) (3 . /" - % 0- . + ) " ". /" " $ + %' , $ ! " ) 0 1 )" 2 0 + ) " "' , # $ % . % 0 3/ 13 " 0- $ 2 " - +% +% , - ) -% 3 3 /" + 0" ' , 4 3 3 /" + !" / + ' , 5 )6 0" $ + + + ' , 5 0" / " + / 3 + ' , 5 )6 0" 0 - 7 + E # $ , )" /" # 0 #51$ + , - !" " !" 3 - % 0- + ' , )" + - 0 10- $ 2 " - +% +% ' / ( 0 D , 5 +! )" /" - % 0- . 3/ ' , 5 6% . +! "" . . #)+ )" " + ) " + 1 +! 2 / ( 0 D , 4 ) " )" /" - % 0- . 3/ ' , 8" %- 0 ) " - 0 /$ 6 7) 0 " $ ) 0 )" /" " + ) " + 1 ) " 2 / ( 0 D , 8" $ " % 9%3 0 )" /" - % 0- . 3/ ' , :; <;=> ? " + ) " 9%3 0@ )" /" " + ) " + ;A>BCDE ?+ 9%3 0@ , F( 0 -% " G &' ( ) *' ' 5 % , 5 )6 0 ) + " 9 3 , - ! " H 0 %- %" + / 0 ?<IDAAJKDEC@ , 0 " 3 . + + 0 /" " "/ !- 3 3 /" " , L 3 - /- " )" - " -% EDMK , 5 )6 0 /" " + ) " + 3 , 8" " + ) " )" - / " H 0 !- 0 3 3 /" " - 0 /$ ) + , + /$ " ) $ 0 - 7 H / ?-'$' - ! " ) 0 - " +/ " 3 @ " ! +@@ <IDAAJKDEC N !D OO % H / + 0" ) *+,-D OO P3 +/ H / + 0" QR <IDAAJKDEC ;STC<>JKDECR <IDAAJKDEC ;STC<>JKDEC*. STC<>JKDEC R
  2. 2. &' ( ) *' U' % ?V=SIM<@ $ ) + , 4 3 % $ ) ? / W 0" @ "% " + /" 0 + )" ?- H @ -% - 3 - / 0 - ' , L - % H 0 . + % 0 " + /" 0 + ?-'$' -% EDMK@. )" " ) ?'@. 9 , 3 ! /" - 3 # ;STC<>'ECESCB !"# "$%&'($"&)% *+#)% %,%-&%./ 0"&)123&/ 5 + /" - % )" XX 'V=SIM<'<VV &' ( ) *' Y' P3 + ?VBMZD>C@ $ ) + , 4 3 + $ ) ? / W 0" @ + )" ?- H @ 5[5 -% %3 0 "% " + /" 0 + , - % H / . )" " " " - +/ H / ' , - + )" 0 - 3 $ 4 5 6 7 889: ::: :9 ; ; <= 5 5 + /" - % )" XX 'VBMZD>C'<VV &' ( ) *' ' # 3 ?# 3 7/ @ , ( + + 0 0 ) + %" +! ' , 5 +! /$ %" /" $ + +%. " 3 0. " - ) 3 " - ! " " 9/ 0 + " - / "' , )" 3 + H ' , " " - ) . )" +/ H - ) . , " )" - " -% *]. )" + 3 . , " )" + -% *]. )" + + 3 , ( 6 -% +! " H 6 /" "0 " )" + + + 3 ' , L 0 - ) 0 ^" " - 3 7^" ?H ' 3 9 @ % 4 4 ; 4 6 47 889> 9 889> 6 9 4 4 # D , $ 3%" - " . 3 3 /" + ? H / @ )" 3 + / ' &' ( ) *' ' # 3 ?# 3 7/ @ , 5 +! /$ ) 0" . 3 % . " - ) )" $ + - ) H 3 0' , -) /$ ) 3 % / 3 . , " - ) +! H )6 _ & 4 4 ; 4 4 ; 4 4 ? 889 6988 4 4 ; 4 6 47 889> 9 88@> 6 9 4 / #6 D , 4 - % 7 / . H / ` / + $ / 6 " $ + - ) , a 0 % . )6 /" " 3 +% %- $ + - ) . 0" <;KA>B=<>;B ?+ + 0 / @ , - ) 0 0"/$ 0
  3. 3. &' ( ) *' ' # 3 ?# 3 7/ @ , EDMK " H % 0 0" " " " + /" "' , 0 + + /" 0 - /" " 3 9! 0 +! 0 0 + 0 7 " + ! 0. - ) )" +% 0 , " + )" + $ + - ) H / 0 3 , ? " /$ " ! 0. - /- " /$ b *]@ ' 4 ? ? A 889B ? C 4 D988 4 ? ; 4 88 4 889B ? C ? 6 988 4 ? ? ? ? $0$ c P3 + #)+ #)+ - ) R c #)+ 8$ " ! 0 c P3 + ! 3! 7 / - + ^ c #)+ H. H_ :; + /" - % )" XX '<IDAA'<VV &' ( ) *' ' # 3 ?# 3 7/ @ # , L " - ) " - 3 7^" + - +% " + ". )" - 3! + + " + 3 % , - ) - ) + 7 - " / " 0 0 "' , " +% + % EDMK 0"3 +% + )+ 9! " " + /" " , )" 0$" 6 " - + " ") , " H " % , " - " + ) " ?MKM>MDIMdD>M;K@ , " " - " - + " ") 0 , )" " " " 7 . - ) $ - ) 0 / 0 ? " + ) " @. " 0 - ) ) ' () &' ( ) ' # % *' 5 % 0" " /9 -% " + , 45 = F # $ 3=$($ $ F$ # # # . 3! % 0 , )" 1+ ! 2 " /$ 0 %" / ? H / @ + - % 0- 0" " ? / 3 @ + , ^ " /$ 0 " 7 . $ ) " $ 6 " + 0 - / / +^3 + ' , " $ )6 0 ? + @ 3 -% " 0 - ) 9 $ $ ) , ? 3 ! 0"/$ 0 @ , " ) 0 ^ %3 0 + % + , $ #$ 1 # #51$ 3 # , + % + ? EDMK@ )6 0 ^ 0" , 1$5 = 1 31 1 $ 0 # 1 $ # 1 #$ $ # 5 # #$ $ # G $ $5=$H $ 1 #$ # #6 DD ? -) 0 H/ @ (( BC>=BKJ>eVC ! +@@I?+J-DDEC>f;gJKDEC?DBh=ECK>A@ , i # #6 1 5 B3 ?BCA;I=>M;K A<;VC ;VCBD>;B@ DD /$ - / $ + /$ 0 3 3 3! , $^ J " " " + ) " ?-'$' A>g <;=>@ , + 0" ?-'$' g;h SDBj?@@ &' ( ) ' # % *' 5 % 0" " /9 -% " + , 3 ! " 0 - ) ^ 0" " /9 -% " + ( E 4 8 7 4 4 4 ; 4 4 ? 4 ; 4 4 F 4 4 ; 4 4 ; 4 4 4 ? 889 6G988 4 4 4 ; 4 6 47 889> 9 889> 6 9 5 + /" - % )" XX '<IDAAJEC>f;gA'<VV
  4. 4. &' ( ) ' # % ' # 0 ) P3 +^" W % " / " + , )3 % %- - 0 - 0 6 0 / + %3 0 , )" 7 " - ^ 3 % / ` %3 0 + /- 3 + / ` %3 0 , % 0 % )" 1 3 +%2. 0" )6 -% + ! - / ' , ( - ! " " 0 / -% 3 + 3 % . " % /9 , % 0 % 3 " 0" )6 + )" " +%' (! H F H F F &' ( ) ' # % ' # 0 ) P3 +^" W % " / " + , 8" 0" /" %- 7 " + " )" + +% 0 , 3^ $ + / ` / 3 - 0 7 0 / + . $ ) " /$ 0 ) " - % H . )6 " -% ^ " )" 3 + ' (" H F 8I , - ) 0 ^" " ^ ". /$ - ) * %- - 0 " 7/ ' &' ( ) ' # % U' k % $ ) , L 0" /" - + VB;TC<>A )" 9 , L 3 + )" /" $ ) - /+ 'f ?-'$' <IDAA'f@ , 4 ^ " 0" " )" " /" $ ) - /+ '<VV ?-'$' <IDAA'<VV@ , 4 $/ 0 % + " lMK<I=gC 3 + 3 - + , 4 $ ) 0 % - ) " ). ^ " - $ ) /" "3 $ ) ?-'$' <IDAA';@ ? " + " +% $ ) @ , L EDMK H ) + 9 $ % $ ) . - ) - /$ " " 1H H + 2 + - 0 /$ 0 + + 0 ' , $ + ? 3! + @ (# E 4 J K E 4 J K L'%M0NO &3O .2PM3O E 4 J K QR0)&) &-# 0"SN,-# T L'%M0NO &3O .2PM3O T E 4 J T K QR0)&) &-# 0"SN,-# T E 4 J K E 4 J T K QU#P'&3M3 &' ( ) ' # % U' k % $ ) , 3 ! " $ % $ ) - 3 ($ V 4 V 4 4 ; 4 4 ? 4 ; 4 4 V 4 V E 4 8 7 E 4 J4 K 4 4 4 ; 4 4 ; 4 4 4 ? 889 6G988 4 4 4 ; 4 6 47 889> 9 889> 6 9 V V E 4 8 7 E 4 J4 K 4 4 ? ? A 889B ? C 4 D988 4 ? ; 4 88 4 889B ? C ? 6 988 4 ? ? ? ?
  5. 5. &' ( ) ' # % U' k % $ ) ?;KIMKCmno@ , p /- 0 + - )" 0 - ) - H " 0 ;KIMKCmno , 0 ! /" + " ! VB;TC<> + /- + 0 -) "/ 0 $ ) 0 , 0 ! ) $ ) ?+ 0 / 0 + " ) $ "% @ , + 0 -) qr=Ks ;KIMKCmno + " % - ) "/ , # - 9 ) ?VBCVB;<CAAMKh@ , 5 " / lMK<I=gC " + ) " -% " +^3 + " $ ) " - ) " 7/ " ?+ " ) $ 3 ) 0 - - 9 @ , ^ ?<;EVMIMKh@ , $ ) '<VV )6 %" 0 + - /" "/ $ ) ? 0" - /+ ' @ - ) /$ 7 0 $ ) 0 ^ $ " + / " + " +% $ ) , !"3 ?IMKjMKh@ , 4 " + " + $ ) ?';@ 0"30 6 " /" +% + / $ ) ?'CtC@ (% , ;KIMKCmno 3 " H /- 0 "3 3 + /$ / +% + / ' &' ( ) U' 3 +/ / 3 " *' " + $ 3%" + + )6 0 U + /" %3 " - 0 + " 0" 0" /" ) , 5 # 5 #6 ?<;KA>B=<>;B@ , 8$ % 0 " $ + - + ' , 8$ 3 +% 0" + +%. - ) )6 -% , ) 0 % ' , 5 # # $23 ?gCA>B=<>;B@ , " "/ % " /" " + ) " + /7 , 8$ 3 +% 0" + +%. - ) )6 -% , ) 0 % ' , 5 0 #3 1 B ?D<<CAA;BA@ , / $ 0" " - % H 3 3 /" W / + , !" -% " - ' (& , W)')&/'3M3 X$P'Y"% .)% Z )#&%['PZU , $ZU "$+M3O .)2"+&)% )U&N0)&) .)% (Y"% "%,%.N MU#&).&%.N , .)% S) &Z# 0"2"&/MZU0" M" "$N0"#Z 0PS30) &' ( ) U' 3 +/ / 3 " ' + 0 ?<;KA>B=<>;B@ , L 3 0 + + 0 )" 3 % / + ' , # $ , 8$ 51$% # ( $ $ # ? 0 %" " %- )6 % )" + + 0 @ + ( 3% #H1$ 1 # $26 ?3 " - /7 )- @ , - ! " /$ 0 1$ $H # 5 #3 " )3 + 0- 7% 0 ?<;KA>B=<>;B ;ZCBI;DgMKh@ , $ #$5 % $1$ 6 % / ? H / @ - ) -% " $ 3) ' , L 3 0 - !- 0 )" , L 3 0 ^ )" (' , 5 <;KA>B=<>;B - "% 0- $ + + - 0 7 9 0 -% " , 5 # 5 #6 !>?@,) !,>)" )" 3 % / 3 + . - ) , + ) 0 % % " + + 0 6 /" " + ) " + ' <IDAAJKDEC?DBh=ECK>A@R <IDAAJKDEC <IDAAJKDEC?DBh=ECK>A@R &' ( ) U' 3 +/ / 3 " ' + 0 ?<;KA>B=<>;B@ , ^" 0 " % + q +! s ^ " /$ <;KA>B=<>;B' , # ! % + 3 0 " + /" 0. H 6 0 ) 0 <;KA>B=<>;B' , - ! " ) 0 + <;KA>B=<>;B $ ) ) . , ( + ) + 3 0 " + /" 0 ?3 " - ) - " / @ ) V V E 4 8 7 4 4 4 ? 4 ; 4 4 4 4 ? 889 6G988 4 4 4 ; 4 6 47 889> 9 889> 6 9 889B ? C 4 D988 4 ? ; 4 88 4 889B ? C ? 6 988 4 ? ? ? ?
  6. 6. &' ( ) U' 3 +/ / 3 " U' 7/ ?gCA>B=<>;B@ , L 3 0 + 7/ )" 3 % / + ' , # $ , 8$ 51$% # ( $ $ # 1$5 1 $ # 3% 1 1 3 6 K ? 0 %" " %- )6 % )" + 7/ @. ( 3% #H1$ 1 # $26 ?3 " - /7 )- @ + ( 1 $ # ' , + - ) " /$ #$ 1$ H 3 # # $23 , $ #$5 6 "/ - 0 - /- " )" 0" - " " + ) " + 7 ) , L 0$"% "/ )" " - 0 ^ " - 0 /$ 0 3 ! 30" + ? 3 ! -% " @ , L 3 0 - !- 0 )" , L 3 0 ^ )" ( , 5 # # $23 A-@,) !,>)" )" 3 % / 3 + . - ) , + ) 0 % % " + /7 /" " + ) " + ' u<IDAAJKDEC?@R <IDAAJKDEC u<IDAAJKDEC?@R &' ( ) U' 3 +/ / 3 " U' 7/ ?gCA>B=<>;B@ , # / 0 + /" " ?3 + +%@ + 7/ " + V 4 V E 4 8 7 4 4 4 ; 4 4 ? 4 ; 4 4 4 4 ; 4 4 ; 4 ! ! "#$ 4 4 ? 889 6G988 4 4 4 ; 4 6 47 889> 9 889> 6 9 4 ? A 889B ? C 4 D988 4 ? ; 4 88 4 889B ? C ? 6 988 4 ? ? ? ? &' ( ) U' 3 +/ / 3 " U' 7/ ?gCA>B=<>;B@ , ) - + H ?%- " + ) " - 0 /$ 0 - % @ , ! $^ " + 3 , - 3 ! " $^ " . % " + ^" 0" " - ) /$ 3 )' , 8 gCA>B=<>;B. - 3 . + ) % " + ^" 0" EDMK' ! # % $ %" 6 " " + /" ". 3 ! " 0 + -% " ' &' ( ) U' 3 +/ / 3 " Y' &<<CAA;BA ?AC>>CBA W hC>>CBA@ , 5 +!!-@@>)@ ? + / - % H / + @ , )" 1 $0 # # 6 1 # 6. - 0 / % % / ? H / @ + - /- " )" 3 + ' , " / 0 " 6 0 " "% / 0 . )6 0 ) 0" - 0 , )" 3 % . , - ) " % "/ . + 6 " 0 / 0 ' , /$ " 9 7 ? " / -'$' " 6 t + )" +/ @ , L 0" 0 / @-,,-)' , " / 0 " - ) " 0 " "% / 0 . )6 0 ) 0" - 0 , )" 3 % + - /7 " ' , L 0" 0 / L-,,-)' " 4 ;5 ;5 5 ;5 ;5 5
  7. 7. &' ( ) U' 3 +/ / 3 " Y' &<<CAA;BA ?AC>>CBA W hC>>CBA@ , - + )" 0 " + 1 +! 2 0 D<<CAA;BA H E;;g # E 4 8 7 4 4 4 ; 4 ]4 4 ? 4 ; 4 4 F F F &' ( ) U' 3 +/ / 3 " Y' &<<CAA;BA ?AC>>CBA W hC>>CBA@ , 5 D<<CAA;BA !" - ) - + - + + . 3 % , " - /- " - + " /$ - % H - + 3 3 /" ' , 0" -^ 09 " " - - +% -/" " + - $ ' , )" 30" % " - + . , " 9 " 0 - ) + . , $ ) " - ) +^3 + - 0 3 /$ 7 ) + $ - ) " + ' $ &' ( ) U' 3 +/ / 3 " Y' &<<CAA;BA ?AC>>CBA W hC>>CBA@ ?*' / 3 <;KA>@ , # % hC>>CB + /$ /9 + 3) !>?@, " + 0 ) 3 , " %- 0 % )6 0 % 3=$($ ( 3% ( 1 # 3 # , ( ) % )" + - + - + + " )6 0 ) / 3 <;KA> , % " " )6 0 % 3 " - /- " - / + . , " ) +% 7 + 3 + )3 -/" " - + - 0 - ) " - +!v 0"' , L /9 `+ 3) <;KA> , - /- " + 0 ) % 3 0 - !- 0 %3 0 , % + 3 0 ^ %3 0 % p' + w + * - - ! <;KA>B=<>;BA & + 0 /" - % . - ) " 0 - ) + %" g=EEe - ) , [ /$ +/ H 3 +% / %" t' , [ /$ 3! + + 0 / , /" " $ ) ) . - 0 " $ + - ) H t ]' , /" " % " - 0 - H t , [ /$ D<<CAA;BA , [ /$ gCA>B=<>;B , /" - % " +% "0 . % + /7 " + ) " " - 0 /$ H t' ) 0" EDMK - ) , [ 3 ^" 3! " + ) " g=EEe , 4 /" " $ + - ) " H t *] ? / 0 <;KA>B=<>;B@ , :; " " $ + - ) H t
  8. 8. p' + w + ) ' 5 ) ) + - 0 " - + ")6 /" ) 0 - -/3 0 , ( /$ / 0" /" 0 t. e ?- +/ @ , ( /$ <;KA>B=<>;B $ ) ) - 0 $ + - ) ) ?].]@ , ( /$ <;KA>B=<>;B ) 0" /" 0 "/ 0 ) 0 , ( /$ D<<CAA;BA , ( /$ 3 % 0" + !- - 0 0-^" 0" /" 7 ?t.e@ 5 ) + ) 0" EDMK , L - ) 3 0 ) U ) + ) ' , ( 0-^" " %" ' p' + w + U ) + - + 0 % 0 !) 5 mDKgDIx , /$ +) ]*y ^" ?" )" $ + - ) / <;KA>B=<>;B@ , ! 0 )" $ ^ + ? 0 H . " $ + - ) / <;KA>B=<>;B@ , /$ /" " 3 )+ ) ?" $ + - ) *]] / <;KA>B=<>;B + " /$ D<<CAA;BA@ , - ) " + " 9 9% + , zMBCSDII {VCII ?+ )6 U] -%" 0 ) @ , |Mhf>KMKh {VCII ?+ )6 y] -%" 0 ) @ , - ) " - /" ?}DM>@. - / " *] -%" 0 ) " 3 )+ 0 ~ - /" " - ) mDKgDIx , " /" xMBCSDII AVCII , # /" , " /" IMhf>KMKh AVCII , # /" , # /" , " /" xMBCSDII AVCII + H 0 " ) 0 " 0-^" " %" , 8" "0 + " mDKgDIx , L 0 ) 0

×