Sunteți pe pagina 1din 38

Infrouucere Nof, ,l C÷÷

1 lnLtoducete
2 NoL
,
3
4
,
i C÷÷
2 ¸38
Infrouucere Nof, ,l C÷÷
,
daLelot
ptobleme?
¨teinvenLatea toL
,
ii¨
, ,
i ptoceduti
,
i ptocedutile ce acL
,
asupta acesLot daLe sunL LtaLaLe ca un obiecL
3 ¸38
Infrouucere Nof, ,l C÷÷
,
Le catacLetisLicile absLtacLe ale unui obiecL
,
i ÷ meLode
obiecL: o instant
,
a unei clase
4 ¸38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass Cut 
2 stzing cc1cz,
8 µub|ic:
4 vcid Mecu´)  ccut  "Mecu1 Mecu1", 
6 ,
6
7 int æuin´)
8 
0 Cut 1cæ,
ì0 1cæ.Mecu´),
ìì return 0,
ì2 
5 ¸38
Infrouucere Nof, ,l C÷÷
C÷÷
dezvolLaL de Bjatne SLtousLtup
numiL iniL
,
ial ¨C cu clase¨
÷÷
clase
LtansmiLete ptin tefetinL
,
eLc
6 ¸38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì =inc1ude icstzeuæ
2
8 using namesµace std,
4
6 vcid inc´int ax)
6 
7 x++,
8 
0
ì0 int æuin´)
ìì 
ì2 int x = 0,
ì8 inc´x),
ì4 ccut  x  end1,
ì6 int ay = x, ,, o rcìcriaì

ì6 inc´y),
ì7 ccut  x  " "  y  end1,
ì8 ccut  ax  " "  ay  end1,
ì0 return 0,
20 
7 ¸38
Infrouucere Nof, ,l C÷÷
Declarat
,
ii s
, ,
ii
,
ie) = declataL
, ,
ie
, , ,
iei clasei
best µractice: declataL
, ,
iet headet, sepataLe de
implemenLate
8 ¸38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì Cut.n:
2 ...
8 c|ass Cut 
4 stzing cc1cz,
6 µub|ic:
6 vcid Mecu´), ,, uoar ucc|araì

ia acìouci
7 ,
8
0 Cut.cpp:
ì0 ...
ìì =inc1ude "Cut.n"
ì2 ...
ì8 vcid Cut::Mecu´)

ia acìouci
ì4 
ì6 ccut  "Mecu1 Mecu1",
ì6 
9 ¸38
Infrouucere Nof, ,l C÷÷
membtii ptivaL
,
i: accesaL
,
i doat din inLetiotul clasei
membtii publici: accesaL
,
impliciL: ptivaLe
,
,
i accesatea lot - set s
,
i get
struct s
,
i c|ass
10 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass Cut 
2 µrivate: ,, acccs iap|iciì
8 stzing cc1cz,
4 µub|ic:
6 stzing 0etCc1cz´)  return cc1cz, 
6 vcid 8etCc1cz´stzing c1)  cc1cz = c1, 
7 ,
8
0 int æuin´)
ì0 
ìì Cut 1cæ,
ì2 1cæ.cc1cz = "b1ue", ,, croarc: Caì::co|or csìc privaì
ì8 1cæ.8etCc1cz´"b1ue und unite"), ,, o|
ì4 ccut  1cæ.0etCc1cz´),
ì6 return 0,
ì6 
11 ¸ 38
Infrouucere Nof, ,l C÷÷
Constructor. Destructor. this
consLtucLot: iniL
,
ializatea membtilot unei clase, alocate memotie
desLtucLot: elibetatea memotiei alocaLe penLtu obiecL.
consLtucLot s
,
i desLtucLot impliciL
,
i
new s
,
i de|ete
tbis - poinLet la obiecLul cutenL
12 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass Cut 
2 stzing cc1cz,
8 µub|ic:
4 Cut´stzing c1)  tbis-cc1cz = c1,  ,,poiaìcr |a oLiccìu| curcaì
6 Cut´)  ccut  "uestzucting u Cut", 
6 ,
7
8 int æuin´)
0 
ì0 Cut 8czutcny, ,, croarc: coasìrucìoru| iap|iciì au aai csìc siaìcìizaì
ìì Cut 1cæ´"b1ue"), ,, o|
ì2 Cut -8y1vestez = new Cut´"b1uck und unite"),
ì8 ,, a|ocarc acaoric s

ì4 de|ete 8y1vestez,

ì6 return 0,
ì6 
ì7
13 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì =inc1ude icstzeuæ
2
8 using namesµace std,
4
6 int æuin´)
6 
7 int n,
8 cin  n,
0 int u|n|,
ì0 ccut  sizeof´u)  end1,
ìì int -b = new int|666|,
ì2 ccut  sizeof´b)  end1,
ì8 de|ete|| b, ,, !NU "uc|cìc L"
ì4 return 0,
ì6 
14 ¸ 38
Infrouucere Nof, ,l C÷÷
,
iilor s
,
i oµeratorilor
C44
,
ii cu acelas
,
i nume
funcL
,
iile ptin lisLa de patameLti: Lipul lot,
funcL
,
iile doat ptin Lipul teLutnaL
consLtucLotii s
, ,
C44 s
, ,
i
15 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass Cut 
2 µub|ic:
8 vcid Mecu´),
4 vcid Mecu´int),
6 vcid Mecu´stzing),
6 int Mecu´stzing), ,, iacorccì!
7 ,
8
0 vcid Cut::Mecu´)  ccut  "Mecu1 Mecu1", 
ì0
ìì vcid Cut::Mecu´int cnt)
ì2  for ´int i = 0, i  cnt, i++) ccut  "Mecu1", 
ì8
ì4 vcid Cut::Mecu´stzing 1ine)  ccut  1ine, 
ì6
ì6 int æuin´)
ì7 
ì8 Cut 1cæ,
ì0 1cæ.Mecu´), 1cæ.Mecu´8), 1cæ.Mecu´"8uzk1"),
20 return 0,
2ì 
16 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass Ccæp1exNuæbez 
2 t1cut ze, iæ,
8 µub|ic:
4 ,, va|ori ucìau|ì pì paraacìri:
6 Ccæp1exNuæbez´t1cut z = 0, t1cut i = 0) : ze´z), iæ´i)  
6 vcid Þzint´)  ccut  ze  " + "  iæ  "i", 
7 Ccæp1exNuæbez oµerator+´ccnst Ccæp1exNuæbeza cn),
8 ,
0
ì0 Ccæp1exNuæbez Ccæp1exNuæbez::oµerator+´ccnst Ccæp1exNuæbeza c) 
ìì Ccæp1exNuæbez teæp,
ì2 teæp.ze = tbis-ze + c.ze, teæp.iæ = tbis-iæ + c.iæ,
ì8 return teæp,
ì4 
ì6
ì6 int æuin´) 
ì7 Ccæp1exNuæbez u, b´ì), c´6, 6),
ì8 u = u + b, u = u + c,
ì0 u.Þzint´), ,, 6 ÷ 6i
20 return 0,
2ì 
17 ¸ 38
Infrouucere Nof, ,l C÷÷
absLtacLizatea daLelot
mos
,
Lenitea
18 ¸ 38
Infrouucere Nof, ,l C÷÷
´
lncaµsularea
tesLt`angetea accesului la implemenLate
independenL
,
a de implemenLate
uLilizate set s
,
i get
19 ¸ 38
Infrouucere Nof, ,l C÷÷
Abstractizarea datelor
complex
catacLetisLicile esenL
,
iale ale obiecLului
20 ¸ 38
Infrouucere Nof, ,l C÷÷
Mos
,
tenirea
noua
,
Lenes
,
Le)
µrotected: accesul petmis claselot detivaLe
is-a vs bas-a
21 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass |niæu1 
2 µrotected:
8 stzing cc1cz,
4 
6
6 c|ass Cut : µub|ic |niæu1 
7 ,, Caì a aos

ìcaiì uc |a Aaiaa| propricìaìca uc a avca o cu|oarc
8 µub|ic:
0 Cut´stzing c1) : cc1cz´c1)  ,
ì0

ìì 
22 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass |niæu1 
2 µrotected:
8 stzing cc1cz,
4 µub|ic:
6 |niæu1´stzing c1) : cc1cz´c1)  ,
6 ,
7
8 c|ass Cut : µub|ic |niæu1 
0 stzing bzeed,
ì0 µub|ic:
ìì Cut´stzing c1, stzing bd) : |niæu1´c1)
ì2
ì8
ì4 
ì6 bzeed = bd,
ì6 ,
ì7 ,
23 ¸ 38
Infrouucere Nof, ,l C÷÷
Suµrascrierea metodelor
ascundetea tutoror meLodelot cu acelas
,
24 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass |niæu1 
2 µub|ic:
8 vcid Mcve´)  ccut  "Mcved", 
4 vcid Mcve´int x)  ccut  "Mcved x", 
6 ,
6
7 c|ass Cut : µub|ic |niæu1 
8 µub|ic:
0 vcid Mcve´)  ccut  "Cut æcved", 
ì0 ,
ìì
ì2 int æuin´)
ì8 
ì4 Cut 1cæ,
ì6 1cæ.Mcve´),
ì6 1cæ.Mcve´ì), ,, croarc: ìuacì

ì7 1cæ.|niæu1::Mcve´ì),
ì8 return 0,
ì0 
25 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass |niæu1 
2 µub|ic:
8 vcid 8peuk´)  ccut  "1'æ u genezic uniæu1", 
4 ,
6
6 c|ass Cut : µub|ic |niæu1 
7 µub|ic: vcid 8peuk´)  ccut  "Mecu1 Mecu1", 
8 ,
0
ì0 c|ass ucg : µub|ic |niæu1 
ìì µub|ic: vcid 8peuk´)  ccut  "8uzk1 8uzk1", 
ì2 ,
ì8
ì4 int æuin´)
ì6 
ì6 |niæu1 -C, -u,
ì7 C = new Cut, u = new ucg,
ì8 C-8peuk´), ,, "I'a a gcacric aaiaa|"
ì0 u-8peuk´), ,, "I'a a gcacric aaiaa|"
20 de|ete C, de|ete u, return 0,
2ì 
26 ¸ 38
Infrouucere Nof, ,l C÷÷
Funct
,
ii virtuale
funcL
, ,
Le la runtime
dynamic binding
o funcL
, ,
ul de
mos
,
Leniti
mecanismul: virtua| tab|e
27 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass |niæu1 
2 µub|ic:
8 virtua| vcid 8peuk´)  ccut  "1'æ u genezic uniæu1", 
4 ,
6
6 c|ass Cut : µub|ic |niæu1 
7 µub|ic: vcid 8peuk´)  ccut  "Mecu1 Mecu1", 
8 ,
0
ì0 c|ass ucg : µub|ic |niæu1 
ìì µub|ic: vcid 8peuk´)  ccut  "8uzk1 8uzk1", 
ì2 ,
ì8
ì4 int æuin´)
ì6 
ì6 |niæu1 -C, -u,
ì7 C = new Cut, u = new ucg,
ì8 C-8peuk´), ,, "Mcow! Mcow!"
ì0 u-8peuk´), ,, "Bar|! Bar|!"
20 de|ete C, de|ete u, return 0,
2ì 
28 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass | 
2 µub|ic:
8 |´)  ccut  "|::ccnstzuctcz"  end1, 
4 virtua| |´)  ccut  "|::destzuctcz"  end1, 
6 ,
6
7 c|ass 8 : µub|ic | 
8 µub|ic:
0 8´)  ccut  "8::ccnstzuctcz"  end1, 
ì0 8´)  ccut  "8::destzuctcz"  end1, 
ìì ,
ì2
ì8 c|ass C : µub|ic 8 
ì4 µub|ic:
ì6 C´)  ccut  "C::ccnstzuctcz"  end1, 
ì6 C´)  ccut  "C::destzuctcz"  end1, 
ì7 ,
ì8
ì0 int æuin´) 
20 8 -c = new C´), de|ete c,
2ì return 0, 
22
29 ¸ 38
Infrouucere Nof, ,l C÷÷
Funct
,
ii µur virtuale s
,
i interfet
,
e
,
,
ia
inLetfaL
, ,
ii put vitLuale - abstractizarea date|or
30 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass |niæu1 
2 µub|ic: virtua| vcid 8peuk´) = 0,
8 ,
4
6 c|ass Cut : µub|ic |niæu1 
6 µub|ic: vcid 8peuk´)  ccut  "Mecu1 Mecu1", 
7 ,
8
0 c|ass ucg : µub|ic |niæu1 
ì0 µub|ic: vcid 8peuk´)  ccut  "8uzk1 8uzk1", 
ìì ,
ì2
ì8 int æuin´)
ì4 
ì6 |niæu1 -|, -C, -u,
ì6 | = new |niæu1, ,, croarc!
ì7 C = new Cut, u = new ucg,
ì8 C-8peuk´), ,, "Mcow! Mcow!"
ì0 u-8peuk´), ,, "Bar|! Bar|!"
20 de|ete C, de|ete u, return 0,
2ì 
31 ¸ 38
Infrouucere Nof, ,l C÷÷
polimotf = mai mulLe fotme
,
genetice
32 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass |niæu1 
2 µub|ic: virtua| vcid 8peuk´) = 0,
8 
4
6 c|ass Cut : µub|ic |niæu1 
6 µub|ic: vcid 8peuk´)  ccut  "Mecu1 Mecu1", 
7 
8
0 c|ass ucg : µub|ic |niæu1 
ì0 µub|ic: vcid 8peuk´)  ccut  "8uzk1 8uzk1", 
ìì 
ì2
ì8 int æuin´)
ì4 
ì6 |niæu1 - uniæu1s|2|,
ì6 uniæu1s|0| = new Cut´), uniæu1s|ì| = new ucg´),
ì7 for ´int i = 0, i  2, i++) uniæu1s|i|-8peuk´),
ì8 for ´int i = 0, i  2, i++) de|ete uniæu1s|i|,
ì0 return 0,
20 
33 ¸ 38
Infrouucere Nof, ,l C÷÷
Membri statici s
,
i constant
,
i
Membti sLaLici:
apatL
,
in c|asei, s
,
i nu obiectu|ui
meLodele sLaLice poL accesa doat membti sLaLici
, ,
Membti consLanL
,
i:
vatiabile membte: iniL
,
ializaLe de consLtucLot
meLode const
Membti static const
,
ializaL
,
34 ¸ 38
Infrouucere Nof, ,l C÷÷
Lxemµlu
ì c|ass Cut 
2 µrivate:
8 stzing cc1cz,
4 µub|ic:
6 stutic int Nz0tCuts, ,, csìc uoar o ucc|araì

ic
6 Cut´stzing c1)  cc1cz = c1, Nz0tCuts++, 
7 ,
8
0 int Cut::Nz0tCuts = 0, ,, variaLi|c|c acaLrc sìaìicc ìrcLuic s

ì0
ìì int æuin´)
ì2 
ì8 Cut 1cæ´"b1ue"), 8y1vestez´"b1uck"),
ì4 ccut  Cut::Nz0tCuts,
ì6 return 0,
ì6 
35 ¸ 38
Infrouucere Nof, ,l C÷÷
Alte not
,
iuni (TODOs}
mos
, ,
copy-consLtucLot
vitLual Lable
LemplaLe-uti
STL
sLteam-uti
namespace
36 ¸ 38
Infrouucere Nof, ,l C÷÷
Cuvinte cheie
consLtucLot, desLtucLot
nivel de acces
encapsulate
absLtacLizatea daLelot
mos
,
Lenite
37 ¸ 38
Infrouucere Nof, ,l C÷÷
Referint
,
e recomandate
TuLotial C÷÷: nttp:))neudutu.Lcx.sk)Lx)c)index.ntæ
RefetinL
,
e: nttp:))uuu.cp1usp1us.ccæ
C÷÷ in a NuLshell - Ray Lischnet
38 ¸ 38

S-ar putea să vă placă și