Classes and Objects Part1
Classes and Objects Part1
CSTRUOTURES RevsED
The me the
tuntg nt ealkus 77he Clapquaze is
Stuetaud. 7hey provide a mrtho fo Packip
ge1s
data
lilh+-types, Astuel+ine u o Cmvenent
to r andllar agroup f ogiealA latid data
ems à aU usdapnid olatatypè uwits a
templali
That egas daie i t datapop eskre4 .
Slui stuclen
Chau namiloJ
Iht stl-numbu
4loat total_maiks
7he
keynerd tauct leclats luedsn & as neno dala a
S t u c Stolnt A c dcdaialon
cr l u otines au
Suctan Can han auRyL, pointu
membeAs,
C STRU CTURE
LIMITA77ONS O
allns 7hoAtuetdala
dou4 not
7he <tandaseC
b e tienlid tike built-in tyPLu.
Type
Sme Coplex
flont
float y
(2
S u e Complex CL c2, c3
The lomplex embeus C1, c2 ard cs car eanly b
usi'gne d Values ucingthe dat bJpesa tor, but we
(fan
mot add tup
mot ald ttup Ceriplex umsee on Aubtia cE ome 0
the and67heg .
C cL +C2 itlant i
C iuelinus u hat Zhay do
&The impordant- Limitatrirn E/ by t h i
ns Pmit clati hidps. by
accesed th
membeu can /AineetHy
*cAu anyhet n Theui
yart'ables by any fupeerom
STyutlTuw
7he ktuataun ne mu au pullre m m b u e .
Strpe.
ExTENSIONS 70 TRUCT VR ES
membeis as 7hat
Sttetars vaslebles.
The membew
elass as pmvate ,'Sshile by efhult
a Stiuetau au pubrc.
SPECIFVING A CLAss
anoa/ald
and i t
a uway 5 b1nt 7he otath
A clees cdata ler tndrrns)
Ze atoos
nchons tosclhu
.
Sh
6 h ee
n
c l a s s decaralion
clarslepn1tron
Puncier
and seg7
Thetyppe
becifrés
h e Ela44 oleetaatrin defiitron
oles
ebes
howihe
elaks finidrent
eleclaa7ton
it
clarr
pm 7a
The geneal
class c l a i s n a me
pmvat:
Vazjasle drcaintront
hncTion decasat6n
pubhe
Vrlable elechsaons
findiin cleclatotos
The cla cleclaialron r Smila t a Stue chelatat
The Key orrd pllok iu am
class Sperilet thak hat
abst(ate clate 57Tpe clatname. 7ne
body 7a
class Ze enclese d' thin baces and
a Simitsen
tuhinalrsy
icibeli labek.
encapsilatioin.
Class
No
Enley to -
ivai aya
X
Date
hin ctrons
Puslhe aua
enay alned|
Pu6/èosal
Dala
hunctoms
clae km
V a t t a b l e declata-
tren
Int tumber
Publie
afloat )
VerZetdaa
lnt fencth n
declatalvon
auitprrbpe
enold stom
CREAT7NG DBJECIS
w e Can cuate
Leen d e c latud,
claes ha Cdtathi
The Cla
c l a k name
name,
Onu a
Tha dype y f
VaaLly S7
/TEMM
ITEM
class
DATA getdata)
mumbe
COA
putlala0
FUNeTIOnLs
Gldatal
clas
Kepttounh tion 57 a
He /mem ory r u caratd
CLeates a Vatratlo
the claas allables Knnon as
aee jocis
7hetepore called an shyett Type v~m
elass tem
zty
Atm
memst.
Valuts 57 eda7a
wauld duplay 7h
6y 49
Amembe iinctie Can be Invoked
an h y e i .
ho meanin/
getdata Clo, 7Ss)
numbu =/op|/tgal
A
A vailaLe decatud at publie Can be a ecescAd y
Int
Int
P 7 0,/er
pusie P Z = /0 P vals
int 2
y2
6)
DEF INING MEMBER FUnc71ONNC
Kruch
a vey
Theli dfinitrene
Anetenf vnsron 7knehein
the a7d
Since ctt loes n st Support
MUat
muot de
Ae u
us4
eed
he ANS? prtatpe m
ekepnctrein
Tke fonet o n e a d u
o depoig hin Cfrcm an
need
laa
m
meem
mb5e
eii
ftinctron
Letuen a
The AHesen
te
membe Hinctron tiheome.
m e m b e r Aincttó hpa
Thab a
Tha
mamal
finctron
Casel
(asel h e headu.
Ih headi.
mernbNshIp
dentitg
Tdetity
aies a
A
frincthon-name
agument
gumenE
Metun-type
name:'!
cloks_na
etecaialior)
tells
I i he
Ki Compilet
Copilet
lakr-name !
The membeShip label c he
"clas
Thate Junchen Aunttroin-ham beleng Anothe
Awlivsts
Clcname. 1è the ezpe 7 tAN fn ci en u
Aepla ce
heaaln_line.
tthe c laccname specifhid w 7he tencdre
The Sym bot Calleol Zhe S p e Juastuton opuals
a . Float 6)
Void Hm getelale Cin
n u Mbel = a
Ce =6,
" Cea
Cout L<
odo n o t
S e r u n any value thui
Value thui.
Stnee heee fned1one
ueliunype LEhe
hatachuéttô
Lome p eciak
The membel fintcns kaue tui kome tneion
Kanme frinchon
claies Can u tae All
f e v u a l ifesent well
Kedstu thei
lasel
name. The membelship
7he penvali
datn f7hs
Can asers
Memk functiohe, lo,
(( nen-memb-es t n c ro
CannT do
clasf. A
M
meem
m L
L en
1 fn ttha
finutjo7
k1ntran
Can Call ano fhw
an5this
memsei
(3) d o t - Dpelalor.
alyvett
wttheud uenp
INsIDE THE CiAs
DEFINIT7ON
Anoth e melueol 5/ def1n a mem bet Juneh on is ts
Aepla ce the tnction dec?kk atrèn Ay 1 h actual
1nt numbe
Ploat Cos
Pushé:
Y d etdatal inz a, float 6)}/dethsalian
3
IEhen a Auncttón 2 cdan èd tàciue Cleae, i 7 is ditata
aA an hune tuncron. &an
ion and l a m t a r o n s
tha- apply
/trebrons
All the
Kut.
alho apliak
inlne Ainctrm ae
chc
Phog am
incluode Kiostadz
cless l e m
In niurnbe,
l o a t Ci
ub-hc
a,
P)
/lo at c t;
o r y p e lehra
lint
otcdato
va
votd puteala [vor'e)
umLec<<
"
\n"
&n
"neimbe .
C e u t < <
matn
Coute "|n e c t x
"
"\n "
24tdata (lev, 299.4)
ptdala )
getda Ta ( 20no, 1 c o )
Iputdal ();
Metun D
3
NLINe
FUncr1oN
MARING AN OUTS/DE
a u thn
o0P u s ep
yiotws f
One of tne fom the clast otefinitran.
impleme+ation membe
detailt th
bcdefinè
PLacb'ou
good
2 il thiuefer
the clars.
cla
drokste e
hncth one
h e can cdene a menbet tunckèn outi'ple R e c l athe
rs
the
it inrre
n liie by uaui
m a ke
a n d shll
epn tt en Kader
Kradeu lun
luni kf Anbtein
Anbrèn
he
Pualpes nleat_
defnetron.
clas Lm
Pubh
Cn a,
f/oat 6)
Venh gtdal
int a , tloat6)
inltne t d item' 2tda7a l
umbe =a
Cet b,
3
FONCT7OALY
APEST ING Df MEMBER
m e m b e s fiunétien
a7
aotAu. tnetros,
name hsile
sfmembe
as mestnig
i Knoo
class . 7 h i
be called by
membefin etrón
can vnh
pYira
no7hu tncicn thatt 'u a membn 67 tk clas
invske a p t a l fun etron tain
en an
het- Cann&
clas Sampli
nt m /pvalt mums
vtd Kead Cvw'a
PAnet èn
Pubhé
(vo1/)
vo le u jpdat
ample, huy
Cannot
/won t woK 8S c
( v a t e 'memsns,
he
s 11gat,The fineton Mendl) can se callee 7
eunetron cupAat ( tue alut s7 m.
yid sample upda t (votl)
s i m s t e callj
no
rp
terdl )
A CASS
hITHIN
ARRA YS
1h a
vaitabtes
uieel as membe
The an44 Can be valed.
u
clast defnctrén
7he follooin
ela
CemstInt
Sicè
=
lo,
clacs QMaY
3 è
leec
clta
av d
rd
ai
t a p
v ti
a va
é t
Vaiialle
at7
tusued
usied iin the
t he
canbe be
he aAy can
clhs anay
anRy v
Vaau
uans
Lke.
the
m e m b e n b7 any d
&thes
ths v
vnaay
y
memfruncthons
Uke ay
OBTECT
allocatrdsepacatol
p a c e for memset ataslesu
7he
s p a i a t t * m e m ry
locatrónt fr
Freach e c t e m b e vaiiasle
nli'a, beraiel The
hye ett a
ene
enteLhinetren 2
shon
raled
mennW
uncont clefncod
e c t 22 p e le3
DhjeetL
m e m b e i V a i i a b l e 2
m e m s e s v a i i e s h L
membeu r a l n s e
membeA vaiiable 2
membe Vailo bh2
mrnbe v a i u a s i2
memoy cuatod
mem
7
(la)
S7ATIC DATA MENBERS
a4
gualifreol
class can be a
date
membe57
a
hes, Vasa Lle
A s t u t i e mem
pmpeitiae e f
7he Vauolle
t tKat 7 a C sutie
Simila Cha(a-
Chaa
CeKTanu p
CeKdann e ccial
spe ia
har
)StaTie
membet Vaiiabe
membeu u
entaZrd p 7h
tha that
e cy 7
0y
t haieol ey all th eaeit
entir clhs aet Ca
a l
a
cLatrd.
cnsatis
mat#L
Kow man hje
ns
cla, but t
biue i
t7
idi tiosn K
t he
e class,
clas,
vsibte ly
'a
( pegtam.
Z the enñy
trme
uded t o matt w
Satie vataslu
ae noimaly
ts-the ent elars .
valuis common
PAoLam2.
* i n e d u d i o t t i v a m
cla
uLing na miipace
clat 1em
Pubhè
ei'd geten ta Cn a)
numbet =
Ceun t '
2
Vtd etlotunt l vdo)
Cout H Count
a, 4,c
agetmurt)
bJottntC)
Cettount ( )
agetdala. (ioo
bgetdnta (200)
Cetdal lBoo)'
Cout < A ptes A e a o i p clala " ze "|n "
autCount)
6ekcoentt)
CJetcomnt ()
3
7he ouyut ofAe þmam
COunt: D
(ont
Ceunt 0
A
teAadnp data
Couht 3
Count 3
Cout 3
int t m :: count
olata membere
wth any
clars oyoee1hey
VaAralis
memse4fune7tor
nm--inuie
Vas/asles
at. Cike
statc
dtelased
tu a Clas de olaiatron ane
S
&tta
atti
roe
as hey au s h i l e dafininp
dafininp a
a
h
Loue .ile tSh le
drfned Can alko b e ' a t tYned
ntal
Value
Ame
ayinsle,
the vauasl
tm:! Count=10
Int
Statie Membel funct ons
that i de claud taii has Tbe
nembe fune en
ollosn9 opotties he
kaw arcex
& mly
s t a t i func7ron
can
)
or Vattasle) eclaieo
atie mombes (hincono
3
n TAL Same clas.
Th
rembel huncTrón can se calud uasp
A S tatie
class nam a4 slloor:
PARRam
mcuole<t'os~iam
e lass t e s t
n t co de
Staie mt Count '
Pushce
Votolsetmode Cvotel
Code = * t Couht
3
t d shnscoole Cvatel )
Cout " b 4 e e r nmumhe " < code < " | -
3
nt tes- " Count_
InE main)
S
trat tL, t 2
t.
l)setcode
t e s t ! s h a 2 L o u n t U
testt3
t3Setcode()
t e s - : ! s h o sc o u n t l ;
t shoocoolsl
t3.sh ervdCodel)
Coun 2
Count' 3
D numbet L
5hpeet u m s e a 2
tomk
wil nat
fhencioin
7h follnsn' sherseoaumtl)
oe vrded E t I A a t t e
I tahe cocle
Cmut << E d e S
2
ARRAY Of 0BJECTS
datatype
as ay can be of any
he krouO t h a t an
deys
Kaw u ayf
can alsa/
stutd Simil4kly wr
valsntlea
hedudm' elate. urh
au etHutype
of VatNstes tha
au
Calleod
anays 7 ge
b
class poyee
chau namel3 0
lont
Pushe
VielatdatalvoT
varel Putdata ( i t ) ,
3 2e a tel defncd
olata type
ee
empy t u a t u u l a t
The idtntifiei th i
cuala
ued
and cao Le empiye4.
Cave
shu
3m a n a g L
dppenn
l e y e e
manaei37; aua l, pret
of rema
oskeTrI. auay w ke
toeke.
emslóyee
empby
hoee
Thoee kzeia
ohyetid.
conlarus
manafu
aMay
7h
managuLt 7 .p u t d a t l ) ;
6thwauay
6thuu auay
hk ngy
ay
rhec
beha
b e
Kav
mulusa
s7 aMay
ate ostiks
aCeesfid
erore
a c e u
t n d v i o l u a l
the
membu
m mbu r n erork
frsn
acear
memsu opeqatw
LsE>
ThiStaement JUCgne he ohyet mananl IJ
invkethe membi Juntion pridn tal)
al ?
# r i n c u o l eK i o s t i e a m >
na meepace s-Ld,
tsIng
yee
S cha namel 30
float
tvrel)
pustie.
data
uttata
L e r
qet Cvtel)
n a e
n e name
"Enki
A
Cout
C name
"
E n t age
Ct
bord)
pentata
t m p l y e e
na
" z
Vord
Name
:
Cout
A
3 =3
Int s/
Con&+
i nt ma iu C)
Employee marapeqLire 7
s f m a n @ y e ' 2 e tt
Coict L2 n Desåele