0% found this document useful (0 votes)
80 views

Classes and Objects Part1

Uploaded by

Mohit Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Classes and Objects Part1

Uploaded by

Mohit Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CLAssEs AND DETEC7S

A cass }ean extenuon of the iodea Sthuctiuue Licel


C . 7he most mpontant Jeatasu s7 c44 dhi 'claur'.
T Signi fieanee ir igh herd by h
fict that
hdluP inulialy gaulhe ame

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 .

Once t h ctuclare Type hal keen depned, we carcseal


Vagta declatatiins t h a a
that ype usn
rnila h e built type dechialiend

Slui stuclen

Chau namiloJ
Iht stl-numbu
4loat total_maiks

7he
keynerd tauct leclats luedsn & as neno dala a

pe Yha Can hstd thote elds dflut daleypu.


These 4ielel au Kheron al tuuctau membeAs y
ements 7he identher stud1ns, Sh ih
Aeleuol to al uolin mame or stiutao Ta, Can
be ttd s coueti Vuwasles y p t stuolen &

S t u c Stolnt A c dcdaialon

ana Kas thte


Ais a Vaivosle D7ype Ataolun
membeu Vaivables a4 depned by thu Tamplaka.,
cot Y
Nembe Vavastes can be a cc erud uyn The
peu'od opes dlor

SApyl A:name , "Jshn "


A Hsllbumben=?91
A Ts7nlmaik! =S)SS*
inal-total = A- to/almaiki +5

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

D Ct+ Supports all the ealuu 7


Siuclincu as edefno
in C. 'Brut+ C++ has expandea t t apa bilitie

Aurthat Suit i t oDP thils 7

a Hemmpts ks nbun, h uwudefpnrd lypes Calt


Class closea pes4
and a/ro pwvieles a a t itiky b aey hoee
ypa
1he data,
Toheranct e a mechanie m by h c h one ype
37
inherit 7het i p u u
Can chaacteuictii.s fn
1S also
Supptkd Ctt.
7 n
77,
7
Shnictane Can a e o t u Vatabls a
a

An chns as membeus , Ti can a lso declac some


paivate' so
They eannst

membeis as 7hat

be aCcessecl lhe by the -ex n a l Anchons.

S> T ltt, the smietins ames au stan od oulone


and can be ueed ike any othu ype namis.
The Rey oes a u e con be omitted 1 tha eleelaati

Sttetars vaslebles.

Shiden& A', Ct+ leclakattn


Thc ie en enTe in C

The only oliffeunte between a ciutue anod a clasu


tIn ct 4 7hat by elafault t h mem bne s a

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

From crenal uu exetnal


u

t e hidelin, if necesrauy a a n e o abaliace


aellcceatin y 7hw buil
clae we
o 7hw
ehefnny a
be uatfl
kike any
That n
dntrkype
th dnla lype.
A ClaarpecfeaTionhas toopadi.

c l a s s decaralion
clarslepn1tron

Puncier
and seg7
Thetyppe
becifrés
h e Ela44 oleetaatrin defiitron
oles
ebes

and elass tinetion


mem beu aiL
implemented.

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

The clas Pdy ConTaus The olecaialion 57 vaiiables


and Anttiods, hese unctrins and Vasia bles ae

Colleehwl Called elau membu

Theq acutualy genped


Theg unde tvo Sectrens

&aendt whe èh 6f the


6neyprilglz hnt pusde
anol h i Ch 7 7hem
membes ale pvati

The key-oe pmvat anod blhe a Knmn a

icibeli labek.

The class membes that hane been eleclerrd a perva


Can be arcemed nly h«thin t clas.
Ca9 se accoud
m thi dlhet hand pushc nembur
m 6xaicle thu clak albo,

By default, Tke membu a Clat a_piivalb,


DA brtuw he labe au mie/n Than by otefaiult
all he membeu at ptiva
Buch a clats tr Compleely hoteten pom he ou/ele

odl and dees not deue a y puype


The
Vailab/es declateol 1nc/cle he clas a g kn w as
Nata membeus ano 7he fun ltrcns au knoon a

mem bet unorons , mly 7he membe incone Cay


hane eeeas tz e b b a s data nem bus and
Ava l Ainctrono.
7he b h e memStu bottunttrent and data) Ca
be accoeed m 7tsicl he l a s

The bihdip data and fun otren osehes in

Spyi ehdu ~ype varnsle Kepus ts a4

encapsilatioin.

Class
No
Enley to -
ivai aya

X
Date
hin ctrons

Puslhe aua
enay alned|
Pu6/èosal
Dala

hunctoms

nPetr Kicdig tu clases


S m p l e cJau Evamp
lt

clae km

V a t t a b l e declata-
tren
Int tumber

lont c s paiva y dfaute

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

7he clar specificntron like a staclane pioNelis


my a emplals and dets 6 S r a l az Mem N

pace the Thy eet.


e c s Can'se Ceateol Shenb a clasi 2 definod
placf 7heri mames
imm1dialuly aPer th

yclosn? &ace as he do in he cyie7 stu ctuu

elass tem

zty
Atm

lce EssING CLUASs M\EMBERS


7
7he privale data
clau a Can e accene ny

Thrryph the nembolJuncdint 7hau clags.

Shjettname . frun Chm-ame (aelual ctifumentt)


etda la Clo, 7S.
umses and
Z val anol aisgns 7ha alur l o
mplemen trhp
t utdata) fnctrón

memst.
Valuts 57 eda7a
wauld duplay 7h
6y 49
Amembe iinctie Can be Invoked
an h y e i .
ho meanin/
getdata Clo, 7Ss)

7he statrmente 1Ke

numbu =/op|/tgal

A
A vailaLe decatud at publie Can be a ecescAd y

The ct- dutcty

Int
Int
P 7 0,/er
pusie P Z = /0 P vals
int 2

y2
6)
DEF INING MEMBER FUnc71ONNC

Membet funtren Can be elefied Ih hoo places

Oectsicle the cla& dafà itrcn


&) Zssete the class edefanttrón.
he place. elefmrt ein, he fnetión
Irepecthue 7
he Samo 7äck.
Sholdplform
A OUTSIDE 7HE CLASS DEFINIT ION
ns)cle a c/a
l e c lart
Mermset frnct one 7ha aiu

6 t riel the class,


elejird Sepatcath al2
haw b e ke The nom

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,

Votol te m putdalo (vtol)

humbei c " \n"


Co utre< "Numbel

" 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

fencron definitrón insele he elask.


eialor
Clats em

1nt numbe
Ploat Cos
Pushé:
Y d etdatal inz a, float 6)}/dethsalian

Vi putdalo l viol) the clas

Cout& humbet ze "n ";

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 < <

Coute " cOIt

Memben functon Defnih on


a {oat 6)
Voobl t-lem
qtdala (t une maLel h
umbe = a oiveali vau asl
Coet=6, t u e l uao

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

clae can s e calllod only


membe funchon 7 a

7hab class uing a d opeA at


y aa n
hec 7 ik
Can be calltY by Lus/nz
A menmse_frn ct7on hudame
thkame

m e m b e s fiunétien
a7
aotAu. tnetros,
name hsile
sfmembe
as mestnig
i Knoo
class . 7 h i

PRIVATE MEWBER PwcT7ON

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

/ *a' u int type


1nta site T
pub/te votd)
t Val l
votd
vw oliphy Cver'd)'

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

NEMORY ALLOCATTON FOR


Shn thy
1shen thuy
2 allotates
he memey Spate for shyect
hot Lshen
the clastiu yelfed
a declalsd and

paitty uue. he membe Ainetione


hu sttimantu cnly
memenyYace 671/ once
au aeated anol placed Yr Ke
Arhen he ae lensod as a pait s7 a elas-eeeifieatin
rhe Lame.
Siia allne hyoek belonsn tb hat clas ue
t
membes hunction s, no Sepatata pace c allotaol
m e m b e faun chont tshen h e skpecti aw C i t a t e d , ..

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

h«td elerun d a t a Valuen For airent hzecó.


Cill " allohjeei
Cmmon fsr
membeA f u n c h o n

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

Cwhen 66eit defro

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

to zno when the irz u i


inttiallzed
(tratiztion
Z e cALatrA. a otlu tn
clap 2i
Dt
pygnethal.

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

Ctamt int tount


Int numbe

Pubhè
ei'd geten ta Cn a)
numbet =
Ceun t '
2
Vtd etlotunt l vdo)
Cout H Count

Ceut e ountce" In'


3
InE Hem :' Count
Int malin)

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

7he type and scpe s7 each ttntre mem e e vaua ske

muut he depnsd ovesrde. th claes cefnilrn Thie


catic data memns a t Ltned
e cekay be cause the
A a t t a s ttan
a
pait b7 an syoit , Snce
sepaarly aa then Tran
theaThan
h Tke clast t r e f
cot
aanoelals
tey e llars
e ars
au
a e to
as kner3 n
a knenan
aa s

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:

clachame ' uncAon-nanme

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 < " | -

Statte Votd Shoicotu tCvrA)

Cout<< 1ount? 2 Counte"|n

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

Sce a n auay atey


houal a°e

ulh tuu tRonthe


can
and
h e l e m e n t t

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

You might also like