SlideShare a Scribd company logo
Introduction ttoo PPrrooggrraammmmiinngg 
LLeeccttuurree 2266
TTooddaayy’’ss 
LLeeccttuurree 
– CCllaasssseess 
– OObbjjeecctt
ssttrruucctt
CCllaassss 
 AA ccllaassss hhaass 
– ddaattaa 
– ffuunnccttiioonnss
CCllaassss 
AA CCllaassss iiss aa uusseerr 
ddeeffiinneedd 
ddaattaa ttyyppee..
OObbjjeecctt 
TThhee iinnssttaanncceess ooff tthhee 
ccllaassss aarree ccaalllleedd 
OObbjjeeccttss..
Structure ooff aa ccllaassss 
ccllaassss nnaammee__ooff__ccllaassss 
{{ 
//// ddeeffiinniittiioonn ooff aa ccllaassss 
}}
EExxaammppllee 11 
ssttrruucctt DDaattee 
{{ 
iinntt ddaayy ;; 
iinntt mmoonntthh ;; 
iinntt yyeeaarr ;; 
}} ;; 
DDaattee mmyyddaattee ;; 
mmyyddaattee..mmoonntthh == 11 ;; 
mmyyddaattee..ddaayy == 2211 ;; 
mmyyddaattee..yyeeaarr == 11997799 ;;
EExxaammppllee 22 
ccllaassss DDaattee 
{{ 
iinntt ddaayy ;; 
iinntt mmoonntthh ;; 
iinntt yyeeaarr ;; 
}} ;;
EExxaammppllee 22 
mmaaiinn (( )) 
{{ 
DDaattee mmyyddaattee ;; 
//** mmaanniippuullaattee tthhee ddaattaa mmeemmbbeerrss 
mmyyddaattee..ddaayy ;; 
mmyyddaattee..mmoonntthh ;; 
mmyyddaattee..yyeeaarr ;; 
**// 
}}
EExxaammppllee 22 
ccllaassss DDaattee 
{{ 
iinntt ddaayy ;; 
iinntt mmoonntthh ;; 
iinntt yyeeaarr ;; 
}} ;;
EExxaammppllee 22 
mmaaiinn (( )) 
{{ 
DDaattee mmyyddaattee;; 
mmyyddaattee..mmoonntthh == 1100 ;; //// EErrrroorr 
}}
PPrriivvaattee
Default vviissiibbiilliittyy ooff 
aallll ddaattaa aanndd 
ffuunnccttiioonn 
iinnssiiddee aa ccllaassss iiss 
pprriivvaattee
PPuubblliicc
ccllaassss DDaattee 
{{ 
pprriivvaattee :: 
//// pprriivvaattee ddaattaa aanndd ffuunnccttiioonnss 
ppuubblliicc :: 
//// ppuubblliicc ddaattaa aanndd ffuunnccttiioonnss 
}};;
DDaattee CCllaassss 
ccllaassss DDaattee 
{{ 
iinntt ddaayy ;; 
iinntt mmoonntthh ;; 
iinntt yyeeaarr ;; 
}} ;;
mmaaiinn (( )) 
{{ 
DDaattee mmyyddaattee ;; 
mmyyddaattee..mmoonntthh == 1100 ;; //// iilllleeggaall 
}}
ccllaassss DDaattee 
{{ 
pprriivvaattee :: 
iinntt ddaayy ,, mmoonntthh ,, yyeeaarr ;; 
ppuubblliicc :: 
sseettMMoonntthh (( )) ;; 
pprriinntt (( )) ;; 
}};; 
DDaattee CCllaassss
mmaaiinn (( )) 
{{ 
DDaattee mmyyddaattee ;; 
mmyyddaattee..sseettMMoonntthh 
(( 1100 )) ;; 
mmyyddaattee..pprriinntt (( )) ;; 
}}
Separation ooff IInntteerrffaaccee 
ffrroomm tthhee IImmpplleemmeennttaattiioonn..
ccllaassss DDaattee 
{{ 
ppuubblliicc :: 
vvooiidd ddiissppllaayy (( )) ;; 
DDaattee (( iinntt ddaayy ,, iinntt mmoonntthh ,, iinntt yyeeaarr )) ;; 
pprriivvaattee:: 
iinntt ddaayy ,, mmoonntthh ,, yyeeaarr ;; 
}} ;; 
EExxaammppllee 33
void DDaattee :::: ddiissppllaayy (( )) 
{{ 
ccoouutt <<<< ddaayy <<<< ““// "" <<<< mmoonntthh <<<< ““// "" <<<< yyeeaarr ;; 
}} 
EExxaammppllee 33
SSccooppee RReessoolluuttiioonn 
OOppeerraattoorr
EExxaammppllee 33 
mmaaiinn (( )) 
{{ 
DDaattee mmyyddaattee ;; 
mmyyddaattee..ddiissppllaayy (( )) ;; 
}}
EExxaammppllee 33:: MMooddiiffiieedd 
ccllaassss DDaattee 
{{ 
ppuubblliicc :: 
DDaattee (( iinntt mmoonntthh ,, iinntt ddaayy ,, iinntt yyeeaarr )) ;; 
vvooiidd ddiissppllaayy (( )) ;; 
sseettDDaayy (( iinntt )) ;; 
sseettMMoonntthh (( iinntt )) ;; 
sseettYYeeaarr (( iinntt )) ;; 
pprriivvaattee :: 
iinntt mmoonntthh ,, ddaayy ,, yyeeaarr ;; 
}} ;;
EExxaammppllee 33:: MMooddiiffiieedd 
mmaaiinn (( )) 
{{ 
DDaattee mmyyddaattee ;; 
mmyyddaattee..sseettDDaayy (( 1100 )) ;; 
}}
void Date :::: sseettDDaayy (( iinntt ii )) 
{{ 
ddaayy == ii ;; 
}} 
EExxaammppllee 33:: MMooddiiffiieedd
EExxaammppllee 33:: MMooddiiffiieedd 
mmaaiinn (( )) 
{{ 
DDaattee ddaattee11 ,, ddaattee22 ,, ddaattee33 
;; 
ddaattee11..sseettMMoonntthh (( 1100 )) ;; 
ddaattee22..ddiissppllaayy (( )) ;; 
}}
CCoonnssttrruuccttoorr
EExxaammppllee 33:: MMooddiiffiieedd 
ccllaassss DDaattee 
{{ 
ppuubblliicc :: 
DDaattee (( iinntt mmoonntthh ,, iinntt ddaayy ,, iinntt yyeeaarr )) ;; 
vvooiidd ddiissppllaayy (( )) ;; 
pprriivvaattee :: 
iinntt mmoonntthh ,, ddaayy ,, yyeeaarr ;; 
}};; 
DDaattee :::: DDaattee (( iinntt mmoonntthh ,, iinntt ddaayy ,, iinntt yyeeaarr )) 
{{ 
//// BBooddyy ooff tthhee ffuunnccttiioonn 
}}
EExxaammppllee 33:: MMooddiiffiieedd 
mmaaiinn (( )) 
{{ 
DDaattee mmyyddaattee (( 11 ,, 11 ,,22000022 )) ;; 
mmyyddaattee..ddiissppllaayy (( )) ;; 
}}
Date :: Date ( int day , int month ,, iinntt yyeeaarr == 22000022
EExxaammppllee 33:: MMooddiiffiieedd 
mmaaiinn (( )) 
{{ 
DDaattee mmyyddaattee (( 11 ,, 11 ,,22000022 )) ;; 
DDaattee mmyyddaattee (( 11 ,, 11 )) ;; 
}}

More Related Content

PPT
CS201- Introduction to Programming- Lecture 20
PPT
CS201- Introduction to Programming- Lecture 07
PPT
CS201- Introduction to Programming- Lecture 30
PPT
CS201- Introduction to Programming- Lecture 34
PPT
CS201- Introduction to Programming- Lecture 40
PPT
CS201- Introduction to Programming- Lecture 28
PPT
CS201- Introduction to Programming- Lecture 09
PPT
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 07
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 09
CS201- Introduction to Programming- Lecture 29

What's hot (20)

PPT
CS201- Introduction to Programming- Lecture 03
PPT
CS201- Introduction to Programming- Lecture 42
PPT
CS201- Introduction to Programming- Lecture 15
PPT
CS201- Introduction to Programming- Lecture 33
PPT
CS201- Introduction to Programming- Lecture 12
PPT
CS201- Introduction to Programming- Lecture 25
PPT
CS201- Introduction to Programming- Lecture 41
PPT
CS201- Introduction to Programming- Lecture 16
PPT
CS201- Introduction to Programming- Lecture 10
PPT
CS201- Introduction to Programming- Lecture 11
PPT
CS201- Introduction to Programming- Lecture 06
PPT
Ankit
PPT
CS201- Introduction to Programming- Lecture 44
PPT
CS201- Introduction to Programming- Lecture 27
PPT
CS201- Introduction to Programming- Lecture 14
PPT
CS201- Introduction to Programming- Lecture 13
PPT
CS201- Introduction to Programming- Lecture 37
PPT
CS201- Introduction to Programming- Lecture 36
PPT
Edit data base menggunakan web
PPT
CS201- Introduction to Programming- Lecture 08
CS201- Introduction to Programming- Lecture 03
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 15
CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 12
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 16
CS201- Introduction to Programming- Lecture 10
CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 06
Ankit
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 14
CS201- Introduction to Programming- Lecture 13
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 36
Edit data base menggunakan web
CS201- Introduction to Programming- Lecture 08
Ad

Viewers also liked (18)

PPT
CS201- Introduction to Programming- Lecture 43
PPT
CS201- Introduction to Programming- Lecture 45
PPS
ENG101- English Comprehension- Lecture 38
PPT
MGT101 - Financial Accounting- Lecture 23
PPT
MGT101 - Financial Accounting- Lecture 32
PPS
ENG101- English Comprehension- Lecture 26
PPS
CS101- Introduction to Computing- Lecture 45
PPT
CS201- Introduction to Programming- Lecture 21
PPS
CS101- Introduction to Computing- Lecture 36
PPS
CS101- Introduction to Computing- Lecture 25
PPT
CS201- Introduction to Programming- Lecture 17
PPT
MGT101 - Financial Accounting- Lecture 35
PPS
CS101- Introduction to Computing- Lecture 28
PPT
ISL201 - Islamic Studies- Lecture 15
PDF
Consumer electronic show (ces) 2015 report - Wearables, robots, smart home, a...
DOC
Irr verbs21 2_sl
DOCX
Resumen el enfero imaginario
PPTX
Making information easy for voters
CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 45
ENG101- English Comprehension- Lecture 38
MGT101 - Financial Accounting- Lecture 23
MGT101 - Financial Accounting- Lecture 32
ENG101- English Comprehension- Lecture 26
CS101- Introduction to Computing- Lecture 45
CS201- Introduction to Programming- Lecture 21
CS101- Introduction to Computing- Lecture 36
CS101- Introduction to Computing- Lecture 25
CS201- Introduction to Programming- Lecture 17
MGT101 - Financial Accounting- Lecture 35
CS101- Introduction to Computing- Lecture 28
ISL201 - Islamic Studies- Lecture 15
Consumer electronic show (ces) 2015 report - Wearables, robots, smart home, a...
Irr verbs21 2_sl
Resumen el enfero imaginario
Making information easy for voters
Ad

Similar to CS201- Introduction to Programming- Lecture 26 (17)

PPT
CS201- Introduction to Programming- Lecture 32
PPT
CS201- Introduction to Programming- Lecture 22
PPT
CS201- Introduction to Programming- Lecture 23
PPT
CS201- Introduction to Programming- Lecture 38
PPT
CS201- Introduction to Programming- Lecture 24
PPT
Teaching English grammar
PPT
Syrah clones for warm climates
PPT
Syrah clones for warm climates
PPT
19 Algorithms and complexity
PPT
CS201- Introduction to Programming- Lecture 31
PPT
Md1 2007-2
PDF
Z deck operations
PPT
CS201- Introduction to Programming- Lecture 35
PPT
Morphemes
PPT
Microsoft excel 2
PPT
CS201- Introduction to Programming- Lecture 39
PDF
MSE 602 - Inspect Process Improvememt
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 24
Teaching English grammar
Syrah clones for warm climates
Syrah clones for warm climates
19 Algorithms and complexity
CS201- Introduction to Programming- Lecture 31
Md1 2007-2
Z deck operations
CS201- Introduction to Programming- Lecture 35
Morphemes
Microsoft excel 2
CS201- Introduction to Programming- Lecture 39
MSE 602 - Inspect Process Improvememt

CS201- Introduction to Programming- Lecture 26