Uploaded by riyal mohammadu

C++ in sinhala

advertisement
C++
mrs.Kl l%uf,aLKh
uq, isg ir,j
C++ Computer Programming
^1 fldgi&
- liqka ,shkf.a -
fmrjok
fuu fmd; uQ,slju ,shd we;af;a" m%Odk jYfhka Windows mrs.Kl fufyhqus moaO;sh ;=,"
C++ mrs.Kl NdYdj u.ska mrs.Kl l%uf,aLKfha (Computer programming) fhoSu bf.kSug
werTSfus leue;af;ka isgsk wh i`oydh' fus fyhska fuysoS lreKq yels;dla ir, f,i oelajSug
W;aidy f.k we;' fuu fmd; u.ska Tng C++ u.ska mrs.Kl l%uf,aLKfhys fhoSfusaoS Ndjs;d jk
uQ,sl ixl,am y|qkajd fokq ,efns' tfiau" tu ixl,am m%dfhda.slj fhdouska mrs.Kl jevigykla
(ir, Media Player jevigykla) ,shk wdldrho fmkajd fokq ,efns' fuu.ska Tng C++ Ndjs;d lr
uDoqldx. ,sjSu ms,sn`o uq,au jegySula ,ensh yel'
iqN C++ wkd.;hla!
2
C++ mrs.Kl NdYdj ms,sn`o flgs ye`oskajSula
C++ hkq C mrs.Kl NdYdfjys l=vd fidfydhqrdh' tkus" C++ iy C f.dvk.d we;af;a tlu
mokula u;hs' fus ksid isoqjsh hq;= mrosu C++ i;=j we;s ,CIK fndfyduhla u C NdYdfjyo ;sfnk
,CIKhs' C hkq b;d m%N, NdYdjla nj fkdryils' fuu m%N,;djh oelajSfus fyd`ou WodyrKhla
kus Unix mrs.Kl fufyhqus moaO;shh' Unix iusmQraKfhkau ,shd we;af;a C Ndjs;fhks' tfukau
Windows 98 iy Windows NT o ,shd we;af;a jevsjYfhkau C mdjspsps lsrSfuks' ta wkqj" C mokus
fldgf.k" C ys jevs oshqKqjla f,i ksmojd we;s C++ mrs.Kl NdYdj jvd;a m%N, NdYdjla jS we;'
C NdYdj fuf,dj t,sh olskq ,enqfha 1970 jifrAoSh' tys ksrAud;D jQfha AT&T Bell mrAfhaIKd.drfha
fiajh l, Dennis Ritchie kus mrs.Kl l%uf,aLlhdh' C mokus lrf.k C++ ksrAudKh lrkq
,enQfhao AT&T Bell ysu fiajh l, fodia;r Stroustrup keue;a;dh' Tyq ;u ksrAudKh nsys lsrSu
werTQfha 1980 .Kkaj,oShs'
C fukau C++ NdYdjo fusjk jsg jsYd, ckms%h;djla oskdf.k we;s w;r fuhg idCIs kus wm Ndjs;d
lrk fndfyduhla u mrs.Kl uDoqldx." iy mrs.Kl lS%vd wdosh C fyda C++ u.ska ,shd ;snSuh'
mrs.Kl l%uf,aLlhka merKs C fjkqjg C++ Ndjs;d lsrSug fya;=j kus" C++ u.ska mrs.Kl
jevigyka ,sjSu C u.ska jevigyka ,sjSug jvd myiq jSuh' tfukau C ys wka;rA.; fkdjk fndfyda
wxf.damdx.o C++ ;=, oelsh yel' fusksid" C++ bf.kSug Tn fhdok ld,h b;d fyd`o wdfhdackhla
jkq we;'
1' Tfns m,uq mshjr
mrs.Kl jevigykla iy mrs.Kl NdYdjla hkq l=[email protected]
usksfil= yg mrs.Kl hka;%hla ,jd lsishus fohla isoqlrjd .ekSug wjYH jQ jsg m%:ufhka
Tyq l, hq;af;a [email protected] Tyq l, hq;af;a Tyqg wjYH ldrAhh l=ulaoehs tu hka;%hg meyeos,s lr oSuhs'
WodyrKhla f,i lsisfjl=g mrs.Klh ,jd 68975 hk ixLHdj 123 ka .=Klr.ekSug wjYHkus Tyq
fuh mrs.Klhg f;reus lr osh hq;=h' kuq;a Tn ysgs wvsfha mrs.Klh bosrshg f.dia thg wefik
f,i “lreKdlr 68975" 123 ka jevslr fokak” jekss fohla thg lsjfyd;a mrs.Klh isoqlrk tlu
foh jkq we;af;a" ;ud ;=, we;s jsoq,s mxldfjka uq.aO Ynhla k.uska fkdfi,ajS isgSu muKhs'
ulaksidoh;a mrs.Klh fld;rus iqmsrs hka;%hla jqj;a thg Tn Ndjs;d lrK usksia ni fkdjegySuh'
Tn;a mrs.Klh;a fuysoS fjkia NdYd folla Ndjs;d lrk" foaYhka follg wh;a usksiqka foofkl=
jeksh' tfiakus Tng wjYH foh mrs.Klh keue;s fuu “jsfoaYslhdg” meyeos,s lr oSfus l%uh
[email protected] tu l%uh kus wm ienE jsfoaYslhl=g hula f;rAeus lr oSfusoS Ndjs;d lrk l%uhuhs' tkus
NdYd mrsjrA:lfhl=f.a iyh ,nd .ekSuh' fuysoS Tfns msysgg tk NdYd mrsjrA:lhd kus Tfns
3
mrs.Klh ;=, wka;rA.; Calculator jevigykh' th mK.kajd th yryd mrs.Klh u.ska Tng
wjYH .Ks; lrAuh isoqlr .ekSug yelsh' Calculator uDoqldx.hg Tn by; ixLHd hq., iy .=K
lSrSfus jsOdkh iemhQ jsg" th Tfns o;a; iy jsOdkh mrs.Klh Ndjs;d lrkq ,nk NdYdjg" tkus
oajsuh ixLHd fkdfyd;a fofla mdofha ixLHd j,ska ieos mrs.Kl fla; njg mrsjrA:kh lr
mrs.Klfha uhsfl%d fm%dfiirhg imhkq ,nhs' fuu mrs.Kl fla; fofla mdofhka hq;= ksid" tajd
binary codes f,iska ye`okajsh yelsh' mrs.Klhg oeka kus l, hq;= foh jegfyk ksid th Tfns
ixLHd hq.,h .=K fldg" ,efnk ms,s;=r Calculator jevigykg kej; binary codes f,iu ,ndfohs'
Calculator jevigyk fuu oajsuh fla; f,iska we;s o;a;h usksidg y|qkd.; myiq oYuh fyj;a
oijk mdofha ixLHd j,g yrjd Tng osiajSug i,iajhs' fuf,i Calculator keue;s mrs.Kl
jevigyk NdYd mrsjrA:lfhl= f,i ls%hd lrhs' tfiakus" mrs.Kl jevigykl ls%hdj kus" fufia
usksid iy mrs.Klh w;r iusnkaOhla" nqoaOs md,ula ne`oSuh' fusksid" C++ mrs.Kl NdYdj
bf.kSfuka Tn mrs.Kl l%uf,aLlfhl= (Computer programmer)" fyj;a mrs.Kl jevigyka
ksrAudmlfhl= jSug woyia lrkafka kus" Tn w;.eish hq;af;a mrs.Klh yd usksid w;r fuu
iusnkaOh we;slsrSfus ldrAhhgh' Tn fuu NdYd mrsjrA:k ldrAhh Ndr .; hq;=h'
Tn NdYd mrsjrA:lfhl=f.a ls%hdj bgq lsrSug kus" Tng NdYd folla .ek wjfndaOh wjYH fjs' tkus"
Tng usksia ni fukau mrs.Klfha ni .eko jegySula ;snsh hq;=h' kuq;a oeka fuysoS .eg,qjla
u;=fjs' thkus" mrs.Klh Ndjs;d lrK oajsuh ixLHd ^fofla mdofha ixLHd& j,ska ieos NdYd l%uh
idudkH usksfil=g jgyd .ekSug b;d oqYalrjSuh' wo Ndjs;d jk mrs.Kl (bit 32 mrs.Kl) jgyd
.kakd NdYdfjs jpkhla (word) iEoS we;af;a 1 yd 0 hk b,lalus 32 la jsjsO wdldrhkag tla jSfuks'
fuf,i" fofla mdofha ixLHd id.rhla f,iska ksuj we;s fuu NdYdfjka mrs.Kl jevigyka ,sjSu
fuksid ienejskau lgqlh' fus .eg,qfjka w;au`oSfus udrA.h tfiakus [email protected] th kus" C++ jeks
l%uf,aLK NdYdjla bf.kSuh' C++ jeks mrs.Kl NdYdjka ksmojd we;af;a Tnj by; i`oyka l,
lgql oajsuh ixLHdxls; fla; ms,snoj W.ekSug isoqjSfus jsm;ska .,jd .ekSugh' C++ u.ska
isoqlrkafka Tng mrs.Kl jevigyka ,sjSug yelsjk f,i myiq NdYd l%uhla iemhSuh' C++
mrs.Kl NdYdj iEoS we;af;a usksidg f;rAeus .ekSug yels bx.%Sis NdYdjg ,xjQ jpk iuQyhlsks'
fusksid Tng C++ bf.kSu u.ska jvd myiqfjka mrs.Kl jevigyka ,SjSug yelsfjs'
C++ mrs.Kl NdYdj iy mrs.Klfhys iajNdjsl NdYdj w;r iusnkaOh
fuu iusnkaOh jsuid ne,Sug fmr" “mrs.Klfhys iajNdjsl NdYdj” ms,sn`oj ;joqrg;a jsuiSu
m%fhdackj;ah' Tn l,ska o lshjQ mrsos mrs.Klhlg lshjsh yels NdYdj iEoS we;af;a oajsuh ixLHd
j,sks' oajsuh ixLHd fyj;a binary numbers hkq 1 iy 0 hk ixLHd j,ska iEoqKq ixLHdh' WodyrKhla
jYfhka 100010110 hk ixLHdj oajsuh ixLHdjla f,i .; yelsh' TSkEu NdYdjla ksrAudKh jkafka
jpk j,sks' mrs.Klfha NdYdjgo fuh fmdoqh' tys jpkhla (word) hkq oajsuh wxl 32 la tlajS
iEoqKq tallhls' WodryKhla f,i wxl 32 lska ieos 10100110101011001010011110110111 hk ixLHdj
mrs.Klhl tla jpkhla f,i ie,lsh yel' kuq;a fuu 1 yd 0 hk ixLHd mrs.Klhg meusKsfha
[email protected] ir, f,iska lsh;fyd;a" fuu 1 yd 0 hk ixLHd j,ska ksrEmKh jkafka mrs.Klh ;=,
we;s uhsfl%dfm%dfiirA (Microprossesor) mrsm:fhys cks; jk jsoHq;a ix{djka fjs' mrs.Klfha
ls%hdldrS;ajhg fya;=jkafka fusjdh' fm%dfiirh ;=, wka;rA.; g%dkaisiagr u.ska fuu jsoHq;a ix{djka
bmfohs' 1 u.ska ksrEmKh jkafka g%dkaisiagrhla yryd jsoHq;h .uka lrk wjia:djls' 0 u.ska
ksrEmKh jkafka jsoHq;h .uka fkdlrk wjia:djh' fuu 1 fyda 0 hk tla jsoHq;a ix{djl
jsYd,;ajh nsgs 1ls (1 Bit). (Bits hkq mrs.Klhl o;a; Odrs;djka .Kkh lsrSu msKsi Ndjs;d flfrk
l=vdu tallhhs) mrs.Kl jevigyla u.ska isoqlrkafka" fuu oajsuh fla;hka ckkh lsrSu u.ska
mrs.Klfha uhsfl%dfm%dfiirh lsishus ls%hdjla isoqjSu msKsi md,kh lsrSuh' Binary codes j,g we;s
fus uhsfl%dfm%dfiirh md,kh lsrSfus hdka;s%l ugsgfus yelshdj ksid fuu fla;hka i`oyd Machine
codes fyj;a hdka;s%l fla; hk ku Ndjs;dfjs' uska bosrshg mrs.Klfha NdYdj .ek l:d lsrSfusoS wm
fuu Machine codes, ke;skus hdka;s%l fla; hk fhoqu Ndjs;d lruq'
by; Tn lshjQ mrsos mrs.Klfha NdYdfjka l%uf,aLKfha fhoSu hkq fuu oajsuh ixLHdxls; fla;
fyj;a hdka;%sl fla; Ndjs;d lsrSfuka mrs.Kl jevigyka ,sjSuh' uq,a ld,fhaoS l%uf,aLlhka jsiska
wkq.ukh lrk ,oafoa fuu wiSre l%uhhs' fuu wiSre;djh y|qkd.;a uq,a l%uf,aLlhska jsiska
Assembly kuska mrs.Kl NdYdjla y|qkajd fokq ,enSh' Assembly u.ska isoqjQfha hdka;%sl fla; u.ska
iemhsh hq;= mrs.Kl jsOdkhka fjkqjg" bx.S%is jpk j,g iudk fjk;a jsOdkhka y`oqkajdoSuh'
4
tkus" WodyrKhla f,i ixlSrAK hdka;%sl fla; j,ska ,shQ mrs.Kl l%uf,aLKhla ^l%uf,aLKhla
hkq ljr fyda mrs.Kl NdYdjlska ,shkq ,enQ mrs.Kl jsOdk /ila f,i ye`oskajsh yelsh& Assembly
NdYdjg fmr,qjfyd;a" th jvd myiqfjka jgyd.; yels" Assembly jsOdk j,ska hq;= l%uf,aLkhla
njg m;a fjkq we;' fumrsos mrs.Kl l%uf,aLlhkag Assembly u.ska jvd myiqfjka jevigyka
ieliSfus wjia:dj ,ensKs' l%uf,aLlhd ;u jevigyk Assembly Ndjs;fhka ,shd th hdka;s%l
fla;hkag mrsjrA:kh lsrSug Assembler keue;s uDoqldx.h mdjspsps lrkq ,enSh' Assembly jeks"
hdka;%sl fla; mdol fldgf.k ksrAudKh lr we;s mrs.Kl NdYdjka Low Level Languages fyj;a
my; ugsgfus mrs.Kl NdYd f,i ye`oskafjs'
flfiafj;;a" Assembly mrs.Kl NdYdjo bf.kSug t;rus myiq fkdjSh' tfia jQfha th l%uf,aLKh
jvd myiqfjka f;reus .; yels ;;ajhg m;a l,o" th u.ska lsishus mrs.Kl ls%hdjla isoqlsrSu i`oyd
,sjsh hq;= jsOdkhka m%udKh wvq fkdjQ fyhsks' ir, mrs.Kl jevigykla jqjo Assembly u.ska
,sjSug W;aidy l<fyd;a" th b;d os.= l%uf,aLKhla jkq we;' fuksid Assembly u.ska l%uf,aLKfha
fhoSuo ld,h fndfyda f,i jehjk lghq;a;la jQfhah' fus ksid jvd myiq f,i W.; yels" jvd
ir," kj mrs.Kl NdYdjka ksrAudKhg mrs.Kl l%uf,aLlhska fm,TsKs' fus whqrska ksmojqKq
NdYdjkah C iy C++. Assembly u.ska isoqjkafka hdka;s%l fla; i`oyd wdfoaYl jk jsOdk iemhSu jQ
whqrska" C / C++ u.ska isoqjkafka tu Assembly jsOdk /i ne.ska tla fldg tajd i`oyd wdfoaYhka jk
jvd ir, mrs.Kl jsOdkhka y|qkajdoSuhs' fus ksid hdka;s%l fla; Ndjs;fhka ,shq jevigykla
Assembly u.ska ,shQ jsg th jvd ir, jk w;r" thu C / C++ j,ska ,shq jsg th jvd;a flgs iy ir,
fjs' fus wkqj C / C++ jeks mrs.Kl NdYdjka ;kd we;af;a Assembly NdYdj mdol fldg f.k" th
u;h' fusksid l%uf,aLlfhl= jsiska C++ u.ska mrs.Kl jevigykla ,shQ jsg Tyq th m,uqj
Compiler keue;s uDoqldx.h Ndjs;d fldg Assembly NdYdjg mrsjrA:kh l, hq;=h' bkamiqj Tyq tu
mrsjrA:kho kej; Assembler uDoqldx.h u.ska hdka;s%l fla; njg mrsjrA:kh l, hq;=h' fuh
isoqlsrSu b;d myiq fohls' th isoqlrk wdldrh bosrsfhaoS Tn bf.k .kq we;' C++ jeks NdYd
Assembly NdYdj mdol fldg ilid we;s by, ugsgfus NdYd ksid tajd fmdoqfjs High Level
Languages f,i ye`oskafjs'
mrs.Kl jevigykla ieliSug" uQ,sl jYfhka Tng my; oelafjk oE wjYH fjkq we;:
1. mrs.Kl jevigyka ilik uDoqldx.hla - Program editer.
wm mrs.Kl jevigykla ,shkq ,nkafka fuu jrA.fha uDoqldx.hla ;=,h' Tn Windows mrs.Kl
mdjspsps lrkafka kus" Tng f;drAd.; yels fyd`ou jevigyka ilik uDoqldx.h jkafka Microsoft
Visual C++ 6.0 h' fuh Tng myiqfjka fj,`ofmdf,ys fidhd.; yelsh'
2. Compiler
Compiler uDoqldx.h u.ska isoqlrkafka C++ jeks High level language >Khg jefgk mrs.Kl NdYd
u.ska ,shk ,o mrs.Kl jevigyka" mrs.Klhg jegfyk oajsuh ixLHdjka njg mrsjrA:kh lsrSu
nj Tn by; oS lshjQjd' Tn Microsoft Visual C++ 6.0 us,oS .;ajsg" tys Compiler uDoqldx.hla o wvx.=
jkq we;'
3. Debugger
fuh" Tn ,shQ mrs.Kl jevigykl fodaI we;akus tajd y`oqkd.ekSug myiqlus i,ik uDoqldx.hls'
Microsoft Visual C++ 6.0 ;=, Debugger uDoqldx.hla o wka;rA.; fjs' Visual C++ debugger myiqlu
Ndjs;hg .kakd wdldrh fmdf;ys Appendix fldgfiys flgsfhka oelafjs'
fuu fmdf;ys u;=jg wvx.= jk ish,q jevigyka ,shd we;af;a Microsoft Visual C++ 6.0 Wmfhda.S
lr.ekSfuks' fusksid fu;eka isg fuu fmdf;ys wvx.= ls%hdldrlus isoqlsrSu i`oyd Tn tu uDoqldx.h
5
Tfns mrs.Klh ;=, install lr.; hq;=h' Visual C++ 6.0 install fyj;a ia:dmkh lrk wdldrh
Appendix fldgfiaoS meyeos,s lr we;'
Tfns m,uq C++ mrs.Kl jevigyk
my; oelafjkafka C++ Ndjs;fhka Tng ,sjsh yels b;du ir," ls%hdldrS jevigykls' m,uqfjka u
lsjhq;= jkafka fus jevigyfkys wvx.= ish,qu foa jgyd .ekSug Tn W;aidy fkdl, hq;= njhs' th
bosrsm;a lr we;af;a yqfola C++ u.ska ,shk ,o mrs.Kl jevigykla fujekaklshs y`oqkajdoSu
msKsih' tu ksid tys wvx.= ish,qu oE iusmQrAKfhka u jgyd.ekSug W;aidy fkdlrkak' Tng oekg
fkdjegfyk oE" fuu fmd; bosrshg lshjSu;a iu. wjfndaO jSug mgka.kq we;:
=====================================
// hello.cpp
#include<iostream.h>
void main()
{
cout << “Hello beautiful world!!!” << endl;
}
=====================================
wm oeka isoq lsrSug hkafka fuhhs: m,uqj Microsoft Visual C++ jsjD; lruq' bkamiq by; jevigyk
ta whqrskau tys h;=re ,shkh lruq' tkus" wmf.a jevigyk jsiska l, hq;= oE" C++ jsOdkhka /ilska
,shuq' uS,S.g wm lrkafka fuu C++ jsOdkhka hdka;%sl fla;hka njg mrsjrA:kh lsrSuh ^tfia
fkdl,fyd;a mrs.Klh tu jsOdkhka y`oqfkd.kq we;&' tkus" wm C++ mrs.Kl jevigyk compile
l, hq;=h' wjidkfhaoS wm tu mrsjrA:kh lrk ,o mrs.Kl jevigyk ls%hd;aul lruq' fuh
isoqlrk wdldrh mshjrska mshjr my;ska oelafjs'
uq,skau l, hq;= jkafka Tfns jevigyk wka;rA.; lsrSu i`oyd Visual C++ ;=, kj Project tlla
ksujSuh' Project tlla hkq lsishus mrs.Kl jevigykla i`oyd m%fhdackhg .efkk ish,q C++ files
wka;rA.; lsrSu i`oyd ;kkq ,nk tallhls' by;ska wm oqgq jevigyfkys kus ;sfnkafkaa tla file
tlla muKla jQjo miqj Tng oelSug ,efnk mrsos mrs.Kl jevigykla hkq C++ files iuQyhl
tl;=jls' C++ files hkq “.cpp” hk extension tl iys; computer files h'
1' m,uqfjkau" fuu fmdf;ys wvx.= jk ish,q jevigyka ;ekam;a lsrSu msKsi" Tfns mrs.Klfhys
oDV ;egsfhys Tn leu;s ia:dkhl “VC++ book” kuska kj folder tlla ;kkak'
2' oeka Microsoft Visual C++ 6.0 uDoqldx.h jssjD; lrkak'
3' tys File keue;s menu tflys we;s New keue;s item tl ^File > New& select lrkak'
4' u;=fjk Dialog Box tflys Projects window tl ;=, we;s Win32 console application keue;s
icon tl select lrkak' ^?mh 1'1& fufia isoql,hq;af;a wm fus wjia:dfjoS iEoSug n,dfmdfrd;a;=
jkafka console jrA.fha mrs.Kl jevigykla ksidh' fuh u`ola myeos,s lr.ekSug wjYH fohlss'
m%Odk jYfhka i,ld n,k jsg mrs.Kl jevigyka jrA. follg fnfoa'
tkus"
(1)
Console applications fyj;a Terminal (DOS) window tl yryd
ls%hd;aul l,hq;= mrs.Kl jevigyka iy"
6
(2)
GUI (Graphical User Interface) tlla fyj;a ?muh u;=msgla
hkqfjks'
iys; mrs.Kl jevigyka
by; wm oqgqfjs fuu Console >Khg jefgk ir, jevigykls' fusjdg mdjspsps lrkakd iu.
wka;rA ls%hd lsrSu msKsi GUI tlla fyj;a rEmuh u;=msgla fkdue;' th mdjspsps lrkakd iu. lghq;=
lrkafka Terminal fyj;a DOS window tl yrydh' fus i`oyd Tyqg mrs.Klfha h;=re mqjrej WmldrS
fjs' wfkla w;g GUI tllska hq;= jevigykla hkq rEmuh u;=msgla u.ska jevigyk mdjspsps
lrkakd iu. wka;rAls%hd isoqlrk uDoqldx.hls' Microsoft Windows ;=, Tn Ndjs;hg .kakd ish,q
ckms%h mrs.Kl jevigyka fus >Khg whs;s fjs' Visual C++ o wksjdrAhfhkau GUI application
tlls'
(rEmh 1'1)
5' Location keue;s Text box tl u.ska isoqjkafka Tn idok project tl oDV ;egsfha .nvd l,hq;=
ia:dkh oelajSuh' oeka Location text box tlg bosrsfhka we;s Button tl click lr" oDV ;egsfhys by;
Tn ;ekQ “VC++ book” folder tl select lrkak'
6' Project name text box tl ;=, wm iEoSug kshus; jevigyfka ku f,i “hello” hkqfjka type
lrkak' ^?mh 1'1&
7' OK button tl Tnkak' fujsg “hello” hk kuska hq;a folder tlla “VC++ book” folder tl ;=,
Visual C++ u.ska ksmojkq we;'
8' oeka 1'2 rEmfha oelafjk dialog box tl Tn bosrsfha osiajkq we;'
7
(?mh 1'2&
9' by; rEmfha wdldrhg tu dialog box tflys we;s “An empty project” hk option tl select
lrkak' tysoS wm VC++ fj; mjikafka" oekg lsisoq file tlla wvx.= fkdjk" ysia project tlla idok
f,ih' bkamiq Finish button tl click lrkak' fujsg ;j;a dialog box tlla open fjsjs' tho OK
lrkak' oeka “hello” keue;s ysia project tlla Tn idod we;'
Visual C++ window tflys ol=Kq mi we;s mgq window tl foi oeka n,kak ^rEmh 1'3&' fuu fldgi
yeosfjkafka workspace hkqfjks' tu window tflys my, wka;fhys file view keue;s tab tlla we;s
wdldrh n,kak' tu tab tl u; click lrkak' tjsg Tn fudfyd;lg fmr iEoQ project tl fuu file
view keue;s window tl;=, Tng oel.; yelsh ^rEmh 1'3&' tys “hello files” hkakg hgska we;af;a
Tfns project tlg wod, ish,q files h' Tn idok C++ files wka;rA.; jkafka tys “source files” hk
kuska oelafjk folder tl ;=,h' “Header files” hk folder tl;=, wka;rA.; jkafka Tn ;ju y`oqkd
fkd.;a Headers kuska yeoskafjk files jsfYaIhls'
(?mh 1'3)
Tn fuu folders u; double click lr ne,qjfyd;a tajdfha lsisjla wvx.= fkdjk nj Tng oelsh yels
jkq we;'
oeka Tn ierfikafka wmf.a ysia project tlg C++ file tlla we;=,alr by; wm oqgq mrs.Kl
jevigyk tu file tl;=, type lsrSugh' my; oelafjk f,i fuh isoqlrkak:
1' File > New menu tl u.ska New dialog box tl open lrkak ^1'4 rEmh&'
8
2' 1'4 rEmfha oelafjk mrsos tys File window tflys we;s “C++ source file” keue;s item tl select
lrkak' bkamiq Dialog box tflys “Add to project” keue;s check box tl check lrkak' “File
name” text box tl ;=, oeka wm ksujk C++ file tflys ku f,i “hello” hkak type lr dialog box
tl OK lrkak' tjsg 1'5 rEmfha fmfkk f,i kj ysia C++ file tlla Visual C++ ;=, jus mi we;s
jsYd, window tl;=, open fjsjs' fuu window tl y`oqkajkafka “Editor window” hk kusks' fus
wjia:dfjsoS Tn workspace window tl foi ne,qjfyd;a source files hk folder tl;=, Tn fus oeka
ksujQ “hello.cpp” hk file tl ;sfnkq oelsh yelsjkq we; ^?mh 1'6&'
(?mh 1'4)
(?mh 1'5)
(?mh 1'6)
9
3' oeka Tn l,hq;af;a by; Tn W.;a mrs.Kl jevigyk" editor window tl ;=, open jS we;s fuu
kj document tl fyj;a “hello.cpp” ;=, type lsrSuh' oeka th isoq lrkak'
oeka Tn C++ NdYdj u.ska ir, jevigykla ,shkq ,enqjd' fjk;a jpk j,ska lsh;fyd;a Tn lsishus
oE reila isoqlrK f,i C++ jsOdk reila u.ska mrs.Klhg wKl,d' kuq;a C++ u.ska Tn l, ta
wK lsrSus mrs.Klhg fkdjegfyk ksid Tn fmroS lshjQ f,i jevigyk compile lsrSfuka th
oajsuh fla; njg mrsjrA:kh l, hq;=hs' fuh my; oelafjk wdldrhg isoqlrkak'
1' Visual C++ ys Build Menu tflys “Build hello.cpp” keue;s item select lrkak ^?mh 1'7&' tfia
ke;skus tu Menu item tflys keyboard shortcut jk F7 h;=r Tnkak'
(?mh 1'7)
2' oeka Tfns mrs.Klfha fjs.hg wkqrEm ld,hloS oeka hello.cpp file tl compile fjkq fyj;a osjsuh
fla; njg mrsjrA:kh jkq we;' fujsg Tn olsk ;j;a fohla jkafka Visual C++ ys my; fldgfia
we;s os.= window tflys huslsisjla igyka jk nj fyj;a print jk njh' fuu pane tl yoqkajkafka
“Output window” hk kusks ^1'8 ?mh&'
(?mh 1'8)
Output window tl u.ska ldrAhhka lSmhka isoqlrhs' tlla kus lsishus file tlla compile lsrSfusoS
thg wod, jsia;r print lsrSu fyj;a osiajSug ie,eiajSuhs' “thg wod, jsia;r” hkqfjka m%Odk
fldgu yeoskafjkafka" compile ls%hdj,shg ,lajk mrs.Kl jevigyfkys jHdlrK fodaY fyj;a
syntax errors wdosh we;skus tu fodaYhkao" tu fodaYhka jevigyfkys mj;sk ia:dkhka ms,sn`o
jsia;r wdoshhs'
^fus ms,n`o jevs jsia;r oek.ekSug fmdf;ys Appendix fldgfia we;s “mrs.Kl jevigyka j, fodaY
- Programming errors” keue;s fldgi lshjkak&
by; jevigyk Tn ksjerosj h;=re,shkh lrkq ,enqjdkus" jevigyk fodaY j,ska f;dr nj 1'8
rEmfha oelafjk wdldrfha output tlla u.ska output window tl Tng olajkq we;'
wm oeka wmf.a hello keue;s mrs.Kl jevigyk compile lr wjidkhs' Microsoft Visual C++ jsiska
Tn ,shk ,o jevigyk oajsuh fLs; j,ska ,shkq ,enqjd'
oeka" u`olg by;oS Tn idok ,o “VC++ book” folder tl Windows explorer u.ska jsjD; lrkak' tyss
wvx.= “hello” keue;s folder tl Tn olskq we;' tho jsjD; lrkak' bkamiq th ;=, we;s “Debug”
keue;s folder jsjD; lrkak' tys Tng “hello.exe” keue;s file tlla oel.; yelsjkq we;' Tn ,shQ
jevigyk oajsuh fLs;hka f.ka ,shjS we;af;a fuu file tl ;=,h' fjk;a jpk j,ska lsjfyd;a" fus
kus Tn ,shQ mrs.Kl jevigyfkys l%shdldrS wjia:djhs' fus ksid Tng fuu file tl open lsrSfuka
10
Tfns jevigyk ls%hd;aul l, yel' kuq;a fuysoS wm “hello.exe” u; click lruska th ls%hd;aul
fkdlr" Visual C++ u.ska u th ls%hd;aul lrjuq' th my; f,i th isoqlrkak:
12' visual C++ ys" Build > Execute hello.exe item tl select lrkak' ke;skus Ctrl + F5 Tnkak' tjsg
DOS fyj;a Tfns mrs.Klfha Terminal window tl ;=, “hello.exe” file tl open jkq we;' fufia
jkafka Tn ksujQfha DOS ;=, ls%hd;aul jk console jevigykla jk ksidh' fus fmdf;ys bosrshg
yuqfjk ish,qu jevigyka fus jrA.fha console jevigyka jkq we;' oeka Terminal window tflys
“Hello beautiful world!!!” hkak print j ;sfnkq Tn olskq we;'
fus whqrska Tfns m,uq mrs.Kl jevigyk Tn idrA:lj ksulr we;' oeka wm fuu l=vd jevigyk
ksujS we;af;a flfiaoehs i,ld ne,Su wm wrTuq' tys fldgia tlska tl f.k my;ska jsia;r lr we;:
===================
// hello.cpp
===================
by; oelafjkafka Tn ,shQ jevigyfkys by,skau we;s fma,shhs' m,uqfjkau we;s ZZ//ZZ i,l=K
ye`oskafjkafka comment tlla hkqfjkss' wm th C++ l%uf,aLKhla ;=, fhdokafka thska werfTk
fma,sh tu jevigyfkys ls%hdldrS fldgila fkdjsh hq;= jsgoSh' fuksid by; jevigyfkys //
hello.cpp hk fma,sh" jevigyfkys ls%hdldrS fldgila fkdfjs' hello.cpp hkq yqfola wm
fuu jevigyk ,shkq ,nk file tflys kuhs' jevigyk compile lrk wjia:dfjsoS ZZ//ZZ i,l=k
yuqjQ jsg compiler uDoqldx.h tu fma,sh compile fkdlr fkdi,ld yrshs' Tng wjYH kus fuu
fma,sh bj;a lr jevigyk ls%hd;aul l, yelsh' thska lsisu fjkila isoq fkdfjs'
fujeks comments l%uf,aLKhla ;=, fhdokafka fya;= lSmhla u;h' tla m%Odk fya;=jla kus lsishus
l%uf,aLK fldgila u.ska flfrk ldrAhh jdrA:d lr ;enSuh' fuf,i jdrA:d lr ;nkafka" Tn
,shk jevigyk os.= iy ixlSrAK tlla jkafka kus" tys wvx.= lsishus fla; /ilska isoqlrk ldrAhh
ld,h .; jSu;a iu`. Tng wu;l jS hEug ms,sjka fyhsKs' l%uf,aLKhl we;s tjeks ixlSrAK ia:dk
j,oS" tu ia:dkfha wvx.= fla;hkaf.ka isoqjkafka l=ulaoehs comment tlla u.ska f,an,a lr ;enSu
miqjg b;d m%fhdackj;a jkq we;' ;j;a fya;=jla kus" Tn ,shQ l%uf,aLkhla fjk;a l%uf,aLlfhl=
jsiska lshjkq ,nk jsgoS" Tn ,shQ mrs.Kl fla;hkaf.ka isoqjkafka l=ulaoehs Tn tys ;nd we;s
comments wdOdrfhka Tyqg jvd myiqfjka jgyd .; yels jSuh'
======================
#include<iostream.h>
======================
by; jevigyk jeks b;du ir, mrs.Kl jevigykloS mjd jsYd, ldrAhhka m%udKhla Tng
fkdfmfkk f,i compiler uDoqldx.h jsiska bgqlrhs' fus ldrAhhka isoqjkafka “;srhg hgska”h'
fuu ;srhg hgska flfrk ldrAhhka f.ka iuyrla fndfydajsg iEu jevigyklgu fmdoq foah'
WodyrKhla f,i" mrs.Klh u; Odjkh jk iEu jevigyklg u o;a; we;=,a lr.ekSfus yd
msglsrSfus (data input-output) yelshdj w;HjYHh' tksid th iEu jevigyklgu fmdoq fohls'
fusksid mrs.Kl l%uf,aLlfhl=g" fus fmdoq ls%hdldrS;ajhka Tyq ,shk iEu jevigykla ;=,oSu
kej; kej;;a l%uf,aLKh lsrSug isoqfjs' fuh l%uf,aLlfhl=f.a ld,h wmf;a hjhs' fuhg ms,shula
jYfhka" fuu fmdoq jQ ldrAhhka ish,a, l%uf,aLKh lr mqia:ld,hka fyj;a C++ Libraries ;=,
.nvd lr ;sfns' fuksid Visual C++ Ndjs;hg .kakd l%uf,aLlhka yg" iEu mrs.Kl
jevigyklgu fmdoqjQ tu ls%hdldrS;ajhka ;uka jsiska kej; kej; l%uf,aLKh fkdlr Visual C++
Libraries ;=,ska myiqfjka ,nd.; yel' fjk;a wdldrhlg lsh;fyd;a tu libraries ;=, wvx.= jk
mrs.Kl fla;hka wmf.a jevigyka ;=, mdjspspshg .; yelsh'
11
fus wdldrhg" by; #include <iostream.h> hkqfjka oelafjkafka iostream hk Library
tl wm ,shk jevigyklg we;=,a lrk wdldrhhs' iostream hkq input-output stream hkakhs'
jevigyfkys by; fla;hg miqj wvx.= jk cout yd endl hk jsOdk hq., .nvd lr we;af;a
fuu iostream library tl ;=,hs' tu library tl jevigykg we;=,a fkdl,fyd;a" wmg tu
jsOdkhka mdjspspshg .ekSug fkd,efnkq we;'
#include hkak directive tlla f,i ye`oskafjs' #include directive tflys ldrAhh kus wm
,shkd mrs.Kl jevigyklg" msg;ska hus ls%hdldrS;ajhla we;=,a lsrSuh' fuysoS th iostream
library tl jevigyk ;=,g we;=,a lrhs'
===================
void main()
===================
by; oelafjkafka Tng yuqjk m,uqjk C++ function tlhs' Function tlla hkq ir,j lsh;fyd;a"
C++ mrs.Kl fla; reila wvx.= lr ksujk tallhls' C++ jevigykla ls%hd;aul jSu werfTkafka
fuu main() keue;s functions tlskqhs' main() hgska Tn i`., jryka hq.,la olskq we;' lsishus
function tlla ;=, wvx.= jsh hq;= C++ fla;hka wka;rA.; jsh hq;af;a tu function tflys kug miqj
tk by; jeks i`., jryka ;=,h' Functions ms,sn`o jevs jsia;r “Functions” mrspsfpsofhaoS i,ld
n,uq'
=============================================
cout << “Hello beautiful world!!!” << endl;
=============================================
fuu code tl u.ska isoqjkafka “Hello beautiful world!!!” hkak Terminal window tl u;g
output lsrSuhs'
cout << hkq fuu ldrAhh bgqlrkq ,nk jsOdkhhs'
endl << hkq DOS window tl ;=, print jsh hq;= jpk fyda ixLHd fm,la wjidkh jsh hq;= ia:dkh
;SrKh lrk fla;hhs' fuu fla;h wka;rA.; jkafka main() ys i., jryka ;=, jSu ksid" fuh
main function tlg wh;a fla;hla nj Tng jegfykq we;' ;jo Tn olsk fohla jkafka by; fla;h
wjidkfha we;s “ ; ” ,l=K fyj;a Semicolon character tlh' Semicolon tlska olajkafka lsishus
Expression tll wjidkhhs' Expression tlla hkq Operators kuska ye`oskafjk jia;+ka f.ka ieos
mrs.Kl fla;hls' Expressions iy Operators hkq fudkjdoehs Tng bosrsfhaoS bf.kSug ,efnkq we;'
oekg" by; mrs.Kl fla;h expression tlla nj muKla oek.kak' Semicolon tlla u.ska
isoqjkafka by; jeks expression tlla wjikajk ia:dkh oelajSu njo u;l ;nd.kak'
Whitespaces
Whitespaces hkqfjka ye`oskafjkafka mrs.Kl l%uf,aLKhl lsisjla i`oyka fkdfjk ysia bv
m%udKhkah' by; jevigyfkys #include<iostream.h> hkakg;a void main() hkakg;a w;rska we;af;a
whitespace tlls' tfukau { g;a" cout << "Hello beautiful world!!!" << endl; hk fla;hg;a w;rska
we;af;a whitespace tlls' kuq;a we;a; jYfhkau C++ mrs.Kl NdYdj" mrs.Kl l%uf,aLKhl we;s
fuu whitespaces .ek jevs ;elSula fkdlrhs' tkus" Tng wjYH kus lsishus mrs.Kl jevigykl
jevs fldgila lsisu whitspace tllska f;drj jQjo ,sjsh yel' WodyrKhla f,i" by; jevigyfkys
main function tl whitespaces j,ska f;drj ,sjQ fyd;a th my; f,i osiajkq we;:
12
===========================================================
void main() { cout << "Hello beautiful world!!!" << endl;}
===========================================================
by; fla;h foi ne,Sfuka Tng meyeos,sjk fohkus" whitespaces Ndjs;fhka mrs.Kl jevigykl
meyeos,s;djh jevsjk njh' ish,a, tl fma,shlg ,sjSfuka jevigykska isoqfjk foh wmyeos,sjS hhs'
tu ksid mrs.Kl jevigyka ,sjSfusoS Tn iEujsgu iqoqiqf,i whitespaces fhdod.kak'
fyd`ohs" wms by;oS b;du ir, C++ jevigykla ,shkafka flfiaoehs oqgqjd' tu jevigyfkys we;s
m%Odk fldgia ms,sn`oj hus wjfndaOhla Tn ,nkakg we;s'
RAM (Random Access Memory) u;lh
mrs.Kl l%uf,aLlfhl= f,i Tng RAM u;lh ms,sn`o hus fyda wjfndaOhla ;snsh hq;=h'
RAM u;lh mj;skafka Tfns mrs.Klfha RAM psmh ;=,hs' Tfns mrs.Klfhys hus fudfy;l
ls%hd;aul lr we;s jevigyka ish,a,u /`os we;af;a fuu RAM u;lh ;=,hs' tu iEu jevigyklau
RAM ;=, mj;sk u;lfhka" tu jevigykg jsfYaI jQ nhsg Odrs;djla mrsfNdaPkh lrhs' ^nhsgh
(byte) hkq mrs.Kl u;l Odrs;d uksk l=vdu tallh fjs'& WodyrKhla f,i lsishus fudfyd;l Tn
Windows Media Player jevigyk ls%hd;aul lrf.k ix.S;hg ijka fok w;r Internet Explorer
jeks jevigykla Wmfhda.S lrf.k w;a;rAPd,fha iersirKjd hehs o" f;jeks jevigykla f,i
Disk Defragmenter jevigyk miqnsfuys ;u ls%hdfjys ksr; jkjdhehs o is;kak' fuysoS fuu
jevigyka ;=ku RAM u;lfhka ;udg wjYH nhsg fldgi mrsfNdaPkh lruska ta ;=, PSj;a fjhs'
fus wdldrhg" Tn jsiska ,shkq ,nk jevigyklao ls%hd;aul wjia:dfjsoS mj;skafka fuu RAM
u;lh ;=,hs' RAM u;lh “;djld,sl u;lh” f,io ye`oskafjs' thg fya;=j kus RAM u;lh ;=,
Tng lsisjla ia:sr f,i .nvd l, fkdyels jSuh' tys we;s o;a; iqrCIs;j mj;skafka mrs.Klh
Odjkh jk ld,h w;r;=roS muKh' fuhg fya;=j kus" RAM psmfhys o;a; /`oS mj;skafka thg
jsoHq;h iemfhk ;dla l,a muKla jSu fya;=fjks' RAM psmfhka jsoq,sh jsikaOs l, js.iu tys .nvd
jS we;s ish,q o;a; thg wysus jS hhs' Tng ia:sr f,i lsisjla .nvd lr.ekSug wjYH kus th
mrs.Klfha oDV ;egsfhys (Hard Disk) .nvd lrkak' oDV ;egsfhys .nvd lr we;s o;a; jsoq,sh jsikaOs
lsrSulska uelS fkdhkafka tys u;lh /`oS mj;skafka we;af;a jsoHq;fha wdOdrfhka fkdj" pqusNl
n,fhka jSu fya;=fjks'
kuq;a mrs.Klhl u;lh fuf,i RAM u;lh yd ia:sr u;lh fyj;a f,i fowdldrhlska
mj;skafka [email protected] mrs.Klh ls%hd;aul jSfusoS RAM psmh mdjspsps fkdlr oDV ;egsfhysu wjYH o;a;
.nvd fkdlrkafka [email protected] fya;=j kus RAM psmfha ls%hdldrS;ajh oDV ;egsfha ls%hdldrS;ajhg jvd
w;sYhs;a fjs.j;a jSuh' lsishus mrs.Klhla" hus fudfy;l jsjD; jS we;s uDoqldx. j, o;a; ,nd
.ekSfus iy o;a; msglsrSfus (I/O) wjYH;dj oDV ;egsfhys u;lfhka muKla imqrd,Sug ;e;al,fyd;a
tu mrs.Klfha ldrAhCIu;dj ie,lsh hq;= f,i my, niskq we;' fuf,i RAM u;lh mrs.Klhl
mdjspsps jkafka uDoqldx. j,g wjYH fjs.j;aj o;a; iu`. lghq;= lsrSfus wjYH;dj imqrd,Su Wfoidh'
RAM ;=, we;s iEu bit tllgu u;l ,smskhla fyj;a memory address tlla we;' fuu ,smskh u.ska
RAM ;=, iEu bit tllau msysgs ia:dkh oelafjs' Memory address ms,sn`oj fuysoS flgs i`oykla
muKla lrkafka Tng th fuu fmdf;ysoS miqjg yuqjk ksidh' th jevs jYfhka Ndjs;djkq we;af;a
“Pointers” keue;s mrspsfpsofhaoSh' Memory address tlla u.ska wmg lsishus o;a;hla RAM ;=,
ienejskau we;s ia:dkh fidhd.; yel' th yrshgu Tn lsishus ksjila msysgs ia:dkh tys ,smskfhka
fidhd.ekSu jeksh' kuq;a memory address tlla hkq Tn ,shqus ljr u; ,shkq ,nk jsj`o kus iys;
,smskhla jekakla kus fkdjkq we;' Memory address tlla hkq ;kslru ixLHd;aul w.hls' tu
ixLHdjka yeujsgu olajkq ,nkafka 16 jeks mdofha ixLHd f,isks' 16 mdofha ixLHd wm Hexadecimal
numbers hkqfjka y`oqkajuq'
kuq;a memory addresses u.ska bits we;s ia:dk fijSfus wruqK [email protected] tys wruqK kus mrs.Kl
u;lfha we;s o;a; j,g Rcqj we;=,ajSfus wjia:dj ,nd.ekSuh' fus ms,sn`o jevs jsia;r “Pointers”
mrspsfpsofhaoS wms i,ld n,uq'
13
Variables
mrs.Kl jevigykla ksrAudKh lrK jsg" th jsiska hus hus o;a; u;lfha ;nd .ekSfus wjYH;djka
u;=fjs' WodyrKhla f,i" NdId Yn`ofldaIhla f,i ilid we;s uDoqldx.hla .ek is;kak' thg Tn
tla NdIdjlska jpkhla we;=,a l, jsg th tu jpkh wfkla NdIdjg mrsjrA:kh lr Tng ,nd fokq
we;' fuysoS uDoqldx.h Tn ,nd fok jpkh ;u u;lfha ;ndf.k thg iudk jQ wfkla NdIdfjs
jpkh ;SrKh l, hq;=h' “u;lfha ;nd.ekSu” f,i ye`oskafjkafka Tn ,ndfok jpkh" mrs.Kl
u;lfha ;djld,slj .nvd lsrSuhs' fufia .nvd lrkafka mrs.Klfha RAM u;lfhah' kuq;a wm
lsishus o;a;hla RAM ;=, .nvd l, jsg" tu o;a;h;a" fuf;la RAM ys .nvdjS we;s wfkla fndfyda
o;a;hka w;r ;j;a tla o;a;hla muKla njg m;afjs' fufia .nvd flfrk o;a; bkamiqj wmg
kej; m%fhdackh i`oyd ,nd .ekSug isoqfjs' fuu kej; ,nd.ekSu i`oyd wm jsiska wm .nvd l,
o;a;h ksjerosj kej; y|qkd.; hq;=h' kuq;a o;a; id.rh we;s tla l=vd o;a;hla muKla fjkalr
y|qkd.kafka [email protected] fuysoS wmf.a msysgg tkafka fuu variable keue;s tallhs' Variable tlla hkq
wm jsiska RAM ys .nvd lrK ,o lsishus o;a;hla i`oyd wdfrdamKh lrKq ,nk wrA:dkajs; kduhls'
fus u.ska wmg wjYH o;a;h" RAM ;=, wka;rA.; wfkla o;a; w;rska fjkalr y|qkd .; yelsh' th
yrshgu mqoa.,hka rdYshla w;rska" Tng wjYH mqoa.,fhl= Tyqf.a kuska y`oqkd.ekSu jeksh'
mrs.Kl jevigykla ;=, variable tlla iEoSfusoS wm jsiska lreKq 2la iusmqrAK l, hq;=h:
1' m,uqfjkau wm idok variable tflys jrA.h iy kduh compiler uDoqldx.hg y`oqkajd osh hq;=h'
fuh variable declaration f,i ye`oskafjs' fuys wjYH;dj kus" ienE f,iu variable tl ksujSug fmr
thg wjYH jk bv RAM ;=,ska fjkalr .ekSug wjYH jSuhs'
2' bkamiq Tng variable tlg w.hla ,ndoS tys meje;au ienE f,iu ;yjqre l, yelsh' fuh variable
definition f,i ye`oskafjs' wm tu ,nd fok w.h kus" wmg RAM ;=, .nvd lsrSug wjYH o;a;h fjs'
fus ms,sn`o jevs jsia;r uS,. tk" o;a; jrA.hka ms,sn`o mrspsfpsofhaoS i,ld n,uq'
Identifiers
Identifiers hkq" mrs.Kkl jevigykla ;=,oS Tng wjYH o;a; y|qkd.ekSu i`oyd tajd kus lrkjsg"
Tn tu o;a; fj; fokq ,nk kduhkah' by;oS Tn y|qkd.;a variables, mrs.Kl jevigykla ;=,oS
idok jsg" thg imhkq ,nk kduh identifier tlla i`oyd WodyrKhls' kuq;a identifiers f,i
y`oqkajkafka variables muKla fkdfjs'
Expressions
mrs.Kl jevigykloS fkdfhla .Kkhka lsrSug isoqjSu idudkH fohls' fujeks wjia:djka j,oS
expressions fhdod.ekSug wjYHfjs' WodyrKhla f,i my; oelafjkafka identifiers 3 la Ndjs;dlr
,shkq ,enQ expression tlls'
Salary = Basic + Overtime
by; expression tflys m%Odk oE 2 la oelsh yelsh:
1. Operands.
by; expression tflys oelsh yels operands jkafka Salary, Basic iy Overtime hk identifiers h'
2. Operators.
by; expression tflys oelsh yels operators jkafka “ = ” yd “ + ” hk .Ks;uh i,l=Kq hq.,h'
Operators j, ldrAhh kus" expression tlloS" operands Ndjs;d lruska .Ks; lrAuhka isoq lsrSuh'
by;oS" + operator tl u.ska Basic iy Overtime hk operands hq.,fhys w.hka tl;= lrkq ,nk
w;r" = operator tl u.ska tu tl;=fjka ,efnk ms,s;=r Salary kus operand tlg wdfoaY lrkq ,nhs'
kuq;a expressions hkq .Kkh lsrSus muKla fkdfjs' th Ndjs;djk ;j;a wjia:d we;' Expressions
iy operators ms,sn`oj Tng ZZOperatorsZZ mrspsfpsofhaoS jevsjsia;r yuqfjkq we;'
14
Keywords
Keywords hkq mrs.Kl jevigykla ;=,oS jsfYaI wrA:hka iys; kus fyda jpkhs' fusjd C++ NdYdj
u.ska jsfYaI ldrAhhka i`oyd fjkalr we;s jpkhs' Keywords Tng identifiers f,i Ndjs;d lsrSug
fkdyelshs' my; oelafjkafka C++ ys wvx.=jk keywords h' j.=fjs oelafjk we;eus keywords
ms,sn`oj Tng bosrsfhaoS lshjSug ,efnkq we;'
asm
auto
bad_cast
bad_typeid
bool
break
case
catch
char
class
const
const_cast
continue
default
delete
do
double
dynamic_cast
else
enum
except
explicit
extern
false
finally
float
for
friend
goto
if
inline
int
long
mutable
namespace
new
operator
private
protected
public
register
reinterpret_cast return
short
signed
sizeof
static
static_cast
struct
switch
template
this
throw
true
try
type_info
typedef
typeid
typename
union
unsigned
using
virtual
void
volatile
while
15
2. o;a; jrA.hka fyj;a Data types
jevigykl o;a; fyj;a Data .nvd lrkq ,nkafka variables ;=, nj Tn miq.sh
mrspsfpsofhaoS oqgqjd' kuq;a lsishus o;a;hla .nvd lsrSug TskEu variable tlla mdjspsps l, fkdyelsh'
WodyrKhla f,i lsishus flfkl=f.a kula jeks o;a;hla .nvd lsrSu i`oyd iy Tyqf.a Wmka oskh
.nvd lsrSu i`oyd fjkia jrA.hka foll variables folla mdjspsps l, hq;=h' fuhg fya;=j jkafka
ku yd jhi hk o;a; hq., tlsfklg fjkia o;a; jrA. follg wh;a jSuh' fujeks o;a; jrA.
reila C++ ;=, wvx.=h' fu;eka isg oekg Tfns wjYH;djhg irs,k mrsos" fuu o;a; jrA.hkaf.ka
lSmhla muKla meyeos,s lr .ekSug W;aidy .ksuq'
1. int (integer)
int f,i ye`oskafjkafka ixLHd;aul o;a;hkah' lsishus ixLHd;aul w.hla variable tlla ;=, .nvd
lsrSug kus wm int jrA.fha variable tlla iEosh hq;=hs' fuf,i variable tlla idok wdldrh meyeos,s
lr.ekSug wm variable tlla ksujd n,uq' year keue;s kuska int jrA.fha variable tlla my;ska idod
we;s wdldrh n,kak:
=============================================
int year;
=============================================
by;" variable tflys kduhg(year)bosrsfhka we;s int keue;s fldgiska oelafjkafka wm
idok variable tl int jrA.hg wh;a njhs' fuysoS wm lrkafka year keue;s variable i`oyd
mrs.Klfha RAM u;lfhka fldgila fjka lsrSu muKhs' fuf,i variable tlla iEoSfusoS wm
m,uqfjka tu variable tlg wod, fldgi fyj;a nhsg ixLHdj RAM u;lfhka fjkalr .; hq;=hs'
fuu ls%hdj variable declaration f,i ye`oskafjs' th yrshgu lsishus f.dvke.s,a,la bos lsrSug fmr"
th boslsrSu i`oyd iEfyk N~us m%udKhla fjka lr.ekSu yd iudkh' RAM u;lfhka fjkalrk
fuu nhsg fyj;a u;l m%udKh" idok variable tflys o;a; jrA.h wkqj fjkia fjs - Tn ;ekSug
kshus; f.dvke.s,af,a jsYd,;ajh wkqj" N~usfhys jsYd,;ajh ;SrKh l, hq;=h' fuf,i int variable
tlla ksmojSfusoS RAM u;lfhka fjka jk bv jkafka nhsg 4 ls'
oeka wm fuu year keue;s variable tl i`oyd wjYH u;lh fjkalr .;a ksid" wmg oeka th ;=,
ixLHd;aul w.hla ;ekam;a l, yel - Tng wjYH N~us m%udKh fjkalr .;a miq" f.dvke.s,a,
bosl, yelsh' fuh variable definition f,i ye`oskafjs' tkus fuysoS isoqjkafka variable tlg w.hla
wdfoaY lr tys meje;au RAM ;=, ia:sr lsrSuhs' th isoqlrkafka my; whqrsks:
=============================================
year = 2010;
=============================================
oeka year keue;s variable tflys w.h 2010 fjs' fjk;a jpk j,ska lsh;fyd;a year keue;s variable
tl RAM u;lh ;=, fjkalrf.k we;s bv m%udKfhys" tkus nhsg 4 la ;=," 2010 keue;s ixLHdj
16
;ekam;a jS we;' wm fuysoS variable declaration iy variable definition hk ls%hd fol wjia:d folloS
isoql,;a" myiqj msKsi fus folu tljsg my; mrsos isoql, yel:
==============================================
int year = 2010;
==============================================
wm oeka int variable tlla fhdodf.k ir, mrs.Kl jevigykla ,shd n,uq:
======================================================
// ints.cpp
#include<iostream.h>
void main()
{
int year = 2010;
cout << “Days in a year = “ << year << endl;
}
======================================================
by; jevigyk Visual C++ u.ska fmr mrosu Compile lr Run lrkak' Terminal window tflys
“Days in a year = 2010” hkak print fjkq we;' fuysoS isoqjQfha [email protected] m,uqj wm 2010 w.h year keue;s
variable ;=, .nvd lf,uq' bkamiqj cout << fla;h" year ;=, we;s w.h" tkus 2010" Terminal
window tl u;g output lf,ah'
Scope
===================================================
#include<iostream.h>
void main()
{
int x = 10;
{
int y = 20;
cout<< "Inside x:" << x << endl;
cout<< "Outside y:" << y << endl;
}
cout<< "Outside x:" << x << endl;
cout<< "Outside y:" << y << endl; // error
}
===================================================
by; jevigyk Visual C++ ;=, h;=re ,shkh fldg compile lsrSug W;aidy lrkak - Tng th
compile lsrSug fkdyelsjkq we;' isoqjk tlu foh jkq we;af;a Visual C++ ys output window tl
;=,g my; oelafjk error message tl print jSuh' ^Tng ,efnk error massage tflys file path tl
fjkiajkq we;&
17
^?mh 2'1&
oeka Tn output window tflys fuu error message tl u; double click l, hq;=h' ^by, isg we;s 4
jeks fma,sh u; double click lrkak& tjsg editor window tflys fuu error tl ksmojSug mdoljqKq
fla;h highlight jkq we;' tu fla;h kus 4 jeks cout jsOdkh wvx.= fla;h nj Tng highlight jS
fmfkkq we;' Error tlska lshfjkafka y hkq declare fkdlrk ,o wd.ka;=l variable tlla njhs'
fufiajkafka [email protected] Tn y declare lr tys w.h 20 njg define o lr we;=jd [email protected] kuq;a Tn
kej; jrla jevigyk foi ne,qjfyd;a fmfkk fohla jkafka" y ys declaration tl we;af;a i`.,
jryka hq.,la ;=, njhs' Error tl ksmojQ cout wvx.= fla;h we;af;a fuu jryka j,ska msg;hs' fuu
fla;h u.ska y j b,a,d we; ^fyj;a y j refer lr we;&' kuq;a compiler uDoqldx.hg y kuska identifier
tlla fidhd .ekSug fkdyelsjQ ksid th by; oelajqKq error tl ksmojQfhah' Compiler uDoqldx.hg y
y`oqkd.ekSug fkdyelsjQfha y ys definition tl we;af;a" we;=,; i`., jryka hq., ;=, jSu ksihs'
fuysoS y ys meje;au fuu jryka ;=,g muKla iSudfjhs' jryka j,ska msg; isg thg by;oS fuka
refer lrkq ,enqjfyd;a compiler uDoqldx.hg th y|qkd.ekSug fkdyelsj hhs' jryka ;=,oS kus th
ksielju y y`oqkd.kshs' i`., jryka ;=, we;s fojeks cout command tl error tlla ksmofkdjSu
fya;=fjka fus nj ;yjqre fjhs' oeka error tl iys; fla;h jevigykska bj;a fldg fyda th
comment fldg ^tu fma,sh uq,g “ // “ i,l=Kla fhdod& kej; jevigyk compile lsrSug W;aidy
lrkak' tjsg th kshudldrj th compile jk whqre Tn olskq we;'
fuf,i variable tlla i`., jryka hq.,la ;=, define lsrSfuka tys meje;au tu i`., jryka ;=,g
muKla isud lsrSu ye`oskafjkafka “Scoping” hkqfjks' jryka fol ;=, we;s bv tu variable tflys
scope tl f,i ye`oskafjhs' tu variable tl refer l,yelafla fuu scope tl ;=,oS muKs'
2. char (Character)
char – o;a; jrA.fhka hq;= variable tll wk;rA.; l, yels jkafka mrs.Kl h;=re moaO;sh ;=, we;s
tla wl=rla" b,lalula fyda i,l=Kla fyj;a character tlls'
WodyrK - 'G’, ‘k’, ‘8’, ‘2’, ‘>’, ‘?’
by; WodyrK j, we;s “ ‘ “ i,l=Kq fyj;a single quotes foi n,kak' wm char w.hla y|qkd.kafka
fus single quotes j,sks' fuysoS ‘8’ iy ‘2’ hkq b,lalus fkdfjs' fufia jkafka tajd single quotes
j,ska wdjrKh fldg we;s ksidh' tjsg tajd b,lalus fkdj' characters njg m;a fjs' fuf,iu ‘>’,
‘?’ jeks i,l=Kqo fuysoS characters f,i ie,fla'
oeka wm char mdjspsps lr ,shQ jevigykla foi wjOdkh fhduq lruq:
======================================================
// characters.cpp
#include <iostream.h>
void main()
{
char x = ‘b’, y = ‘y’, s = ‘e’;
cout<< x << y << s << endl;
18
}
======================================================
by; jevigyk compile lr run lrkak' “bye” hk jpkh console window tl ;=, print fjkq we;'
“bye” hk jpkh ieoqfka ‘b’, ‘y’, ‘e’ hk characters ;s%;ajh tl fm,g fjkfjku print jSfuka nj
Tng meyeos,s jkq we;'
by; variables, define lr we;s wdldrh foi n,kak' tu variables ;=ku define lr we;af;a tlajru'
tla fm,lg nj Tng fmfka' variables fjka lr we;af;a fldud j,sks' fuh Tn fmroS oqgq variables
define lsrSug iudkhs' tkus" by; define lsrSu u my; wdldrfhka o isoql, yel:
===============================
char x = ‘b’;
char y = ‘y’;
char s = ‘e’;
===============================
3. bool (Boolean)
lsisfjl= oeka Tfnka my; m%YaKh wihs:
“oeka Tfns ldurfha j`oq,s n,anh we;af;a oe,aj`[email protected]”
Tfns ms,s;=r jsh hq;af;a “Tjq” fyda “keye” hk folska tlls' tkus" “oeka Tfns ldurfha we;s j`oq,s
n,anh oe,ajS we;” hk m%ldYkh i;H fyda wi;H jsh yelsh' fjk;a wdldrhlska lsh;fyd;a tu
m%ldYkh i;H iy wi;H hk wjia:d folska hqla;h' mrs.Kl jevigykl fufia wjia:d follska
muKla iukajs; foa oelajSug fuu bool keue;s data type tl Ndjs;dfjs' bool – data type tlska hq;=
variable tllg ;snsh yelafla w.hka folls' tkus" i;H wjia:dj iy wi;H wjia:dj. mrs.Kl
jevigyka ,sjSfusoS fuu i;H wjia:dj true hk keyword tlska fyda 1 fyda 0 g jevs TSkEu
ixLHdjlska olajk w;r wi;H wjia:dj olajkafka false hk keyword tlska fyda 0 w.h u.sks'
mrs.Kl jevigykloS bool data type tl mdjspsps flfrk wjia:djla jkafka" jevigyk ;=, lsishus
;;ajhla iemsrS we;s fyda ke;s nj ;SrKh lsrSugh' fuh jgyd .ekSu i`oyd wm by; j`oq,s n,an
WodyrKh mrs.Kl jevigyklg fhdod n,uq:
=====================================================
// bool.cpp
#include <iostream.h>
void main()
{
bool on = true;
if(on){
cout<< “The bulb is on” << endl;
}
else{
cout<< “the bulb is off” << endl;
}
}
======================================================
by; jevigyfkys Tng w,q;a fohla jk if-else statement tl yuqfjs' if-else statement tl .ek wm
u`ola miqj l:d lruq' oekg Tn fus jevigyfkys wjOdkh l, hq;af;a bool – variable tlla jk on
fj; iy thg wdfoaY lr we;s w.h jk true w.h fj;h' jevigyfkys m,uqj isoqjkafka on keue;s
bool o;a; jrA.hg wh;a variable tlla define jS thg true w.h wdfoaY jSuh' bkamiq" Tn ;ju
y|qkdf.k ke;s if-else statement tl ls%hd;aul jSfuka on ys w.h l=ulaoehs mrSCIdjg ,lafjs' tys
19
w.h true jqjfyd;a" “The bulb is on” hk jpk fm, print jk w;r w.h false jqjfyd;a" “The
bulb is off” hkak print fjs' jevigyk compile lr run lr n,kak' on ys w.h true ksid “The
bulb is on” hkak print fjkq we;' oeka on fj; false w.h wdfoaY lr (true fjkqjg on bosrsfha
false hkak type lrkak) kej; jevigyk run lr n,kak' tjsg print jkq we;af;a “The bulb is
off” hkakhs' ta wkqj fuu jevigykska oelafjkafka bool – variable tlla mrs.Kl jevigykloS
m%fhdackhg .; yels wjia:djlah' on ys w.h true fyda false jSu u; jevigyfkys ls%hdldrS;ajh
fowdldrhla .;af;ah' fjk;a jpk j,ska lsjfyd;a wmg jevigykla ls%hd;aul jsh hq;= udrA.h
l=ulaoehs th Odjkh jk fudfydf;aoS fujeks wdldrhkaf.ka ;SrKh l, yelsh'
4. float
wm int – data type .ek l:d lsrSfusoS i,ld ne,qfjs mQrAK ixLHd ms<sn`oj muKla nj Tn [email protected] int –
variables ;=, .nvd l, yelafla mQrAK ixLHdjka muKs' WodyrKhla f,i 10.5 jeks oYu ixLHd tysoS
Ndjs;d l, fkdyel' float o;a; jrA.h we;af;a fuu wjYH;dj imqrd,Su i`oydh' my; oelafjkafka
float Ndjs;d l, jevigykls:
======================================================
// floats.cpp
#include<iostream.h>
void main()
{
float time = 10.35;
cout << “The time is: “ << time << endl;
}
======================================================
5. string
fuu o;a; jrA.h Ndjs;d lrkafka jpk fyda jdlH .Kfha o;a; iu. lghq;= lsrSughs' wm by;
idlpsPd lrk ,o ish,qu o;a; jrA.hka C++ ;=, iajNdjslu wvx.=jk o;a; jrA.hkah' kuq;a fuu
string o;a; jrA.h C++ ;=,g lD;s%uj tlal, fohlehs lsjyelsh' tfukau strings ms,sn`oj i,ld
ne,Sug wfk;a data types j,g jvd jevs lreKq .Kkdjla we;' fusksid fuu jsia;r .ek fu;ekoS
l:d fkdlr ta i`oyd fjku fldgila fuu fmdf;ys “C++ Libraries” keue;s mrspsfpsofhaoS fjka lr
we;' kuq;a oekg string – data type tl hkq l=ulaoehs uQ,sl f,i my; jevigykska meyeos,s
lr.kak:
======================================================
// strings.cpp
#include<iostream.h>
#include<string>
using namespace std;
void main()
{
string message = "Hello there! I'm learning C++.";
}
======================================================
by; jevigyfkys message keue;s string – variable tlla define lr we;s w;r thg flgs jdlHhla
wdfoaY lr we;'
20
3' Operators
fuf;la Tn i,ld ne,Q mrs.Kl jevigyka j, Tn mdjspsps l, variables foi n,kak' Tn
tajd define lr tajd i`oyd w.hka wdfoaY lsrSfuka wk;=rej tajdfha w.hka fkdfjkiaj mej;=kd
[email protected] kuq;a ienE mrs.Kl jevigykloS Tn mdjpsps lrk o;a; fjkialus j,g ,la lsrSug isoqjSu
ks;r isoqjk lreKls' WoyrKhla f,i Tn Tng wjYH B-fus,a ,smskhka .nvd lr ;nd .ekSu i`oyd
uDoqldx.hla ksujSfus fhfok nj is;kak' tys .nvd lr we;s ,smskhka ixLHdj jdrA:dlr ;enSug int
– variable tlla Tn mdjspsps lr;ehs o is;kak' uDoqldx.hg wvx.= lrK ,o ,smskhka ixLHdj
jevsj;au Tn fuu variable tflys w.ho tu ,smskhka ixLHdj wkqj by, kexjsh hq;=hs' fus wdldrhg
jevigykl o;a;hka fjkia lsrSug isoqjk jsg wm operators m%fhdackhg .ksuq'
Operators Ndjs;fhka o;a; tl;= lsrSu" wvq lsrSu" .=K lsrSu iy fnoSu ke;fyd;a lsishus o;a;hka
hq.,la fyda lSmhla iudk w.hla .k`oehs mrSCId lsrSu jeks fkdfhla ls%hdjka isoqlsrSug yelsh'
Operators u.ska fuu ls%hd isoqlsrSug kus wm operators wka;rA.; lr expressions ,sjsh hq;=h'
Expression tlla hkq l=ulaoehs by;oS b;d flgsfhka Tn uSg by;oS y`oqkd.;a;d' Operators wvx.=
lrK ,o expression tlla ljrdldroehs ne,Sug m%:ufhka wm operators hkq fudkjdoehs W.; hq;=h'
C++ ;=, operators reila yuqfjs' my; oelafjk igykska tu operators jevs yrshla oelafjs'
jrA.h
Arithmatic operators
Logical operators
Relational operators
Increment &
Decrement
Operators
Assignment
Operators
ku
Multiplication
Division
Modulus
Addition
Subtraction
Logical AND
Logical OR
Unray NOT
Greater than
Less than
Greater than or
Equal to
Less than or
Equal to
Equal to
Not equal to
Increment
ixfLs;h
*
/
%
+
&&
||
!
>
<
Decrement
--
Assignment
=
Addition
+=
>=
<=
==
!=
++
21
Assignment
Subtraction
Assignment
Multiplication
Assignment
Division
Assignment
-=
*=
/=
Tng fmfkk mrsos operators ie,lsh hq;= ;rus jsYd, ixLHdjla we;;a" we;af;kau fusjd ms,sn`oj
bf.kSu t;rus wmyiq ke;' fu;eka isg fuu operators .ek jsuid n,uq'
Arithmatic operators
Arithmatic .Kfha Operators u.ska isoqjkafka tl;= lsrSu" .=K lsrSu jeks idudkH .Ks; lrAuhkah'
my; oelafjkafka Arithmatic operators mdjspsps lr ,shkq ,enQ ir, jevigykls' Expression tlla
hkq l=ulaoehs hk lreKo Tn fuu jevigykska olskq we;:
===========================================================
// ArithmaticOps.cpp
#include<iostream.h>
void main()
{
int toShop = 500, backHome = 500, distance;
distance = toShop + backHome;
cout << "The distance I walked: " << distance << "
meters"
<< endl;
int time = 50, shoppingTime = 30, walkingTime,
speed;
walkingTime = time - shoppingTime;
speed = (toShop + backHome) / walkingTime;
cout << "My average speed was: " << speed << "
meters per minute" << endl;
int weekShoppingTimes = 3, allTime;
allTime = time * weekShoppingTimes;
cout << "Time I spent on shopping for a week: " <<
allTime
<< " minutes" << endl;
}
===========================================================
^by; jevigyfkysoS yd bosrshg yuqjSug kshus; jevigyka j,oS we;eus mrs.Kl fla;hka tl
fm,la f,i oelajSu i`oyd bv uosjk ksid tu fla;hka fma,s folla jYfhka olajd we;' [fuu ns`oqkq
fla;hka jla B igyka j,ska oelafjs] kuq;a Tn Visual C++ ys fuu jevigyka h;=re ,shkh lrk
jsg tu fma,s hq.,hka tlu fma,s f,i type lrkak' fkdtafiakus fndfydajsg Tfns jevigyka j,
errors u;=fjkq we;&
by; jevigyfkka fmkakqus lrkafka lsishus mqo.
a ,fhl= lvfmd,g f.dia nvq us,oS .ekSfusoS .;
l, ld,h yd Tyq .uka l, oqr wdoshhs' jevigyfkys m,uqfjka isoqlr we;af;a mqoa.,hdf.a ksjfia
isg lvfmd,g;a" tys isg ksjig;a we;s oqr o" uq,q oqr o jdrA:d lr ;enSu i`oyd ms,sfj,ska toShop,
backHome iy distance hk int - variables ;s%;ajh ;ekSuhs' toShop iy backHome fj; 500 wdfoaY
lr we;' 500 w.h uSgrA f,i i,lkak' B,. fma,sfhka Tn olskafka addition operator (+) tl o
22
toShop yd backHome variables (fyj;a operands) fol o Ndjs;d fldg ,shkq ,enQ expression tlls'
Expression hkq l=ulaoehs uQ,sl jYfhka Tn 1jeks mrspsfpsofhaoS y`oqkd.;a;d' fuu expression tflys
ldrAhh jkafka toShop yd backHome hk variables fol tl;= lr uq,q oqr" distance variable ;=,
.nvd lsrSuhs'
bkamiqj we;s fla;fhka distance ;=, .nvd jQ tu w.h console window tl u;g output fjs'
fojeks fldgfiaoS" lvfmd,g f.dia meusKsfusoS .;jQ uq,q ld,h jdrA:d lr ;enSug time o nvq us,oS
.ekSug .;jQ ld,h oelajSug shoppingTime o lvfmd,g yd wdmiq ksjig wejsOsug .;jQ ld,h
oelajSug walkingTime o mqoa.,hdf.a wejsOsfus fjs.h .nvd lsrSug speed o hk variables /i idod
we;' tysoS time fj; 50 o" shoppingTime fj; 30 o wdfoaY lr we;' 50 yd 30 w.hka jskdvs f,i
i,lkak' oeka isoqjkafka lvfmd,g hdug yd taug .;jQ uq,q ld,h ,nd.ekSug" time ys w.fhka
shoppingTime ys w.h subtraction operator (-) Ndjs;d lr wvq lsrSuh' th walkingTime ;=, .nvd
lr we;' bkamiq mqoa.,hdf.a idudkHh wejsOsfus fjs.h fidhd tu w.h speed ;=, .nvd lr we;'
th isoqlr we;af;a division operator (/) Ndjs;d lr" lvfmd,g yd wdmiq ksjig we;s oqr" mqoa.,hdf.a
wejsoSfus ld,fhka fnoSu i`oyd expression tlla ,sjSfuks'
f;jeks fldgiska weekShoppingTimes iy allTime hk variables hq., define lr we;af;a
ms,sfj,ska i;shlg lvfmd,g .sh jdr .Kk iy ta i`oyd i;shloS .;l, uq,q ld,h jdrA:d lsrSugh'
wjidkfhaoS multiplication operator (*) Ndjs;d lr tla jrla lvfmd,g hEu i`oyd .; jQ ld,h
i;shlg .sh jdr .Kska .=Klr uq,q ld,h ,nd f.k we;'
fuu jevigyfkaoS +, -, /, * hk operators y;ru Ndjs;d jQ w;r tys lsjhq;= fohlg we;af;a
multiplication operator (*) .ek muKhs' tkus" Tn fuf;la .=K lsrSu i`oyd mdjspsps l, x ,l=K
fuysoS Asterix - * ,l=K njg m;ajS we;' ;jo" jevigyfkys 2 jeks fldgfia iq,q lsrSfusoS jryka
mdjspsps lr we;s wdldrh n,kak' Tn oekgu;a fndfyda jsg oek isgsh yels mrsos wm iq,q lsrSuloS
jryka fhdokafka tu jryka ;=, we;s fldgi fjku tallhla f,i m%:ufhka iq,q lr.ekSug wjYH
jQ jsgh' tkus fuysoS m%:ufhka toShop yd backHome ys w.hka tl;= jS" bkamiqjhs tu w.h
walkingTime w.fhka fnfokafka' fujeks wjia:d j,oS jryka Ndjs;d lsrSu hym;a mqreoaols' tu.ska
Tng wjYH iq,qjSu ksjeros f,i isoqjk njg Tng ;yjqre lr .; yel'
oeka b;srsj we;af;a Modulus operator (%) .ek idlpsPd lsrSughs' my; jevigyk mrSCId lr
n,kak:
======================================================
// Modular.cpp
#include<iostream.h>
void main()
{
int aNumber = 27;
int mod = aNumber % 2;
cout << mod << endl;
int anotherNumber = 65;
mod = anotherNumber % 5;
cout << mod << endl;
}
======================================================
by; jevigyk ls%hd;aul lr n,kak' m,fjks yd fojeks wjia:d j,oS mod ys w.h f,i ms,sfj,ska
1 yd 0 console window tl ;=, print fjs' m,uq wjia:dfjsoS mod ys w.h 1 jQfha fufiahs: modular
operator (%) u.ska isoqjkafka lsishus ixLHdjla fnoSfuka miq ,efnk b;srsh .Kkh lsrSuhs' fus
wkqj aNumber hk variable tflys w.h jk 27" 2 ka fnod ,efnk b;srsh mod ;=, ;ekam;a lr we;'
27" 2 ka fnoQ jsg 13 o b;srsh 1 o fjs' fusksid m,uq wjia:dfjsoS mod = 1 fjs' 2 jk wjia:dfjsoS;a
isoqkafka fuhuhs' tho mrSCId lr n,kak'
23
Logical operators
wm Logical Operators Ndjs;d lrkafka" tys kuska u lshfjk mrsos" ;rAldkql=, ls%hdjka isoqlsrSu
i`oydh' fuu Logical operators u.ska isoqjkafka wjia:djka lsysmhla f.k tu wjia:djka w;r
;rAldKql=, iusnkaOhla f.dvke.Suh' fus iusnkaOh we;s jkafka tu wjia:djkaf.a i;H-wi;H
Ndjhka w;rhs' fuh jgyd .ekSug wm WodyrKhla f.k n,uq'
oeka Tn" Tn bosrsfha isgskd A" B yd C keue;s ,uqka ;sfofkl= is;ska ujd .kak' A f.a ysifla oqTqre
meye nj Tng fmfkk w;r weia ks,a meyefhka hq;=h' B f.a ysifla l,q meye .kakd w;r wef.a
weia oqTqre meye;sh' C r;= meye ysifliska hq;= jk w;r oEia ld, jrAKh' fus fudfydf;a lsisfjl=
meusK Tng fufia lshhs: “A f.a ysiflia oqUqre meyefhka hq;=hs' weia ks,a mdghs' B g we;af;a oqUqre
meye;s ysifliah' kuq;a weia l,q mdghs' C f.a ysiflia r;= mdghs' wEg we;af;a l,q meye fofk;ah'”
fuu mqoa.,hd f.a lshuk i,ld n,kak' th iusmqrAK f,i i;H fkdjk nj Tng fmfkajs' Tyq
m,uqfjks yd wjika wjia:d j,oS i;H m%ldY lrk kuq;a fojeks wjia:dfjsoS wi;H m%ldY lrhs'
flfia jqjo Tyq jevs fldgila lshd we;af;a i;Hhhs' tfiakus" Tn ;SrKh lrkafka Tyq i;H m%ldY
l, njo wi;H m%ldY l, [email protected] Tyq ;sfokd ms,sn`oju i;H lshd we;akus Tng Tyq i;H mjid
we;ehs ks.ukh l, yel' Tyq ;sfokd ms,sn`oju wi;H foau lshd we;kus Tng Tyq mjid we;af;a
wi;H nj ;SrKh l, yel' kuq;a fus wjia:dfjoS fus folska tlla j;a Tng ks.ukh l,
fkdyelsh' mrs.Kl jevigyka j,oSo fujeks wjia:d u;=fjs' tkus Tng wjia:d lSmhla i,ld n,d
tajdfha i;H-wi;H ;dj wkqj hus hus ;SrK j,g t<TSug isoqfjs' we;eus wjia:d j,oS ish,a, i;H
fjs' ;j;a wjia:djloS ish,a, wi;H fjs' kuq;a we;eus wjia:d j,oS iuyrla foa muKla i;H fjs'
fujeks wjia:djka y|qkd .ekSfusoShs wmg Logical Operators jeo.;a jkafka'
uska by;oS wm i,ld ne,Q Arithmatic Operators ieujsgu ms,s;=r f,i b;srs lf,a jsj`o ixLHd;aul
w.hkah' kuq;a Logical Operators ms,s;=r f,i ,nd fokafka 1 fyda 0 hk w.hka hq., muKh' fuysoS
1 w.h true ^i;H& keue;s w.hg wkqrEm jk w;r 0 w.h false ^wi;H& keue;s w.hg wkqrEm fjs'
fuu true iy false w.hka Boolean data type tlg wdfjsKsl jQ w.hka nj Tn data types mrspsfpsofhaoS
y`oqkd.;a;d u;l we;s'
wm oeka Logical Operators Ndjs;d lr by; A, B, iy C ;sfokd ms,sn`o WodyrKh jsj`o whqrska
mrs.Kl jevigyklg wdfoaY lr we;s wdldrh n,uq'
Logical AND (&&)
Logical AND ys ldrAhh kus wjia:djka lSmhla f.k tu wjia:d ish,a,u i;H kus muKla true w.h
ksmojSuh' tu tla wjia:djla jqjo wi;H fjs kus Logical AND ksmojkafka false w.hhs' fuh
isoqjkafka my; wdldrfhks:
======================================================
// LogicAnd.cpp
#include<iostream.h>
#include<string>
using namespace std;
void main()
{
string aHair = “Brown”, aEyes = “Blue”, bHair = “Black”,
bEyes = “Brown”, cHair = “Red”, cEyes = “Black”;
24
// (1)
bool hairCheck = (aHair == “Brown”);
cout << hairCheck << endl;
// (2)
hairCheck = (aHair == “Brown”) && (bHair == “Red”);
cout << hairCheck << endl;
// (3)
bool eyesCheck = (aEyes == “Blue”) && (bEyes == “Brown”)
&& (cEyes == “Black”);
cout << eyesCheck << endl;
}
======================================================
by; jevigyfkys oekg Tn fkdokakd Equal to (==) operator tl" expressions i`oyd mdjspsps lr
;sfns' Equal to (==) operator .ek oekg fus wdldrfhka is;kak: Equal to (==) operator tl Tn
oekgu;a okakd Assignment operator (=) tlg jvd fjkia ldrAhhla bgq lrhs' = u.ska isoqjkafka
lssishus operands hq.,la iudk lsrSu jQ w;r" == u.ska isoqjkafka lsishus operands hq.,la iudk oehs
mrsCId lsrSuhs' tu operands hq.f,ys w.hka iudk kus 1 fyj;a true w.ho" wiudk kus 0 fyj;a
false w.ho == u.ska ksmojhs' tfiakus by; jevigyfkys 1 jeks fldgfiys we;s aHair ==
“Brown” hk expression tlska isoqjkafka aHair keue;s operand tflys w.h jkafka “Brown” oehs
mrsCId lsrSuhs' aHair fj; “Brown” hk w.h by;ska assign lr we;s ksid fuysoS == u.ska true fyj;a
1 ksmofjhs' ksmofjk fuu w.h hairCheck keue;s bool type variable tl ;=, .nvd lr we;s w;r"
jevigyk ls%hd;aul lsrSfusoS hairCheck ys w.h f,i 1 print fjkq we;'
oeka 2 jk fldgi foi n,kak' tysoS Logical AND - && mdjspsps lr we;' Tn fudfyd;lg fmr oqgq
mrsos aHair == “Brown” hkak true w.h ksmojhs' (bHair == “Red”) hk expression ksmojkafka
false w.hhs' ulaksidoh;a bHair ys w.h “Black” jk f,i th define lr we;s ksidh' oeka fuu
expressions hq., && u.ska iusnkaO lr we;' fujsg isoqjkafka [email protected] isoqjkafka tu expressions
u.ska ksmojqkq true iy false w.hka && u.ska mrSCIdjg ,la jSuh' by; i`oykajQ mrsos && u.ska" th
mrSCIdjg ,la flfrk wjia:d ish,a, true fyj;a i;H w.hla .kS kus true w.h ksmojhs' fuysoS
mrSCIdjg ,lafjk m,fjks expression tl jk (aHair == “Brown”) ys w.h true jk w;r fojk
expression tl jk (bHair == “Red”) ys w.h false fjs' tfiakus" (aHair == “Brown”) &&
(bHair == “Red”) hk expression tl u.ska ksmofjkafka false w.hhs' false w.h hairCheck ;=,
.nvd flfrk w;r false w.hg wkqrEm 0 w.h print fjs' fuh yrshgu Tng by; oS yuqjQ mqoa.,hd
fufia lSu jeksh: “A g we;af;a oqTre ysiflah' B f.a ysifla r;=h'“ Tyq fojeks wjia:dfjsoS wi;H
m%ldY lrhs' fus ksid && u.ska false ksmofjs'
oeka f;jeks fldgig' (aEyes == “Blue”) hk expression tl ksmojkafka true w.hh' (bEyes
== “Brown”) ksmojkafka o true w.hh' (cEyes == “Black”) o true w.h ksmojhs' ish,af,ysu w.h
true fj' fuksid && u.ska ksmofjkafka o true w.hhs' fuksid true w.hg wkqrEm 1 w.h print fjs'
uS,.g wm bf.kSug hkafka Logical OR (||) operator tl .ekhs'
Logical OR (||)
Logical OR u.ska bgqjkafka wjia:d folla mrSCId lr" tu wjia:d folska tlla fyda i;H kus true
w.h ksmojSuhs' my; WodyrKfhka fuh jsia;r fjs:
25
===========================================================
// LogicOr.cpp
#include<iostream.h>
#include<string>
using namespace std;
void main()
{
string aHair = “Brown”, aEyes = “Blue”, bHair = “Black”,
bEyes = “Brown”, cHair = “Red”, cEyes = “Black”;
// (1)
bool hairCheck = (aHair == “Brown”) || (cHair == “Red”);
cout << hairCheck << endl;
// (2)
bool eyesCheck = (aEyes == “Black”) || (bEyes == “Brown”)
|| (cEyes == “Blue”);
cout << eyesCheck << endl;
// (3)
hairCheck = (aHair == “Black”) || (bHair == “Red”)
|| (cHair == “Brown”);
cout << hairCheck << endl;
}
===========================================================
by; ^1& wjia:dfjsoS hairCheck ys w.h true fjs' ulaksido h;a (aHair == “Brown”) yd (cHair ==
“Red”) hk wjia:d folu true w.hka .kakd fyhsks'
^2& wjia:dfjsoS (bEyes == “Brown”) yefrkakg wfkla expressions wi;H h' kuq;a eyesCheck ys
w.h true njg m;afjs' fuh isoqjkakg fya;=j kus" || u.ska true ksmojSug" th mrSCId lrk wjia:d
tlla fyda i;H jSu m%udKj;a jSuh'
^3& wjia:dfjsoS ish,qu expressions, false w.h .ekSu ksid || u.ska false w.h ksmojhs' || u.ska true
ksmojSug kus th mrSCId lrk wjia:d tlla fyda i;H jSu w;HjYH jSu ksid fuh isoqfjs'
Logical NOT(!)
Tng by;oS yuqjQ mqoa.,hd oeka C fj; meusK fufia lsh;ehs is;kak: “Tfns weia ks,a mdghs!” fuysoS
C l=ula is;=jo fuh weiQ jsgu Tng jegfykq we;af;a fuu mqoa.,hd C g wi;H foa m%ldY lrk njhs'
ulaksidoh;a C f.a weia ks,a mdg fkdj ld,jrAK nj Tng fmfkk fyhsks' mqoa.,hdf.a wi;H
m%ldYh expression tlla u.ska fufia ,sjsh yel:
cEyes == “Blue”
oeka lsisfjl= Tfnka fuu m%YaKh wihs: “Tyq C .ek lshQ foh wi;[email protected]”' Tfns ms,s;=r jkafka
“Tjs” hkakhs' tysoS tu m%YaKh wdid we;s wdldrhg ms,s;=re iemhSug Tn f,i ;SrKh l, hq;=
jkafka mqoa.,hdf.a m%ldYKh wi;Ho hk j.hs' Tng by; yuqjQ Logical operators u.ska isoqlf,a
lsishus fohla i;H oehs fidhd ne,Suhs' kuq;a by; mrsos lsisjla wi;H oehs mrSCId lsrSug kus
Logical NOT ys iyh wm ,nd.; hq;=h' Logical NOT u.ska isoqjkafka lsishus wjia:djla wi;H kus
true w.h ksmojSuhs' fuh my; jevigyfkys oelafjs:
26
===========================================================
// LogicNot.cpp
#include<iostream.h>
#include<string>
using namespace std;
void main()
{
string aHair = “Brown”, aEyes = “Blue”, bHair = “Black”,
bEyes = “Brown”, cHair = “Red”, cEyes = “Black”;
bool cEyesCheck =!(cEyes == “Blue”);
cout << cEyesCheck << endl;
}
===========================================================
by; jevigyfkys cEyes == “Blue” hk expression tl u.ska false ksmojqkfyd;a ! – operator tl
u.ska true ksmofjkq we;' Expression tl true jqjfyd;a false w.h ksmofjs' fusksid cEyesCheck ys
w.h by;oS true w.hla .kq we;'
Relational operators
C wfkla fofokdgu jvd Wiska jevs" weh High heels me,`o f.k isgsk ksid nj Tn ksrSCIKh
lrhs' A wfkla fofokdgu jvd Wiska wvqh' Tn oeka fufia lsjfyd;a th i;H jkq we;: “A, B g
jvd Wiska wvqhs”' fufia lsjfyd;a th wi;H jkq we;: “C, B g jvd Wiska wvqj fmfkhs”. Tn fuysoS
isoq lrkafka fus ;sfokdf.a Wi j, wkqmd;slhka w;r iusnkaOhla (Relation) f.dvke.Suhs'
Relational operators j, ldrAhh jkafkao fujeks wjia:d w;r wkqmd;sl iusnkaOhla we;s lsrSuh'
Greater than operator (>)
Greater than operator u.ska isoqjkafka th mrSCId lrk wjia:djka hq., w;rska ol=Kq mi we;s
wjia:dj" jus mi wjia:djg jvd w.fhka jsYd, oehs ksrSCIKh lsrSuhs' th tfia kus > - operator tl
jsiska true w.ho" th tfia fkdjkafka kus false w.ho ksmofjs' my; WodyrKh foi n,kak' tys
i,ld nef,kafka by; WodyrKfha wd A,B,C ;sfokdf.a Wi w.hka ms,sn`ojh:
===========================================================
float aHeight = 4.5, bHeight = 4.7, cHeight = 5.0;
// (1)
bool heightCheck = aHeight > cHeight;
// (2)
heightCheck = cHeight > bHeight > aHeight;
===========================================================
by; ^1& fldgiska isoqjkafka aHeight hk variable tflys w.h cHeight hk variable tflys w.hg
jvd w.fhka jsYd, oehs mrSCId lsrSuh' Tng fmfkk mrsos fus wjia:dfjsoS th wi;H fyhska > u.ska
false w.h ksmofjs'
27
^2& wjia:dfjsoS isoqjkafka [email protected] tu wjia:dfjsoS isoqjkafka “C wfkla fofokdgu jvd Wiska jevshs”
hk lshuk mrSCIdjg ,la lsrSuhs' tkus" cHeight > bHeight hk expression tl u.ska cHeight,
bHeight g jvd jsYd, oehs mrSCId fjs' bkamiq bHeight > aHeight expression tl u.ska bHeight"
aHeight g jvd jsYd, oehs mrSCId fj' fus wkqj cHeight > bHeight > aHeight ys fmdoq yrh f,i
,efnk true fyda false w.fhka .uH jkafka by; lshufkys i;H-wi;H;djhs' ;j;a meyeos,s f,i
lsh;fyd;a" cHeight > bHeight yd bHeight > aHeight hk wjia:d foflkau true w.h ksmojqk
fyd;a tys woyi kus cHeight fyj;a C f.a Wi wfkla fookdg jvd jevs nj i;Hhla njhs'
kuq;a oeka fuysoS tu expression tflka lshfjk woyiu jsreoaO w;go is;sh yel' tkus fufiao lsj
yels nj Tng fmfkkq we;: by; expression tl u.ska aHeight fyj;a A f.a Wi" bHeight fyj;a
B f.a Wigo" cHeight fyj;a C f.a Wigo jvd wvq oehs mrSCId flfrA'
Less than operator (<)
Less than operator hkq Greater than operator ys jsreoaO wjia:djhs' tkus" th u.ska isoqjkafka th
mrSCId lrk wjia:djka hq., w;rska ol=Kq mi we;s wjia:dj" jus mi wjia:djg jvd w.fhka jsYd,
oehs mrSCId lsrSuhs' th i;H kus < jsiska true w.ho" th tfia fkdjkafka kus false w.ho ksmojh'
my; WodyrKfhka fuh oelafjs:
===========================================================
float aHeight = 4.5, bHeight = 4.7, cHeight = 5.0;
// (1)
bool heightCheck = cHeight < aHeight;
// (2)
heightCheck = aHeight < bHeight < cHeight;
===========================================================
by; ^1& fldgiska ksmofjkafka false w.h njo ^2& fldgiska ksmofjkafka true w.h njo Tng
jegfykq we;' ;j oqrg;a meyeos,s lr;fyd;a ^2& jeks wjia:dfjka lshfjkafka my; lshukhs:
“aHeight, bHeight, cHeight hk variables ;s%;ajh w;rska l=vdu w.hla .kafka aHeight h'“ ta
wkqj by; tys w.h true fjs'
Greater than or equal to operator (>=)
fuu operator tl hkq Tn by;ska oqgq Greater than operator iy Equal to operator tflys tl;=jhs'
tkus th tys ol=Kq mi we;s expression tl" jus mi we;s expression tlg jvd jsYd, oehs mrSCId
lrkjd fukau" tu expressions tl hq., iudkoeh`o mrSCId lrhs' tu wjia:djka folska tlla fyda
i;H kus th true ksmojhs:
Wod'
===========================================================
int a = 10, b = 20, c = 10;
// (1)
bool check = (b >= a);
// (2)
check = (c >= a);
===========================================================
^1& wjia:dfjysoS" d, a g jvd jsYd, ksid check ys w.h true fjs'
^2& wjia:dfjys a yd c iudk ksid check ys w.h true fjs'
28
Less than or equal to operator (>=)
fuh Greater than or equal to operator tflys jsreoaO wjia:djhs' thska mrSCId flfrkafka tys ol=Kq
mi we;s expression jus mi we;s expression tlg jvd l=vd fyda iudk oehs hkakhs'
Equal to operator (==)
Equal to operator u.ska mrSCId jkafka tys mrSCIdjg ,la fjk w.hka hq., yrshgu iudko hkakhs:
Wod:
===========================================================
int a = 10, b = 20, c = 10;
// (1)
bool check = (b == a);
// (2)
check = (c == a);
===========================================================
^1& wjia:dfjsoS false ksmofjk w;r ^2& wjia:dfjsoS true ksmofjs'
Not equal to operator (!=)
fuh Equal to operator ys jsreoaO wjia:djhs' mrSCIdjg ,lafjk w.hka hq., tlsfklg fjkia kus
th true w.h ksmojhs" iudk kus false ksmojhs:
Wod:
===========================================================
int a = 10, b = 20, c = 10;
// (1)
bool check = (b != a);
// (2)
check = (c != a);
===========================================================
^1& wjia:dfjsoS true ksmofjk w;r ^2& wjia:dfjsoS false ksmofjs'
Increment & Decrement operators
Increment operator (++)
fuh Addition operator (+) ys jsl,amhla f,io yeoskajsh yelsh' kuq;a increment operator u.ska
isoqjkafka lsishus ixLHd;aul w.hla tllska muKla by, oeuSuh' fus ksid" ixLHd;aul w.hlg 1 g
jvd by, ixLHdjla tlajru tl;= lsrSug wjYH jQ jsg increment operator tl mdjspsps l, fkdyel'
Increment operator ys jHdlrK jsOsh my; oelafjs:
===========================================================
29
int number = 50;
number++;
cout<< number << endl;
===========================================================
by;" number ys w.h tllska by, kxjd we;' tksid tys kj w.h 51 f,i print jkq we;'
Decrement operator (--)
fuh increment operator ys jsreoaO wjia:djhs' tkus decrement operator u.ska isoqjkafka lsishus
ixLHd;aul w.hla 1 lska my, oeuSuhs:
Wod:
===========================================================
int number = 50;
number--;
cout<< number << endl;
===========================================================
49 print fjs'
Assignment Operators
Assignment operators Ndjs;d lrkafka variables j,g w.hka wdfoaY lsrSfusoSh'
Assignment operator (=)
fuh Tn oekgu;a y|qkk operator tlls' ir, wdfoaYhka i`oyd fuh Ndjs;d fjs:
Wod:
===========================================================
int number;
number = 15;
===========================================================
Addition assignment (+=)
Addition assignment operator tlo addition operator ys wdfoaYlhla f,i Ndjs; l, yel' fuh Ndjs;d
lrk wdldrh y`oqkd.ekSug addition operator u.ska isoql, yels assignment tlla addition
assignment u.ska isoqlrk wdldrh i,ld n,uq:
===========================================================
int number;
number = 15;
// (1)
number = number + 20;
// (2)
number += 20;
===========================================================
30
by; ^1& wjia:dfjsoS number fj; 20 la tl;= lr we;af;a addition operator tl Ndjs;d lsrSfuks' ^2&
wjia:dfjsoS oelafjkafka tu wdfoaY lsrSuu addition assignment Ndjs;d lsrSfuka isoqlrK wdldrhh'
tysoS" addition operator Ndjs;fhaoS expression tflys jus mi fhosh hq;= fojeks number fhoqu bj;a jS
we;' += u.ska Rcqju" wjYH w.h number variable tlg tl;=fjs' fusksid lsishus variable tllg
lsishus ixLHdjla fl,skau tl;= lr.ekSug wjYH jQ jsg addition assignment operator Ndjs;h jvd
myiqfjs'
Subtraction assignment (-=)
fuh addition assignment ys m%;sjsreoaO wjia:djhs:
Wod:
===========================================================
number -= 20;
===========================================================
by;oS" number fj;ska 20 la wvqlr we;'
Multiplication assignment (*=)
fuh ir, .=K lsrSus i`oyd Ndjs;d fjs' fuh ls%hd;aul jkafka by; operators hq., ls%hd;aul jQ
wdldrhguhs' my; WodyrKfhka fuh meyeos,s jkq we;:
===========================================================
number *= 20;
===========================================================
by; isoqjkafka number ys w.h 20 ka ir, f,i .=KjSuh' fuh my; multiplication operator u.ska
isoqlr we;s .=K lsrSug iudk fjs:
===========================================================
number = number * 20;
===========================================================
Division assignment (/=) ls%hdlrkafka o by; mrsosuh:
Wod:
===========================================================
number /= 20;
===========================================================
by;" number ys w.h 20 ka fnod" ms,s;=r number ;=, .nvd fjs'
fu;ekska operators .ek jsia;rh ksudjg m;afjs' kuq;a by;oS i`oyka fkdjqkq operators jrA.hla we;'
fusjd Bitwise operators kuska ye`oskafjs' Bitwise operators fndfydajsg Ndjs; jkafka oajsuh ixLHd
iu. lghq;= lsrSfusoSh' fuh Tn oekau w;fkd.ik lghq;a;la ksid Bitwise operators .ek l:d
lsrSu fjk;a wjia:djlg b;srs lr we;'
31
4' mrs.Kl jevigykl ls%hdldrS;ajh
md,kh lsrSu'
(Program Flow Control Statements)
(fuu mrspsfpsoh lshjSug fmr" fuu fmdf;ys Appendix fldgfia we;s “Debug lsrSu iy Errors
y|qkd.ekSu” hk fldgi lshjSu iqoqiqh)
mrs.Kl jevigykla .,d hk c, myrlg iudkh' c, myrl we;s Yla;sfhka ksis m%fhdack
.ekSug kus" wm tu c, myfrys .ukg ndOd l, hq;=h' c, myrl .uk md,kh fkdl, jsg" tys
.ensj we;s pd,l Yla;sfhka m%fhdackhla .ekSug fkdyels fjhs' md,kh fkdl, mrs.Kl
jevigyklao md,kh fkdl, c, Odrdjla fuka kslrefka .,hs' mrs.Kl jevigyklgo
md,khlska f;drj .e,Sug bv y,jsg" thska lsishus m%fhdackj;a fohla lr.ekSug wmg yels
fkdfjs' fuksid wm l,hq;= jkafka" wm jsiska ksrAudKh lrkq ,nk mrs.Kl jevigykaj,
ls%hdldrS;ajh ksis mrsos md,kh lsrSfuka" tajd hym;a uDoqldx.hka njg m;a lsrSuh'
mrs.Kl jevigykl fuu “hym;a nj” hkafkka ye`oskafjkafka [email protected] ir, f,i .;ajsg th
lreKq lsysmhla u; r`od mj;shs' ta w;rska tlla kus" lsishus mrs.Kl jevigykla" th mdjspsps
lrkakd fj; olajk ie,ls,a,h' jevigykla mdjspsps lsrSu th Ndjs;d lrkakd yg wmyiq yd
ixlSrAK nj yef.a kus" th tu jevigyfkys wdlrAYKSh Ndjh my; fy,hs' ;j;a lreKla kus
jevigykla lsishus fohla isoqlsrSug .kakd ld,hhs' mrs.Kl jevigykla Tn ,sjsh hq;af;a yels
;rus wvq ld,hla .;fldg th Ndjs;dlrkakd yg wjYH ldrAhh bgqlroSfus wruQKska hq;=jh'
fus ldrAhhka isoq lsrSug kus wm mrs.Kl jevigyka j, ls%hdldrS;ajh wmg wjYH mrsos md,kh
lr.; hq;=h' fuh isoqlsrSu i`oyd C++ ys jsj`o l%fudamdhhka we;' oeka wm mrs.Kl jevigykl
ls%hdldrS;ajh md,kh lsrSfus tu l%fudamdhhka y|qkd.ekSu wrUuq'
by; lshQ l%fudamdhhka ish,a,u .;ajsg tajd ye`oskajSug “Program control statements” hkak Ndjs;d
fjs' Program control statements jrA. lSmhlg fnod oelajsh yel' my; oelafjkafka tajd jrA.SlrKh
lr we;s wdldrhhs'
Conditional Statements
if – statement
if…else - statement
else…if - statement
switch…case - statement
Looping Statements
while - statement
do…while - statement
for - statement
break & continue
32
Jumping Statements
goto – statement
oeka wm fuu Program control statements jevigyka j,oS Ndjs;d lrK wdldrh jgyd .ekSug
W;aidy lruq'
Conditional Statements
Conditional statements u.ska mrs.Kl jevigykla ;=, Woa.; jk ;;ajhkag wkqj tu jevigyfkys
ls%hdldrS;ajh fyj;a tys .,dhdu md,kh l, yelshs' my; oelafjkafka tu conditional statements
i`oyd meyeos,s lsrSush'
if - statement
if - statement tl u.ska isoqjkafka mrs.Kl jevigykla ;=,oS lsishus ;;ajhla iemsreK jsg muKla
hus l%shdjla isoqlsrSuh'
k`oiqkla f,i lsisfjl= fufia lSjd hehs is;kak: “ud ‘2001: A space odyssey’ keue;s fmd; us,oS
.kafka tu fmdf;ys us, remsh,a 200 g wvqkus muKhs”. tkus" Tyqg fmd; us,oS .ekSug kus tys us,
remsh,a 200 g wvq jSfus ;;ajh iemsrsh hq;=h' fuu lshuk mokus lrf.k wm oeka if Ndjs;d lruska
mrs.Kl jevigykla ,shuq:
==========================================================
// if.cpp
#include<iostream.h>
void main()
{
int bookPrice = 180;
if(bookPrice < 200)
{
cout << “I buy the book(1).” << endl;
}
bookPrice = 230;
if(bookPrice < 200)
{
cout << “I buy the book(2).” << endl;
}
}
===========================================================
by; jevigyfkys ^1& wjia:dfjsoS" if u.ska bookPrice ys w.h 200 g jvd wvqoehs mrSCId flfrA'
fuh isoqjkafka if ys jryka ;=, we;s (bookPrice < 200) hk expression tl u.sks' if u.ska mrSCId
flfrkafka fuu expression tl i;H fjsoehs hkakhs' fjk;a whqrlska lsjfyd;a" if u.ska" bookPrice
ys w.h 200 g jvd wvq jSu hk ;;ajh iemsfrkafka oehs mrSCId lrhs' tu ;;ajh iusmQrAK jqjfyd;a"
fyj;a (bookPrice < 200) hkak true jqjfyd;a" if hgf;a we;s mrs.Kl fla;hka ls%hd;aul fjs' oeka
fuysoS bookPrice ys w.h 180 ksid (bookPrice < 200) hk expression tl true fyj;a i;H fjs'
tkus bookPrice ys w.h 200g jvd wvq jSu hk ;;ajh iusmQrAK jS we;' fuksid if ;=, we;s fla;h
ls%hd;aul jS “I buy the book(1)” hkak print fjs' ^2& wjia:dfjys if u.ska kej;;a tu mrSCIdj
isoqfjs' fujr bookPrice ys w.h 200 g jevs ksid (bookPrice < 200) hk expression tl false
fyj;a wi;H fjs' tfyhska “I buy the book(2).” hkak print fkdfjs' tfiakus" if u.ska jevigyk
33
ls%hd;aul jk wdldrh md,khg ,la lr we; ' tkus fojk wjia:dfjs we;s cout jsOdkh ls%hd;aul
fkdjk whqrska if u.ska jevigyk md,kh flfrA'
if…else - statement
oeka by; mqoa.,hd fufia lshhss: “fmdf;ys us, 200 g wvqjqfkd;a ud th us,oS .kakd w;r" us, 200 g
jvd jevsjqfkd;a uu fjk;a fmd;la us,oS .ksus'“ fuu lshuk ;=,oS Tyq wjia:d 2 la ms,sn`o l:d lr
we;' fmr wjia:dfjsoS Tyqg we;af;a f;dard.ekSus 1la muKla kuq;a fus wjia:dfjoS Tyqg f;dard
.ekSfus wjia:d 2 la we;' mrs.Kl jevigykloS fujeks f;dard.ekSfus wjia:d hq.,la muKla we;s
wjia:d j,oS wmg mdjspsps lsrSug isoqjkafka if…else h' if…else hkq if ys ls%hdldrS;ajfhysu jevs
oshqKqjlss' oeka by; m%ldYh if…else fhdod .ksuska mrs.Kl jevigyklg wka;rA.; lrkafka
flfiaoehs n,uq:
==============================================================
// ifElse.cpp
#include<iostream.h>
void main()
{
int bookPrice = 250;
if((bookPrice < 200))
{
cout << “I buy the book.” << endl;
}else {
cout << “I buy some other book.” << endl;
}
}
by;oS if u.ska bookPrice ys w.h fmr mrsosu 200 g wvqoehs mrSCId fjs' bookPrice oeka 250 la jk
ksid m,uqjk cout jsOdkh ls%hd;aul fkdfjs' ls%hd;aul jkafka else ;=, we;s cout jsOdkhhs'
bookPrice ys w.h 200 g wvq jQjdkus else ;=, we;s cout jsOdkh ls%hd;aul fkdjk w;r" ls%hd;aul
jkafka if ;=, we;s cout jsOdkhhs'
else…if - statement
fmr m%ldYh mqoa.,hd th oeka fuf,i fjkia lrhs: “fmdf;ys us, 200 g wvqjqfkd;a ud th us,oS
.kakd w;r" us, 150 g jvd wvqjqfkd;a uu fmd;a folla f.k tlla uf.a hy,qjl=g ;E.s lrus'
fmdf;ys us, 100 go jvd wvq jqjfyd;a uu fmd;a 3 la f.k 2 la uf.a hy,qjka fofofkl=g ;E.s
lrus'“ oeka isoqjkafka [email protected] Tyq yg f;dard .ekSug oeka wjia:d 3 la we;' mrs.Kl jevigykloS"
fuf,i f;dard.ekSfus wjia:djka 2 lg jevsfhka we;s wjia:d j,oS else…if control statement tl
mdjspsps lsrSug yelsh' my; jevigykska fuh meyeos,s jkq we;:
=====================================================
// elseIf.cpp
#include<iostream.h>
void main()
{
int bookPrice = 85;
if((bookPrice < 200) && (bookPrice > 150))
{
cout << “I buy the book.” << endl;
34
}else if ((bookPrice < 150) && (bookPrice > 100))
{
cout << “I buy 2 copies of the book.” << endl;
}else if (bookPrice < 100)
{
cout << “I buy 3 copies of the book.” << endl;
}
}
=========================================================
if u.ska uq,skau && operator tl Ndjs;d lsrSfuka" bookPrice ys w.h 200 g wvqoeh`o hkak iy 150
jev`oeh`o hkak mrSCId flfrA' tkus bookPrice ys w.h we;af;a 200 yd 150 w;r oehs fidhd nef,a'
bookPrice w.h jk 85" 200 g wvq kuq;a 150 g jvd jevs fkdjk ksid m,uq jeks cout jsOdkh
ls%hd;aul fkdjk nj meyeos,shs' fhdod we;s operator tl && ksid mrSCId lrk wjia:d folu i;H
jsh hq;=hs'
uS,.g fhdod we;s else if u.ska bookPrice w.h we;af;a 150 ;a 100 ;a w;r oehs mrSCIdfjs' th;a i;H
fkdjk fyhska uS,.Sg f;jk wjia:dfjs else…if u.ska bookPrice 100 g wvqoehs nef,a' fuh i;H jk
ksid th ;=, we;s cout jsOdkh ls%hd;aul fjs' fusksid by; jevigyk Tn ls%hd;aul l,fyd;a print
jkafka f;jeks wjia:dfjs else if ;=, we;s cout jsOdkh muKs' fuf,i" f;dard .ekSug we;s wjia:d
.Kk jevsjk jsgoS tu wjia:d .Kkg wkqrEm else if statements .Kklska tu wjia:d iu. lghq;=
l, hq;=h'
Looping Statements
fojk >Kfha Programming control statements jrA.h jkafka Looping statements h' Looping
statements bf.kSu i`oyd wm oeka ir, mrs.Kl jevigykla i,ld n,uq'
fuu jevigyk ls%hdlrkafka fus wdldrhghs: wm jsiska jevigykg lsishus ixLHd;aul w.hla
iemhsh hq;=h' tjsg jevigyk fuu ixLHdj mrSCIdlr" th 100 yd 200 w;r ixLHdjla fkdfjhs kus
tys w.h fjkilg ,la lrhs' tkus" ixLHdj 200 g jevs kus tys w.h 200 olajd wvq lrk w;r" th
100 g wvqkus tys w.h 100 olajd by, ouhs' fjk;a jpk j,ska lsjfyd;a" jevigyk jsiska wm
imhk ixLHdj kshus; mrdihlg we;=,a l, yels jkfia fjkia lrhs' flfiao wm fujeks
jevigykla [email protected] th b;du;a ir,hs'
isoql,hq;= foa jkafka fusjdhs: ^1& jevigyk jsiska wm fokq ,nk ixLHdj mrSCId lr" th 100 g
wvqo" tfia ke;skus 200 g jevs oehs hkak ;SrKh l, hq;=hs' ^2& ixLHdfjs w.h 100 g wvq kus" th
100 jk;=re tys w.h fkdlvjd by, oeush hq;=hs' ^3& ixLHdfjs w.h 200 g jevs kus" th 200 jk;=re
tys w.h fkdlvjd my, oeush hq;=hs' wm Looping statements Ndjs;hg .kafka fujeks lghq;=
isoql,hq;= wjia:dj,oShs' my;" while keue;s looping statement tl wdOdrfhka fuh isoqlrk wdldrh
oelafjs'
while - statement
while u.ska isoqlrkafka" lsishus ;;ajhla iemsfrk ;dla" t;=, wka;rA.; jk mrs.Kl fla; kej;
kej; ls%hd;aul lrjSuhs' fuysoS kej; if statement tl ls%hdl, wdldrh isyshg k.d .kak' if u.ska
isoqjQfha lsishus ;;ajhla iemsfrA kus tlajrla muKla ;ud ;=, we;s fla;hka ls%hd;aul lrjSuhs'
kuq;a while jeks looping statements u.ska isoqjkafka lsishus ;;ajhla i;H jk f;la fla;hka
fkdkj;ajd kej; kej;;a ls%hd;aul lsrSuhs'
35
oeka wm by; i`oyka l, jevigyk while u.ska ilikafka flfiaoehs n,uq:
===========================================
// while.cpp
#include<iostream.h>
void main()
{
int number=0;
cout<<"Enter a number: ";
cin>> number;
while(number<100)
{
number++;
cout<<number<<“, “;
}
while(number>200)
{
number--;
cout<<number<<“, “;
}
cout<<number<<endl;
}
======================================
by; jevigyfkaoS m,uqfjka" cout yd cin u.ska wm number keue;s variable tl ;=,g w.hla we;=,a
lruq' ^cout u.ska isoqjkafka lsishus o;a;hla console tl u;g output lsrSu jQ fia" cin u.ska
flfrkafka wm console tl u;g type lrk lsishus o;a;hla jevigyk ;=,g wdodkh lsrSuhs' by;
th we;=,a jkafka number ;=,gh' cout Ndjs;d lsrSfusoS ta iu`. wm “ << ” ,l=K fhdouq' cin Ndjs;d
lsrSfusoS wm th bosrsfhka fhosh hq;= jkafka “ >> “ ,l=Kh'& oeka Tng while statements 2 la oelsh
yelsh m,uq jekafkka mrSCId flfrkafka number ys w.h 100 wvqo hkakhs' th i;Hhla kus" tu
while ;=, we;s fla;h wkjr;j l%shd;aul jSu werfUs' tkus" number ys w.h kej; kej;;a 1 ne.ska
by, hhs' fuh kj;skafka number = 100 jQ jsgh'
fojeks while loop tlska mrSCId lrkafka number ys w.h 200 g jvd jev`o hkakh' th i;H kus"
while ;=, fla;h wkjr;j l%shd;aul jSfuka number ys w.h 200 olajd by, hkq we;' number ys
w.h 100 ;a 200 ;a w;r kus" while loops folska tlla fyda ls%hd fkdlrk nj meyeos,shs' fuu
jevigyk ls%hd;aul lrkak' Terminal window tl ;=, lsishus ixLHdjla type lr enter key tl
Tnkak' tjsg cin jsOdkh Tn type l, ixLHdj number variable tlg wdfoaY lrkq we;' Tn thg 100
g wvq ixLHdjla iemhqjfyd;a 100 olajd tys w.h jevs jk wdldrho" 200 jevs ixLHdjla iemhqjfyd;a
200 olajd tys w.h wvqjk wdldrho" jevigyk u.ska output lrkq we;'
(fuu jevigyfkys ls%hd;aul jSu fyd`oska oel .ekSug kus th debug lr ls%hd;aul jSfus ,CIh
.uka lrk wdldrh kerTsh hq;=h' Debug lsrSu ms,sn`oj Appendix fldgfiys i`oykafjs&
do…while - statement
fojeks loops jrA.h jkafka fuhhs' fuh tla lreKlska yefrkakg while statement tlg yrshgu
iudkhs' by; WodyrKhu Do… while u.ska ,shQ jsg my; mrsos osiajkq we;:
================================
// Dowhile.cpp
36
#include<iostream.h>
void main()
{
int number=0;
cout<<"Enter a number: ";
cin>> number;
do
{
number++;
cout<<number<<", ";
}while(number<100);
do
{
number--;
cout<<number<<", ";
}while(number>200);
cout<<number<<endl;
}
================================
do… while ys jHdlrKh ;rula fjkia nj Tng oelsh yel' do… while ys isoqjkafka ish,a,g
m,uqfjka do hgf;a we;s fla;hka ls%hd;aul jSuhs' tkus m,uqjk jdrfhaoS while u.ska mrSCId lrK
;;ajh i;H jqj;a wi;H jqj;a do hgf;a we;s fla;h wksjdrAhfhka ls%hd;aul fjs' bkamiq while ys
mrSCIdj isoqjk w;r" th wi;H jqjfyd;a loop tl nS`oSS hhs' i;H jqjfyd;a do ;=, we;s fla; os.gu
ls%hd;aul fjs' fus wkqj" by; while loops u.ska mrSCId flfrk ;;ajhka hq.,u wi;H jqj;a" m,uq
do statement tl ;=,oS ixLHdjg 1la tl;= jS th print jkq we;s w;r" fojeks do u.ska thska 1la wvq jS
print jkq we;' fuh by; wmf.a jevigyk i`oyd t;rus wrA:hla iys; ls%hdjla fkdjqk;a" thska Tng
do… while ls%hdlrk wdldrh W.; yelshs'
for - statement
for statement tl tla w;lska while ysu oshqKq wjia:djla f,i ie,lsh yel' for u.ska isoqjkafka o
lsishus ;;ajhla iemsfrk ;=re ta hgf;a we;s mrs.Kl fla; /i wkjr; f,i ls%hd;aul lrjSuh'
kuq;a for ys while g jvd u`o ixlSrAK njla we;' oeka wm fuu for statement tl ls%hd lrkafka
flfiaoehs jsuid n,uq'
================================
for(int i = 0; i < 10; i++)
{
// code…
}
================================
by;ska oelafjkafka Tng fndfydajsg oelSug yels wdldrfha for – statement tlls' tys jryka ;=,
fldgia 3 la Tng oelsh yelshs' m,uq fldgiska int jrA.fha variable tlla define jS we;' fojk
fldgiska tu variable tflys w.h lsishus ixLHdjlg jvd wvqoehs mrSCId fjs' f;jeks fldgiska
variable tflys w.h 1 lska by, kexfjs' for ls%hd;aul jkafka fus fldgia ish,a, u.sks'
my; oelafjkafka fuu ls%hd;aul jSu isoqjk wdldrhhs:
for ;=, m,uqj isoqjkafka i keue;s int variable tl define jSfuka th 0 g iudk jSuhs' bkamiqj <
operator tl Wmfhda.S lrf.k i ys w.h 10 g jvd wvqoehs mrSCId flfrA' th 10 g jvd wvqkus B,.g
37
isoqjkafka i++ u.ska i ys w.h 1 lska by, oeuSuhs' tksid oeka i ys w.h 1 fjs' oeka for hgf;a we;s
fla; ls%hd;aul jSu werfTs' fla; ish,a, tlajrla l%shd;aul jSfuka miq kej; ls%hd;aul jSfus ,CIh
by,g meusfKa' fus wjia:dfjsoS for jsiska kej;;a i < 10 u.ska i ys w.h 10 g wvqoehs n,hs' ;ju;a
th 10 g wvqksid i++ u.ska kej;;a i ys w.h 1lska by, kxjhs' oeka i = 2 nj Tng fmfka' bka
wk;=rej kej;;a for ys mrs.Kl fla;hka ls%hd;aul fjs' fuf,i kej; kej;;a i ys w.h 10 g
wvqkus th 1lska by, hdu;a" for hgf;a we;s fla; ls%hd;aul jSu;a isoqfjs' fuh weKysgskafka i = 10
jk wjia:dfjsoSh' tkus" i = 10 jS ls%hd;aul jSfus ,CIh by,g wd jsg" i < 10 hkak wi;H jk ksid
i++ hk fla;h ls%hd jsrys;j hhs' fusksid bkamiq i tllska jevsjkafka;a ke;" for hgf;a we;s
fla;hka ls%hd;aul jkafka;a ke;'
oeka fuh ;j;a meyeos,s lr .ekSu msKsio for iy while w;r we;s iusnkaOh jgyd .ekSu msKsio"
while u.ska ,shQ jevigykla kej; for u.ska ,shkq ,nkafka flfiaoehs mrSCId lr n,uq' my;
oelafjkafka while u.ska ,shk ,o jevigykls:
==============================================
// whileProg.cpp
#include<iostream.h>
void main()
{
int count = 0;
while(count < 20)
{
cout << “Go…” << endl;
count++;
}
cout << “Halt!” << endl;
}
========================================
by; jevigyk ls%hd;aul l,fyd;a” “Go…” hkak 20 jdrhla print jk jkq we;' bkamiq “Halt!”
hkak print fjs' oeka fuu ir, jevigyk for statement tl Ndjs;d lr ,shkafka flfiaoehs oeka
n,uq' my; oelafjkafka fuu jevigykhs:
===================================================
// forProg.cpp
#include<iostream.h>
void main()
{
for(int count = 0; count < 20; count++)
{
cout << “Go…” << endl;
}
cout << “Halt…” << endl;
}
===================================================
by; for u.ska ,shk ,o jevigykska isoqjkafkao “Go…” hkak 20 jdrhla print jSs" bkamiq “Halt!”
hkak print jSuhs' oeka fuu jevigyka foflys olakg ,efnk fjkia lus [email protected] m,uqfjka"
while fhdod.kakd wjia:dfjsoS count - variable tl define lr we;af;a fjku tallhla f,ih' kuq;a
for fhdod.kakd wjia:dfjsoS th for statement tl ;=,u" tys fldgila f,i define lr we;' tfukau
38
count ys w.h by, kexjSfu`oSo tu ldrAhh isoqj we;af;a for ysu fldgila f,ihs' tfiakus for u.ska
isoqlr we;af;a while u.ska isoql, ldrAhhka talrdYS lr tla ia:dkhlg .ekSu nj lsj yelsh'
break & continue
break yd continue hk hq., by; Tn oqgq ish,qu Looping statements ;=,g wka;rA.; lsrSu i`oyd
fhdod.efka' fusjdfha ldrAhh kus Looping statements md,kh lsrSuhs'
break ys ldrAhh kus Looping statement tll ls%hdldrS;ajh w;r u. kj;ajd oeuSuh' fuhg
WodyrKhla f,i" break statement tl while ;=,g fhdod we;s wkaou my; jevigykska oelafjs:
====================================================
// break.cpp
#include<iostream.h>
void main()
{
int count = 0;
while(count < 20)
{
cout << “Go…” << endl;
count++;
if(count==5)
{
break;
}
}
cout << “Halt!” << endl;
}
=====================================================
by; jevigyfka count=5 jQ jsg if ;=, we;s break statement tl ls%hd;aul jS while loop tl ns`oS hhs'
fus ksid Go… hkak print fjkq we;af;a 5 jrla muKs' break statement tl fkd;snqKd kus Go…
hkak 20 jrla print jk nj Tng meyeos,sh'
continue statement tl break statement tlska u`ola fjkia fjs' break u.ska flfrkafka loop tflys
l%shdj iusmQrAKfhka kej;ajSu jQ w;r continue u.ska flfrkafka loop tflys oekg isoqfjuska mj;sk
l%shd;aul jSfus jdrh muKla w;ru. kj;ajd B,S. ls%hd;aul jSfus jdrhg jevigyk ls%hd;aul jSfus
,CIh mekajSuh' fuh my; jevigykska meyeos,sfjs:
=======================================================
// continue.cpp
#include<iostream.h>
void main()
{
int count = 0;
while(count < 20)
{
count++;
if(count==5)
{
continue;
}
cout << "Go..." << endl;
}
39
cout << "Halt!" << endl;
}
=========================================================
by;oS" count=5 jQ jsg if ;=, we;s continue statement tl ls%hd;aul jk w;r" tuksid tu jdrfha
while ys ls%hd;aul jSu w;ru. weK ysgSfuka cout << "Go..." << endl; fla;h ls%hd;aul jSu
tlajrla u.yerS hhs' kuq;a continue u.ska loop tl ns`o oukafka ke;s ksid" while os.gu ls%hd;aul
fjs' fusksid wjidkfhaoS Go… hkak print jS ;sfnkq we;af;a 19 jrls' continue statement tl
fkd;snqKd kus Go… hkak 20 jrla print fjs'
5' Functions
Functions hkq fudkjdoehs Tn wihs' kuq;a Tn th oekgu;a oel we;' Tn by; mrspsfpso
j,oS oqgq main(), C++ ys Tng yuqfjk m,uq yd m%Odk function tlhs' kuq;a function tlla wm wrA:
olajkafka [email protected]
Function tlla hkq C++ muKla fkdj fjk;a fndfyda mrs.Kl jevigyka j,o uQ,sl ls%hdldrS
tallhhs' fjk;a jpk j,ska lsh;fyd;a functions fkdue;sj C++ u.ska mrs.Kl jevigyka ,sjSula
ms,sn`oj is;sh fkdyelsh' Function tlla ;=, wvx.= jkafka mrs.Klh ,jd lsishus ls%hdjla lr.ekSu
i`oyd ,shk ,o mrs.Kl fla; reils' Function tlla u.ska lsishus mrs.Kl fla; reila i`oyd
wdjrKhla idohs' tu fla;hka u.ska isoqfjk ldrAhh wkqj Functions tflys ku ;SrKh lrkq
,efnhs' WodyrKhla f,i mrs.Kl ;srh u; lsishus fohla print lrkakdjQ mrs.Kl fla;hka wvx.=
fldg ksujk ,o Function tlla print kuska kus l, yel' Tn oqgq main keue;s function tl fj;
tu ku ,enS we;af;a th mrs.Kl jevigykl m,uqfjkau ls%hd;aul jk function tl jk fyhsks'
Functions u.ska mrs.Kl jevigyklg isoqfjk fiajhka lSmhla meyeos,sj oelsh yelshs :
40
1' tla jrla ,shk ,o mrs.Kl fla;hka kej; kej; m%fhdackhg
.ekSfus myiqlu ie,iSu'
2' mrs.Kl jevigykla fldgia j,g fnod fjkalsrSu' fus u.ska
tu jevigyk ,sjSu iy jgyd .ekSu myiq fjs'
wm oeka function tlla hkq l=ulaoehs y|qkd.ekSfus m,uq mshjr f,i th mdjspsps lr ,shk ,o ir,
mrs.Kl jevigykla l=uk wdldrhla .k`oehs jsuid n,uq' my; oelafjk jevigyk mrSCId lr
n,kak' th bosrsm;a lr we;af;a Tng function tlla ls%hdlrK wdldrh .ek ir, f,i fmkajd oSug
muKs' tksid tys we;s ish,q foa jgyd .ekSug W;aidy fkdlrkak' Functions .ek jsia;r" wm my;
jevigyk .ek idlpspd lsrSfuka wk;=rej jev`oqrg;a i,ld n,uq:
========================================================
// introToFunctions.cpp
#include<iostream.h>
void print();
void main()
{
print();
}
void print()
{
cout << “Hi! This is my 1st function.” << endl;
}
========================================================
wm variables .ek l:d lsrSfusoS" variable tlla define lsrSug fmr th declare l, hq;= nj Tn oqgqjd'
^fus ls%hdj ms,sn`oj Tnf.a u;lh .s,syS we;akus lreKdlr m,uq mrspsfpsofhaoS variables hgf;a tk
fldgi iy 2 jk mrspsfpsofhaoS int – data type hgf;a tk fldgi kej; lshjkak& fuh functions
i`oydo fmdoqjk lreKls' fusksid variable tlla fukau function tlla o declare lr bkamiq define
l, hq;=hs' Function tlla m,uqj declare lsrSfuka isoqjkafka wm jevigyk ;=, tu function tl
mdjspsps lsrSug hk nj mjiuska compiler uDoqldx.hg th y`oqkajdoSuh' tjsg th jevigyk compile
lrf.k hdfusoS" function declaration tl yuqjQ jsg function tl iu. lghq;= lsrSug oek isgskq we;'
;jo" variable tlla iusnkaOfhka Tn oqgq mrsosu function tlloSo declare iy define lsrSu tlajr
isoql, yelsh'
by; jevigyfkaoS void print() hkafkka isoqjkafka fuu function tl declare lsrSfus ls%hdjhs' tys
definition tl isoqjkafka main function tlg my,ska we;s"
void print()
{
cout << “Hi! This is my 1st function.” << endl;
}
hk fldgisks' tkus" print function tl u.ska isoqlrK ldrAhh ;SrKh lr thg kshu w.hla fokafka
fuysoSh' th isoqlrK ldrAhh kus" cout << “Hi! This is my 1st function.” << endl hk
fla;h ls%hd;aul lrjSu nj Tng fmfkkq we;'
main() ;=,o print() f,i function tflys ku i`oyka jS we;s nj Tng oelsh yel' fuh function
call f,i ye`oskafjs' thska isoqjkafka print function tl ls%hd;aul lrjSuhs' fujsg print ;=, we;s
mrs.Kl fla;hka ls%hd;aul jSu isoqfjs' fuh isoqjk wdldrh kerTSu i`oyd Tn" by; jevigyk
debug lsrSu wdrusNlr" Visual C++ ys step in iy step over hk ls%hdldrS;ajhka u.ska jevigyk
41
ls%hd;aul jk wkqms,sfj, mrSCId l, hq;=h' ^fmdf;ys “Debug lsrSu iy Errors y|qkd.ekSu” hk
fldgi lshjSfuka fuh isoq lrK wdldrh Tng W.; yelsh&' tjsg main ;=,ska wdrusNjk jevigyk
ls%hd;aul jSfus ,CIh main ;=, we;s print ys function call tl fj; meusK t;ekska print ;=,g
we;=,ajk nj Tn olskq we;' jevigyk ls%hd;aul jSfus ,CIh print ;=, we;s fla;h u;ska .uka
lsrSfuka miq th kej; main ;=,g wdmiq we;=,a fjkq we;' fujsg Terminal window tl foi Tn
ne,qjfyd;a" th ;=, “Hi! This is my 1st function.” hkak print jS ;sfnkq Tn olskq we;' fus
wkqj by; fmfkk mrsos" Tn jsiska idok ,o functions j,g we;=,a jSfus m%Odk fodrgqj jkafka main
function tlhs'
by;oS" function tlla mrs.Kl jevigykla ;=, uQ,sl f,i ls%hd;aul jkafka flfiaoehs oelajqKd'
oeka" functions hkq fudkjdoehs jvd;a ,xj n,uq'
by; Tn oqgq mrsos function tll Ndjs;fha wjia:d 3 la we;'
1. Function declaration
2. Function definition
3. Function call – fuu.ska isoqjkafka jevigykl lsishus ia:dkhl we;s function tlla" tu
jevigyfkys ;j;a ia:dkhl isg ^by; jevigyfkys oS kus main ys isg& ls%hd;aul
lrjSuhs'
Function tlla fldgia 3 lska iukajs;h' my; oelafjkafka tu fldgia 3 hs'
1.
2.
3.
Return type.
Function name'
Parameter list.
Function tlla le,alHQf,agrA hka;%hlg iudk l, yel' Tn le,alHQf,agrhlg o;a; we;=,a l, jsg
(input) th Tn we;=,a l, o;a;hka fjkialus j,g Ndckh lr" wdmiq Tng o;a;hla ,nd fohs
(output). Function tlla u.ska isoqjkafkao fuhhs' th msg;ska o;a; ,ndf.k" tu o;a; fjkialus
j,g Ndckh lr" tu.ska m%;s:,hla ksmojd" wdmiq o;a;hka msg lrhs' by; fldgia ;=fkka m,uq
jekak jk Return type hkafkka" Function tflka msglrK fuu m%:sm, o;a;fha o;a; jrA.h olajhs'
fojeks fldgiska olajkafka function tflys ku nj Tng meyeos,shs' f;jeks fldgi jk Parameter
list hkafkka oelafjkafka function tl" input tl f,i th ;=,g ,nd .kakd o;a;hkah' kuq;a
Parameter list tlla fyda Return type tlla fkdue;s fyj;a" input tlla fyda output tlla fkdue;s
functions o iEosh yelshs' fus jrA.fha functions u.ska lsisu o;a;hla ;ud ;=,g ,nd fkd.kakd w;r
lsisu m%:sm,hla msg;g ,nd fkdfoa' wm by;ska i,ld ne,Q wjika jevigyfkys print function tl
fujekakls' th isoql, tlu foh jQfha jpk fma,shla console window tl u; print lsrSu muKs'
1. Function declaration
Function declaration tlla u.ska isoqjkafka kj function tlla compiler uDoqldx.hg y`oqkajdoSu nj Tn
by; ia:dkhloS oqgqjd' tfiakus oeka wm by; Return type, Function name, Parameter list hk fldgia
;=fkka u iukajs; jQ function declaration tlla ,shkafka flfia oehs n,uq' my; oelafjkafka tjeks
function declaration tllg WodyrKhls:
========================================================
int add(int num1, int num2);
========================================================
42
my; oelafjkafka by; add function tflys declaration tl" by;ska Tn oqgq fldgia j,g fnod we;s
wdldrhhs'
int add(int num1, int num2)
Return type
Function name
Parameter list
fuu function tl output tl f,i ,nd fok o;a;fhys jrA.h int h' fuh Return type hkafkka
oelafjs' tfukau th num1 yd num2 hk int variables hq., input tl f,i ,nd .kshs' fuh parameter
list hkafkka oelafjs' Parameter list f,i wm fu;e; oS i`oyka lr we;af;a variables 2 la muKla
kuq;a Tng leu;s TskEu variables .Kklska hq;a parameter lists iys; functions iEosh yelshs'
2. Function definition
by; function declaration tlg wod, function definition tl my; oelafjs:
===========================================================
int add(int num1, int num2)
{
int addition = num1 + num2;
return addition;
}
// function head
//
// function body
//
===========================================================
add ys function declaration tlska add ys definition tl fjkia jkafka [email protected] m,uqj thg i`.,
jryka hq.,la tl;= jS we;' i`., jryka jsjD; jk ia:dkfhka oelafjkafka add function tflys
wdrusNl ia:dkhhs' ke;fyd;a function tl n,meje;ajSug mgka.kakd ia:dkhhs' i`., jryka
jefik ia:dkfhka function tflys n, m%foaYh wjikaj hhs' Function tl ;=, wvx.= jsh hq;= ish,a,
fuu jryka w;r wka;rA.; jsh hq;=h' fuu jryka w;r fldgi function body hkqfjka ye`oskafjs'
fuys function declaration tlg iudk f,i ,shd we;s by,u fldgi function head f,i ye`oskafjs'
Function definition tflys function head tl wksjdrAhfhkau return type tlskao parameter list tlskao
function declaration tlg iudk jsh hq;=h'
Function definition tl ;=, isoqjkafka [email protected] uq,ska addition kuska variable tlla idod we;s w;r
thg parameter list tflys we;s variable hq.f,ys w.hka f.a tl;=j wdfoaY lr we;' bkamiq return
keue;s keyword tl Tng olakg ,efns' return ys ldrAhh kus" th bosrsfha we;s variable tflys w.h
function tlska msg;g ,nd oSuh' tkus function tflka msg;g o;a; m%:`odkh lrkafka return keyword
tl uS.sks' return ls%hd;aul jSu;a iu`.u function tflys ls%hdjo ksud jS hhs'
kuq;a oeka function tflka return jk w.hg isoqjkafka [email protected] fuhg ms,s;=r ;j fudfyd;lska Tng
,efnkq we;'
3. Function call
fmroSo i`oyka jQ mrsos function tlla ls%hd;aul jSu wdrusN jkafka function call u.sks' oeka wm by;
add function tlg main ;=, isg call lrK wdldrh n,uq:
43
========================================================
void main()
{
int result = add(10,2);
cout << result << endl;
}
========================================================
add ys function call tl" Tng fmroS yuqjQ print function tflys function call tlska fjkia u.la f.k
we;s wdldrh [email protected] fuu call lsrSu isoqjkafka expression tlla ;=,oSh' fuhska isoqjkafka add u.ska
return lrKq ,nk w.h result keue;s variable tlg wdfoaY jSuhs' function call tl isoqfjk wdldrh
n,kak' add bosrsfha we;s jryka ;=, ixLHd hq.,la wka;rA.;h' fuh kus add function tl ;=,g o;a;
we;=,a lsrSfus udrA.hhs' fuh parameter passing hkqfjka ye`oskafjs' fuu ixLHd hq., function
definition tflys num1 yd num2 hk parameters fj; wdfoaY fjhs' tjsg add ls%hd;aul jSfuka" tu
w.hka hq.f,ys fftlHh jk 12 w.h addition variable tl ;=, .nvd fjhs' oeka return u.ska 12 w.h
function tlska msg flfrA' tu w.h main ;=, we;s result variable tl ,nd .kshs' wjidkfhaoS tu w.h
print fjs'
Tn by; oqgq function declaration, definition iy call hk wjia:djka" tl jevigykla f,i my;ska
fm,.iajd we;:
========================================================
#include<iostream.h>
int add(int num1, int num2);
void main()
{
int result = add(10,2);
cout << result << endl;
}
int add(int num1, int num2)
{
int addition = num1 + num2;
return addition;
}
========================================================
44
6. Arrays
mrs.Kl jevigyka j,oS" lsishus o;a; fm,la iu. lghq;= lsrSug isoqjk wjia:djka u;=fjs'
WodyrKhla f,i i;sfha osk 7 ysoS jd;fha wdrAo%;dj iusnkaO mrs.Kl jevigykla ,sjSug wjYH
kus" tu wdrAo%;d w.hka 7 .nvd lr.ekSug isoqfjkq we;' fuh lsrSu i`oyd my; mrsos variables 7 la
iEoSug isoqfjkq we;'
==========================================================
float humidMon = 3.3, humidTue = 3.12, humidWed = 2.99,
humidThu = 3.01, humidFri = 3.00, humidSat = 2.98,
humidSun = 2.91;
==========================================================
oeka fuu w.hka mdjspspshg .ekSug wjYH jQ jsg fuu variables tlska tl fjk fjku 7 jrloS
Ndjs; l,hq;=h' Tng fmfkk mrsos fus wdldrfha tlu .Kfha o;a; fm,la iu. lghq;= lsrSfusoS
variables ixLHfjs we;sjk by,hdu ksid Tng tajd Ndjs;d lsrSfus wmyiq;djhkag uqyqKoSug isoqfjs'
fuh jvd;a meyeos,sjkafka Tng lghq;= lsrSug isoqfjk o;a; m%udKh by, hkjsghs' WodyrKhla
f,i by; i`oyka jevigyfkys i;sfha osk 7 ys muKla fkdj jifrA osk 365 ysu wdrAo%;djka .nvd
lr.ekSug isoqjQfhahehs is;kak' tjsg lghq;= Tfns md,kfhka f;drj hdu werfTs' lsisfjl=g
variables 365 la jeks jsYd, ixLHdjla iEoSug fyda tjeks variables ixLHdjla iu. fjkfjku lghq;=
lsrSug yelshdjla ke;s nj Tng jegfykq we;' tfiakus wm fujeks jsYd, o;a; ixLHdjka md,kh
lrkafka [email protected] wm Arrays mdjspsps lrkafka fus i`oydh'
Array tlla hkq tla fmdoq lreKla yd iusnkaO o;a; fm,la tlafldg ksmojkq ,enQ tallhls' fj,`o
ie,l i;sfha osk y; ;=,oS" tla tla oskfha ,enqKq wdodhu" ixhqla; ;egshl we;s .S; j, kus
,ehsia;=jla" ls%vlfhla lsishus ls%vdjla l, jdr .KfkaoS ,nd.;a ,l=Kq j, w.hhka" jeks o;a;
Arrays ;=, .nvd l,yels foa i`oyd WodyrKhkah' Array tlla ;=, .nvd lr we;s iEu o;a;hlgu
ia:dkhla fyj;a index number tlla we;' Tng Array tlla ;=,g w,q;a o;a; we;=,a l, yelshs
^WodyrKhla f,i wdydr jgsfgdarejlg ;j;a tla wdydrhl kula tla lsrSu i,lkak&' ;sfnk o;a;
fjkia l,yelshs ^kus ,ehsia;=jl lsishus ia:dkhl we;s kula fjkia lsrSu i,lkak&' lsishus
o;a;hla bj;a lr oeush yelshs ^Tfns us;=rkaf.a e-mail ,smsk ,ehsia;=fjka tla wfhl=f.a ,smskhla
bj;a lr oeuSu i,lkak&' fus iEufohlau isoqlsrSug Tn by; i`oyka l," wod, o;a;fha index
number tl oek.; hq;=h' Index number tl u.ska isoqlrkafka wod, o;a;h iu. Array tl
udrA.fhka iusnkaOhla we;s lsrSuhs' Arrays iu. lghq;= lsrSu fndfydajsg myiqhs' tfiakus" Arrays
ms,sn`o ;dCIKsl lreKq fu;eka isg i,ld ne,Su wrUuq'
Arrays ms,sn`oj l:d lsrSfusoS m%Odk wjia:d 3la i,ld ne,sh hq;=hs:
1. Arrays ksmojSu
my; oelafjkafka array tlla iEoSfuoS fyj;a define lsrSfusoS Ndjs; lrK jHdlrKhhs'
===========================================================
data type array name [length] = { data 0, data 1, data 2, ...... }
45
===========================================================
by; Tng fmfkk mrsos array tlla define lsrSfuoSo m,uqj fhfokafka tys wvx.= jk o;a; j,g
.e,fmk data type tlh' fojkqj array tflys ku fhfoa' f;jkqjg yuqjk fldgq jryka jeo.;ah'
fuu fldgq jryka ;=, fhosh hq;af;a array tflys length tl fyj;a tys Odrs;djhs' Array tll
Odrs;dj hkq tu array tflys we;s o;a; ixLHdjhs' by;oS oelajQ i;sfha osk 7 ys wdrAo%;dj ms,sn`o
WodyrKfhaoS tu array tflys Odrs;dj fyj;a length tflys w.h jkafka 7 hs' ulaksidoh;a array tl
;=, wdrAo%;d w.hka 7 la wka;rA.; jk nejsks' fuksid tysoS tu array tl ksrAudKh lsrSfusoS jryka
;=, fhosh hq;af;a 7 w.h h' fufia wm array length tl i`oyka l,hq;af;a" compiler uDoqldx.h jsiska"
iEosh hq;= array tflys Odrs;dj oek.;hq;= ksidh' Odrs;dj oek.;a jsg" compiler uDoqldx.hg array
tlg wjYH jk u;l mrsudj RAM ;=,ska ksjerosj fjkalr .; yelsh ^wjYHjk uq,q RAM u;l
Odrs;dj fidhd.kafka array tflys wvx.= jk o;a; jrA.fha jsYd,;ajh fyj;a o;a; jrA.fha bit
mrsudj" array tflys wvx.= o;a; ixLHdfjka jevslsrSfuks' wdrAo%;d WodyrKfhaoS we;af;a int jrA.fha
o;a; 7 la ksid int ys jsYd,;ajh jk 4 bit w.h 7 ka jevsl, hq;=h' tjsg tu array tflys jsYd,;ajh
fyj;a size tl f,i 28 bit w.h ,efns' fuu array size hkak array tflys length tl iu. mg,jd
fkd.kak' array length hkq array tflys we;s o;a; ixLHdj jk w;r size hkq tu array tl RAM
;=,ska fjkalr.kakd u;l m%udKhhs' uS,.g “ = ” ,l=K fhoSu u.ska" wjYH jk o;a;hka array tl
;=,g wdfoaY lr" array tl define lsrSu isoqfjs' fuysoS i., jryka ( {} ) Ndjs;d lr we;s njg Tng
oelsh yelsh' i`., jryka mdjspsps lsrSug isoqjkafka fuysoS wm lghq;= lrkafka o;a; tllg jvd jevs
ixLHdjla iu. jSu fya;=fjks' fuu i., jryka ;=, wm array tflys .nvd l, hq;= o;a; wka;rA.;
lruq' by; igyk foi n,k jsg Tng oel.; yels ;j;a fohla jkafka i`., jryka ;=, wvx.=
o;a; wdrusN jkafka 0 ka njhs (data 0)' bkamiq tajd data 1, data2 f,i by,g jefvhs' fjk;a jpk
j,ska lsh;fyd;a array tll index tl wdrusN jkafka 0 fjks - 1 ka fkdfjs' fus wkqj array tll
m,uq o;a;fhys index number tl 0 jk w;r fojekafkys index number tl 1 fjs' f;jekak 2 fjs'
Array tll length tl fyj;a array tflys wvx.= l, yels o;a; ixLHdj fidhd.; yels l%uhla kus
tys we;s wjika o;a;hg ysus index number tlg 1la tl;= lsrSuhs'
Array length = Last index number + 1
ta wkqj" wjika o;a;fha index number tl 10 jk array tll length tl 11 la fjs" tkus tys o;a;hka
11 la .nvd l, yelsfjk w;r tu array tl define lsrSfusoS Tn tys fldgq jryka ;=,g fhosh hq;=
w.h 11 jkq we;'
by; lS lreK ;j;a whqrlska lsjfyd;a : Length tl 10 jk Array tll Wmrsu index number tl 9
jk w;r tys .nvd l, yels Wmrsu o;a; ixLHdj 10 ls'
by; array tflys i., jryka ;=, we;s o;a; tlska tl fjka lr we;af;a fldud j,ska nj ksrSCIKh
lrkak' Array tll w.hka 2 la w;r fldud fhoSug iEujsgu u;l ;nd .kak'
Array tll wvx.= o;a; iy tajdfha memory addresses
Array tll o;a; wvx.= jkafka tla fm,lg njo tajd tlla msgqmi ;fjlla jYfhka ixjsOdkhj
we;s njo Tn by;oS oqgqjd' fuu o;a;" tllg miqj ;fjlla jYfhka define lrk ,o variables
fm,lg iudkfjs' kuq;a array tl u.ska fuu variable fm, tla tallhlg f.dkq lr we;s ksid tu
TSkEu variable tllg array tflys index number tl u.ska ,.d jSug yelshdj ie,iS we;' Array
tlla ;=, we;s variables fm,la RAM u;lh ;=, we;af;ao tla fm,la jYfhka tlsfklg
iuSmfhkqhs' fusksidu tu o;a;hkaf.a memory addresses oelafjk hexadecimal ixLHdjkao tlsfklg
fndfyda fia iudkfjs' ^fuu o;a; fm, iudk jkafka ienE f,dalfha we;s ksjdi fm,lgh'
tlsfklg ,xj boslr we;s ksjdi j, ,smskhka fjkia jkafka iq,q jYfhks&' tla fm,lg we;s o;a;
fyj;a variables foll memory addresses tlsfklska fjkia jkafka tu variables wh;a data type
tflys jsYd,;ajhg iudk w.hlsks' WodyrKhla f,i array tlla ;=, tl ,S.ska we;s num1 iy
num2 keue;s int variables folla i,lkak' num1 ys memory address tl olajk hexadecimal ixLHdj
46
0012FF70 hk w.h hehs is;kak' int o;a; jrA.fha variable tll jsYd,;ajh 4 bit ksid num2 variable
tflys address tl num1 ys address tlska fjkia jkafka 4 lsks' tkus" tys address tl jsh hq;af;a"
0012FF70 w.hg 4la tla lsrSfuka ,efnk 0012FF74 hk w.hhs' fuf,iskau" array tflys B,.g
num3 kuska ;j;a variable tlla we;skus" tys address tl jshhq;af;a 0012FF74 w.hg 4 la tla
lsrSfuka ,efnk 0012FF78 hk ixLHdjhs'
2. Array tll wka;rA.; w.hka ,nd.ekSu
Array tll we;s lsishus w.hla ,nd.ekSug m%:ufhka" tu w.fhys index number tl oek.; hq;=h'
Index number tl okakd jsg int array tllska w.hla ,nd.ekSug Ndjs;d lrK jHdlrK jsOsh my;
oelafjs:
===============================================
int value = arrayname[index number];
===============================================
by;" value keue;s int variable tlg array tflys index number kuska oelafjk ia:dkfha we;s w.h
wdfoaY fjs' Index number tl we;=,a l, hq;af;a array tflys kug bosrsfha we;s fldgq jryka ;=, nj
ksrSCIKh lrkak'
3. Array tllg w.hka we;=,a lsrSu
fuh by; ls%hdjg jsreoaO wjia:djhs' my;ska th oelafjs:
===============================================
int value = 10;
arrayname[index number] = value;
===============================================
by;oS" value ys w.h array tflys index number hkqfjka oelafjk ia:dkhg wdfoaYfjs'
Arrays iusnkaOfhka by; oelajQ ls%hdjka 2 l m%dfhda.slj fmkajdfok mrs.Kl jevigykla my;
oelafjs:
===============================================
#include<iostream.h>
void main()
{
int numbers[10];
for(int x=0; x<10; x++){
numbers[x] = x;
}
for(int y=0; y<10; y++){
cout<< numbers[y] << endl;
}
47
}
===============================================
jevigyfkys m,uqj number keue;s Odrs;dj 10 la jk array tlla define lr we;' fusksid fuu
array tl;=, .nvd l, yels o;a; j, Wmrsu index number tl jkafka 9 nj Tng fmfka'
tfiajkafka fmr lshQ mrsos array index tl wdrusNjkafka 0 isg ksidh' wdfoaYfhaoS for ;=, index
number tl f,i Ndjs;d lr we;af;a x ys w.hhs' wdfoaYjkafkao x ys w.hhs:
numbers[x] = x;
for loop tl ls%hd;aul jSfusoS array tlg 0 isg 9 olajd w.hka 10 wdfoaYfjs' index 0 ia:dkhg
wdfoaYjkafka 0 hs' index 1 ia:dkhg wdfoaYjkafka 1 hs' wjidkfha index 9 ia:dkhg wdfoaYjkafka 9
hs:
numbers[0] = 0;
numbers[1] = 1;
....
....
numbers[9] = 9;
fojeks for loop tl index number tl f,i y Ndjs;d lr array tflys we;s o;a; ,ndf.ksuska tajd
console window tl u; print lrhs' jevigyk ls%hd;aul l,jsg ,efnkafka my; oelafjk wdldrfha
output tlls'
====================================
0
1
2
3
4
5
6
7
8
9
Press any key to continue
====================================
by; f,i index numbers u.ska array tll wka;rA.; o;a; iu. lghq;= lsrSu “Array indexing”
f,i yeoskafjs'
by; jevigyfkys w.hka wdfoaYh for loop tlla Ndjs; fkdlr Rcqj i`., jryka Ndjs;fhka
isoql,fyd;a th my; wdldrh .kshs:
===============================================
#include<iostream.h>
void main()
{
int numbers[10] = {0,1,2,3,4,5,6,7,8,9};
for(int y=0; y<10; y++){
cout<< numbers[y] << endl;
}
}
48
===============================================
char arrays
char array tlla hkq characters j,ska iEoqkq array tllah' fus.ek jsfYaIfhka l:d lsrSfus fya;=j kus
char arrays j, jsfYaI m%fhdackhla ;snSuhs' fmroS" strings Ndjs;djkafka characters iuQyhlska ieos
character fma,s" ke;skus jdlHhka .nvd lsrSug nj Tn oqgqjd' char array tlla hkq fuu string data
type tlg wdfoaYlhls' ksjerosju lsjfyd;a string tlla we;af;kau iEoS we;af;a char array tlla
u.sks' kuq;a fuh isoqjkafka wmg fkdfmfkk f,isks' WodyrKhla f,i Tn my; oelafjk string
tl ksrAudKh l,d hehs is;kak'
string message = “System error!”;
by; message string tl hkq my; oelafjk message char array tlu jkq we;:
char message[13] = {‘S’, ‘y’, ‘s’, ‘t’, ‘e’, ‘m’, ‘ ’, ‘e’, ‘r’, ‘r’, ‘o’, ‘r’,
‘!’};
Array tflys “System” yd “error!” hk jpk LKav hq., w;r ysvei ;nd we;af;a ysia character tlla
u.ska nj ksrSCIKh lrkak' kuq;a char array tlla iEoSug" jvd myiq" ksjeros l%uhla we;' my;
oelafjkafka fuhhs:
char message[] = "System error!";
tysoS i., jryka bj;ajS we;s w;r" fjk fjku characters fm,la fjkqjg Rcqju text string tlla
array tlg wdfoaY lr we;' ;jo array tflys Odrs;dj olajk fldgq jryka ;=, ixLHdjla i`oykaj
fkdue;' tkus" fuf,i char array tlla iEoSfuoS wm tys Odrs;dj i`oyka l,hq;= ke;' char arrays
iusnkaO ;j;a ,CIKhla ;sfns' wm System error! hkak array tlg wdfoaY l, jsg" tu text string tl
wjidkjk ;ekg tla ia:dkhlg Tnsfnka bfnsu ysia character tlla wdfoaYfjs' tkus ‘ ! ’ we;s
ia:dkhg Tnsfnka fuu ysia character tl array tl ;=, ;ekam;afjs' fuys m%fhdackh kus" tu.ska
array tl;=, we;s text string tl ksujk ia:dkh y|qkd.ekSug myiq jSuhs' fuu ysia character tl
ye`oskafjkafka “null character” ke;skus “null terminator” hk kusks' fuh y|qkd.ekSfus ,l=K ‘
\0 ’ fjs'
kuq;a Tng we;eusjsg kef.k m%YaKhla jkq we;af;a" string iy char arrays hkqfjka iudk wldrfha
data type folla iEoSug fya;=jqfka l=ulao hkakhs' C++ hkq C mrs.Kl NdYdfjs l=vd fidfydhqrdh'
tkus" C++ ksmojd we;af;a C NdYdj moku jYfhka ;ndf.kh' char arrays hkq uq,a C ksrAudmlhd
jsiska text strings .nvd lsrSu msKsi ksrAudKh lrk ,o data type tlh' kuq;a char arrays ys Ndjs;fha
we;s wmyiq nj oqgq miq ld,Sk C++ ksrAudmlhd jsiska string hk data type tl text strings iu. lghq;=
lsrSu myiq lsrSu i`oyd y`oqkajd oqkafkah' kuq;a Tyq C ;=, ;snQ char arrays Ndjs;h C++ ;=,o
;sfnkakg yersfhah'
my; jevigykska oelafjkafka char array tlla idok wdldrh yd tys null character tl Ndjs;d lr
we;s wdldrhhs:
===============================================
// char array.cpp
#include <iostream.h>
void main()
{
49
char message[] = "Thank you!";
int i = 0;
while(message[i] != '\0')
{
cout << message[i];
i++;
}
cout<<endl;
}
===============================================
by; jevigyfkys “Thank you” hk w.fhka hq;a char array tlla idod we;s w;r tys we;s
text string tl while loop tlla Ndjs;d lr array indexing u.ska print lr we;' while u.ska
text string tflys wjidkh i`oyd mrSCId lr we;s wdldrh n,kak:
while(message[i] != '\0')
message array tl;=, hus ia:dkhloS null terminator tl yuqjqk jsg" loop tl ns`osf.dia
“Thank you!” hkak print lruska jevigyk ksudjg m;ajSu isoqjk nj Tng fmfkkq we;'
fuf,i null terminator tl u.ska char array tlla wjika jk ia:dkh fidhd .; yelsh'
7. Pointers
wm variables Ndjs;d lr mrs.Kl jevigyka ,shk wdldrh by;oS i,ld ne,qfjuq' variables
Ndjs;d lrkafka mrs.Kl u;lfha we;s lsishus o;a;hla ksfhdackh lsrSu i`oyd nj tysoS oqgsuq'
pointers Ndjs;d lrkafkao fus ldrKhguh' kuq;a tla jeo.;a fjkila pointers iy variables w;r
we;'
fuu fjkialu l=ulaoehs y`oqkd.kakg kus variables yeisfrk iajNdjh meyeos,s lr.; hq;=h'
variables ys iajNdjh meyeos,s lr.; yels fyd`ou wdldrhla kus" ;u argument tl f,i variable
tlla ,nd.kakd function tlla ms,sn`oj i,ld ne,Suhs' my; oelafjk jevigyk foi n,kak:
========================================================
//usingVariables.cpp
#include<iostream.h>
void change(int dat);
void main()
{
int data = 100;
50
change(data);
cout << data << endl;
}
void change(int dat)
{
dat = 200;
}
========================================================
by; jevigyfkys oelafjk change keue;s function tlska isoqjkafka" th argument tl f,i
,nd.kakd variable tflys w.h fjkia lr fjk;a w.hla thg wdfoaY lsrSuhs' m,uqj" main function
tl ;=, w.h 100 la jk data keue;s variable tlla ;kd we;s w;r fuu variable tl change ;=,g
pass lr we;' oeka" change function tl u.ska tu pass l, w.h 200 njg fjkia lrk nj Tng
oelsh yelshs' Tn fuu jevigyk ls%hd;aul l,fyd;a main ys cout u.ska print lrkafka 100 nj
oel.; yel' tkus" change function tl" data u.ska thg pass l, w.h fjkilg ,la l,d hehs lshd"
data ys w.h fjkia jQfha ke;' fuhg fya;=j oel.ekSug kus" function tlla ;=,g variable tlla
pass lrk jsg isoqjkafka l=ulaoehs fidhdne,sh hq;=h'
Function tlla ;=,g lsishus variable tlla pass lrk jsg isoqjk m,uqjk foh kus RAM u;lh ;=,
we;s tu variable tflys wkqmsgm;la RAM ;=, ksrAudKh jSuhs' bkamiq" function tl ;=,g w.h pass
jSu isoqfjs' kuq;a w.h f,i pass jkafka by; ksmojqkq uq,a o;a;fha wkqmsgm;hs' - ienE uq,a o;a;h
fkdfjs' fuksid function tl tu o;a;fha w.h fjkia l,o fjkig ,lajkafka tu wkqmsgmf;ys
w.h muKhs' by; jevigyfkka ,ndfok m%:sM,hg fya;=j by; lshQ ls%hdjhs' change function tl
,nd.kafka data variable tl fkdj tys wkqmsgm;ls' fuu wkqmsgm; kus dat variable tlhs' fusksid
th u.ska fjkia lrkafka dat ys w.hhs' data variable tflys w.h fjkilg Ndckh fkdjS 100
f,iu mj;shs'
Tn by; lshjQ lreKska variables iusnkaO m%Odk ,CIKhla wkdjrKh fjs' th kus variables u.ska
mrs.Kl o;a; j,g imhk wdrCIdjhs' Tn fudfyd;lg fmr oqgq mrsos" variables muKla Ndjs;hg
.kakd function tllg mrs.Klfha we;s o;a; iu. Rcqj lghq;= lsrSfus yelshdjla fkdue;' thg
ieujsgu ls%hdlsrSug isoqjkafka o;a; j, wkqmsgm;a iu.h' fusksid function tllg mrs.Klh ;=,
we;s ienE o;a; iu. is;=jla lsrSug fkdyelsh' fuu o;a; iu. “is;=jla lsrSug” we;s fkdyelshdj
ksid variables Ndjs;fhka muKla lsisfj;=g virus iy worms jeks jevigyka ksrAudKh lsrSug
fkdyelsjS we;' ^virus iy worms hkq" wjirhlska f;drj lsishus flfkl=g wh;a mrs.Klhl we;s
o;a; fjkia lsrSfus" uldoeuSfus fyda fjk;a lsishus wkjir mrs.Kl l%shdjla isoqlsrSfus yelshdj we;s
mrs.Kl jevigkah&' kuq;a ljfrl= fyda variables muKla Ndjs;fhka mrs.Kl jevigyka ,shhs kus
Tyqg mrs.Klfha o;a; iu. Rcq iusnkaOkaO;djla meje;ajSug variables u.ska bv fkdie,iSu
fya;=fjka Tyqg C++ jeks mrs.Kl NdYdjlska kshu m%fhdackh .ekSug fkdyelsj hhs'
by; i`oyka" Variables iusnkaOfhka we;s fuu iSus; nfjka u`oSu i`oyd C++ ;=, we;s tallh jkafka
pointer h. Pointers u.ska Tng variables u.ska bv fkd,efnk ish,a, lsrSfus wjia:dj ,ndfoa'
tfukau pointers fya;=fjka" variables u.ska mrs.Kl o;a; i`oyd fhfok wdrCIl lfmd,q bj;ajS hhs'
wm fu;eka isg pointers yeisfrk iajNdjh jgyd.ekSug W;aidy .ksuq'
Pointers j, ls%hdj kus mrs.Kl o;a; fj; Rcqj iusnkaOjSu nj Tn fudfyd;lg fmr lshjQjd'
ienE f,dalfha oS lsishus mqoa.,fhl= iu. lsishus iusnkaO;djla meje;ajSug kus f,dj ;=, Tyq isgsk
ia:dkh m%:ufhka fidhd.; hq;= h' fufiau" mrs.Kl o;a; yd iusnkaO;djla meje;ajSugo" m,uqj
wm tu o;a; RAM u;lh ;=, msysgs ia:dk fidhd.; hq;=h' kuq;a fuu ia:dk wm fidhd.kafka
[email protected]
fuu fmdf;ys uq,a Nd.fhaoS" mrs.Klfha o;a; wvx.= jkafka RAM ;=, njo" tys wvx.= o;a; j,g
,smskhka fyj;a memory addresses we;s njo i`oyka jsKs' ^fuh Tfns u;lfhka .s,syS we;akus
lreKdlr kej; “RAM” keue;s fldgi lshjkak&' lsishus mqoa.,hl= cSj;ajk ia:dkh fidhd.ekSug
Tyqf.a ;eme,a ,smskh mdjspspshg .;yels fia mrs.Kl o;a; we;s ia:dk fidhd.ekSugo by; lshQ
51
tajdfha “u;l ,smsk” fyj;a memory addresses Ndjs;d l, yelsh' mrs.Kl jevigykloS" lsishus
variable tlla u.ska ksfhdackh flfrk o;a;hl memory address tl ,nd.ekSu b;d myiqhs'
Pointers iusnkaOj lreKq 2la i,ld n,uska wm fus .ek l:d lruq'
1' mrs.Kl o;a;hl memory address tl ,nd.ekSu
Variable tll kug fmr ‘&’ ,l=K fhoSfuka Tng tu variable tl ksfhdackh lrk o;a;fha
,smskh ,efns' my; oelafjkafka fuhhs:
=======================================================
#include<iostream.h>
void main()
{
int data = 100;
cout<< &data << endl;
}
========================================================
by;" cout jsOdkh wvx.= fma,sfhys data ys kug bosrsfhka ‘&’ ,l=K fhdod we;' thska ,efnkafka
data ys memory address tl fyj;a data keue;s variable tl, RAM ;=, msysgs ia:dkfha ,smskhhs' Tn
fuu jevigyk ls%hd;aul lf,d;a my; jeks oyifha mdofha fyj;a hexadecimal >Khg wh;a
ixLHdjla print fjkq olakg ,efnsjs' ^Tng ,efnkafka fjkia w.hla jshyelsh' kuq;a fuysoS
ksrSCIKh l,hq;= foh kus fuu memory address tflys iajNdjhhs' th yeujsgu hexadecimal
jrA.fha ixLHdjls'&
=========
0012FF7C
=========
2' Pointer tlla declare lsrSu iy memory address tlla wdfoaY lsrSu u.ska tu pointer tl define
lsrSu'
Variable tlla ;=, .nvd l, yelafla lsishus w.hla jQ fiau" pointer tll .nvd l, yelafla
memory address tlls' wm “o;a; jrA.hka” keue;s mrspsfpsofhaoS l:dl, wdldrhg" mrs.Kl
o;a;hka" int, char jeks o;a; jrA. j,g fnfok ksid tajdfha addresses .nvd lsrSug ;eksh hq;af;ao
wod, o;a; jrA.hg wh;a pointers h' fusksid wmg int pointer, char pointer, string pointer wdosh ms,sn`o
l:d lsrSug isoqfjs' Pointer tlla ksrAudKhg fyj;a declare lsrSu i`oyd jk fla;ho tla lreKlska
yefrkakg variable declaration wjia:dfjsoS fhfok fla;hg fndfyda iudkhs' fjki kus pointer
declaration ysoS pointer tflys kug bosrsfhka ‘ * ’ (asterisk) i,l=K fhoSug wjYH jSuhs' my;ska
oelafjkafka fuf,i int jrA.fha pointer tlla declare lr we;s wdldrhhs:
========================================================
52
int *p;
========================================================
fus wkqj" p hkq int pointer tlla f,i ye`oskafjs'
oeka wm p pointer tl declare lr we;s ksid uS,.g th define l, hq;=h ^tkus" pointer tlg w.hla
wdfoaY fldg tys meje;au mrs.Kl u;lh ;=, ia:sr l, hq;=h&' Pointers ;=, .nvd l,yelafla
memory addresses jSu ksid pointer defenition wjia:dfjsoS wm thg wdfoaY l,hq;af;a memory address
tlla fjs' my;ska oelafjkafka fuhhs:
========================================================
int *p;
int dat = 120;
p = &dat;
========================================================
by;" dat kuska int variable tlla define lr we;s w;r ;=kajeks fmf,ysoS ‘&’ Ndjs;d lsrSfuka dat ys
address tl p pointer tlg wdfoaY lr we;' fuysoS Tn ksrSCIKh l, hq;= m%Odk fohla kus
wdfoaYfhaoS pointer tl (p) bosrsfhka ‘ * ’ i,l=K fkdfhfok njh' Pointer tl yqfola p f,i Ndjs;d
lr we;' fus wkqj" pointer tlla define lrk jsg thg ‘ * ’ ,l=K fhfokafka ke;'
oeka p ;=, address tlla .nvd jS we;s ksid wmg tu address tlg wod, o;a;h iu. p pointer tl
udrA.fhka Rcqj iusnkaO jsh yel' tkus fus wjia:dfjsoS" p pointer tl, dat iu. dat ys memory
address tl udrA.fhka Rcqj iusnkaOjS we;' oeka wmg p u.ska" dat ys w.h ,nd.ekSu jeks foa isoql,
yel' my; oelafjkafka th isoqlrk wdldrhhs:
========================================================
int *p;
int dat = 120;
p = &dat;
int number;
number = *p;
cout << number << endl;
========================================================
by; jevigyfkaoS" number keue;s int variable tlla ksujd we;s w;r" number = *p hk fla;h
u.ska isoqjkafka number ;=,g dat ys w.h we;=,a jSuhs' fuysoS p pointer tl, dat fjkqjg wdfoaYlhla
f,i ls%hdlrhs' tkus" number = *p hk fla;h fjkqjg number = dat hkak fhoqjfyd;a
isoqjkafkao tlu foh jk number fj; dat ys w.h wdfoaY jSuhs' fuh isoqjkafka p ;=, .nvdjS we;s
dat ys memory address tl ksidh' Pointer tflys ldrAhh kus th ;=, we;s fuu address tl Ndjs;d
fldg tu address tl ysus o;a;h iu. iusnkaOhla we;s lsrSuhs ^fyj;a point lsrSuhs' Pointer tlg tu
ku ,enS we;af;a fus fya;=fjks&' fuysoS kej;" p Ndjs;d jkafka ‘ * ’ (asterisk) ,l=K iu. nj Tng
fmfkkq we;' number = *p hk fla;fha *p u.ska dat ys w.h ,nd fohs' fusksid oeka number ;=,
we;af;ao cout u.ska print jkafkao 120 w.hhs'
by; oelajQ pointer tl declare lsrSu" pointer tlg memory address tlla wdfoaY lr define lsrSu" iy
pointer tl u.ska wod, w.hka ,nd.ekSu hk wjia:d ;=fkka" pointer tl iu. ‘ * ’ (asterisk) ,l=K
Ndjs;d jkafka m,uq yd f;jk wjia:d j,oS muKla nj Tng oelsh yel' Pointer definition ys
wdfoaYfhaoS ‘ * ’ i,l=K Ndjs;d jkafka ke;'
53
oeka" fus jkjsg TskEu wdrusNlfhl=g fukau Tngo yef.kq we;af;a fuu pointers Ndjs;fhaoS
fhdod.kakd jHdlrK(syntax) u`oska u`o ixlSrAK njg m;ajk njhs' fusksid fuu jHdlrK isys;nd
.ekSfus l%uhla jYfhka" my;" pointer syntax oelafjk igyk Tng bosrsm;a flfrA' tu igyk
fld,hl ,shd Tfns mrs.Klh wi, ns;a;sfha w,jd .kak' tys we;s jHdlrK Tfns weig yqrejk ;=re
thg ns;a;sfha t,a,S isgSug bv yrskak'
int i = 10, j; // int variables
int *ip;
// int pointer
ip = &i; // int variable tflys address tl int pointer tlg wdfoaY
// lsrSu
j = *ip; // int pointer tflys wvx.= address tl ysus int variable
// tflys w.h ( j ;=,g) ,nd.ekSu
*ip = 20; // int pointer tflys wvx.= address tl ysus int variable
// tflys (i ys) w.h (20 njg) fjkia lsrSu
by; oelajQfha pointers yd iusnkaO jHdlrK j,ska fldgila muKs' b;srs lreKq fus wjia:dfjsoS wm
idlpspd lsrSug fkdhuq'
fus jkjsg wm jsuid n,d wjika lf,a pointers Ndjs;d lr o;a; fj; Rcqj ,.djSfus uQ,sl
ls%hdj,shhs' kuq;a pointers ys kshu ls%hdldrS;ajh oelSug kus wm pointers Ndjs;d lr ,shkq ,enQ
function tlla ljrdldroehs mrSCId lr ne,sh hq;=h' fus i`oyd" wm wjidkhg i,ld ne,Q change
keue;s function tl wvx.== usingVariables hkqfjka ye`oskajqKq jevigyk pointers Ndjs;fhka
kej; ilid my;ska bosrsm;a lr we;:
========================================================
//usingPointers.cpp
#include<iostream.h>
void change(int *dat);
void main()
{
int data = 100;
change(&data);
cout << data << endl;
}
void change(int *dat)
{
*dat = 200;
}
========================================================
by; usingPointers keue;s jevigyk ms,sn`o l:d lsrSug m,uqj" wm pointers Ndjs;d fkdlr ,shk
,o usingVariables jevigyfkys isoqjQfha l=ulaoehs blaukska kej; jsuid n,uq' lreKdlr oeka
tu jevigyk we;s msgqjg wdmiq f.dia kej;jrla th ms,sn`o u;lh w,q;a lr.kak'
54
tysoS change keue;s function tl data ys w.h ;u argument tl f,i ,nd.kakd w;r tys w.h
fjkia lrhs' kuq;a Function tl ;=,g pass jkafka data ys wkqmsgm;la muKla ksid uq,a data variable
tflys w.h fjkiajSulg ,la fkdfjs' fuksid main ;=, we;s cout jsOdkh u.ska print jkafka 100
w.huhs' oeka wm by; oelafjk usingPointers keue;s jevigyk foi fk;a fhduq l,fyd;a" tysoS
change function tl ;u argument tl f,i ,nd.kafka variable tlla fkdj pointer tlla nj Tng
fmfkkq we;s' fuh Tng change ys function declaration tflysoS change ys jryka ;=, we;s int *dat
hkafkka meyeos,sfjs'
==========================
void change(int *dat);
==========================
tkus" wm change function tlg pass l,hq;af;a memory address tlls' fuh isoqlr we;s wdldrh main
function tl;=, oelsh yel' m,uqj data keue;s int variable tlla idod we;s w;r bkamiq tys address
tl pass lruska change fj; call lr we;'
==========================
change(&data);
==========================
change ;=, isoqjkafka [email protected] change function tl, *dat = 200 hk fla;h u.ska" dat ;=, we;s
address tl ysus data keue;s variable tl fj; ,.djS tys w.h ;SrKd;aul f,i fjkia lrhs' fuh
variable tllg l, yels fohla fkdjk w;r pointer tll yeisrSfus m%Odk ,CIKh fuhhs' ta wkqj
wjidkfhaoS cout u.ska data ys w.h print lrkq we;af;a 100 fkdj 200hs'
Arrays i`oyd pointers
Arrays hkq fudkjdoehs Tn “Arrays” mrspsfpsofhaoS bf.k.;a;d' Tn W.;a fuu arrays i`oyd pointers
iEoSu ms,sn`oj oeka i,ld n,uq'
lsishus array tllg pointer tlla iEoQjsg tu pointer tl Ndjs;d fldg array tl;=, we;s ish,q
o;a;hka iu. iusnkaO jsh yelshs' Array tlla i`oyd pointers iEoSu fowdldrhlg isoql, yel' my;
oelafjkafka fuu wdldrfol Ndjs;dfldg" int array tlla i`oyd pointers folla idod we;s wdldrhhs'
Arrays i`oyd pointers iEoSu
=========================================
int numbers[5] = { 23, 32, 44, 68, 11 };
int* numberP1 = &numbers[0]; // (1)
int* numberP2 = numbers;
// (2)
=========================================
^1& wjia:dfjsoS isoqjkafka l=ulaoehs Tng we;eus jsg jegfykq we;' tysoS numberP1 keue;s pointer tl
define lr we;s w;r thg numbers kus array tflys index 0 jk ia:dkfha we;s o;a;fhys memory
55
address tl wdfoaY lr we;' numbers[0] hkafkka index 0 jk o;a;h olajk w;r &numbers[0] hkak
u.ska tu o;a;fha memory address tl ,ndfoa'
by; oelajQfha array pointer tlla iEoSfus m,uq l%uhhs' tkus" wod, array tflys o;a; jrA.hg wh;a
pointer tlla idod thg array tl ;=, we;s m,uq o;a;fha address tl thg wdfoaY lsrSuh'
^2& wjia:dfjsoS isoqjkafka [email protected] tysoS" “&” iy jryka bj;a lr we;' we;af;kau fuysoS isoqjkafkao
^1& wjia:dfjoS isoqjQ fohuhs' tkus numberP2 hk pointer tlg numbers array tflys m,uq o;a;fha
address tl wdfoaYfjs' fuh yqfola ^1& wjia:dfjs isoqjk fohu ir,j lsrSuls' fusksid fuu
fowdldrfhka Tn leue;s wdldrhla array pointers iEoSfusoS wkq.ukh lrkak'
Array pointer tlla m%fhdackhg .ksuska array tlla ;=, we;s o;a; iu. lghq;= lsrSu
arrays i`oyd pointers idokafka flfiaoehs Tng oeka jegfya' oeka wm iQodkus jkafka tu iEoQ
pointers, arrays iusnkaOfhka Ndjs;d lrkafka flfiaoehs fidhd ne,Sughs'
Tn fudfyd;lg fmr array pointer tlla ksmojk wjia:dfjsoS isoqlf,a array tflys 1jeks o;a;fha
address tl wvx.= pointer tlla ksrAudKh lsrSuh' oeka Tn oek.;hq;= jkafka fuu pointer tl u.ska"
array tflys we;s wfkl=;a o;a; ,nd.ekSu" tajd fjkia lus j,g ,la lsrSu wdoS foa isoqlrk wdldrh
ms,sn`ojhs' uq,skau wm array tll 2 jeks o;a;h fyj;a index 1 ia:dkfha we;s variable tl fj; array
pointer tlla u.ska msjsfikafka flfiaoehs n,uq' my; oelafjkafka int array tlla i`oyd pointer tlla
iEoQ wjia:djls:
==================================================
#include<iostream.h>
void main()
{
int numbers[5] = { 23, 32, 44, 68, 11 };
int* number = numbers;
cout<< number << endl;
}
==================================================
Tn W.;a mrsos oeka number keue;s pointer tl;=, wvx.= jkafka numbers keue;s array tflys m,uq
o;a;h (index 0) jk 23 w.h" RAM ;=, mj;sk iaa:dkfha memory address tlhs' oeka wmg wjYH
jkafka tu pointer tl u.ska array tflys 2 jk o;a;h fj; msjsiSughs' fuh isoqlrkafka [email protected]
wm l,hq;= foh jkafka by; 23 w.h RAM ys mj;sk ia:dkhg miqj we;s o;a;h fj; pointer tl
osYd.; (point) lsrSuh' tjsg pointer tl 32 w.h fj; iusnkaOjkq we;' Pointer tl ksjeros ia:dkhg
osYd.; lsrSug kus wm l,hq;= tlu foh jkafka pointer tl ;=, we;s memory address tl" 32 w.h
mj;sk ia:dkfha address tlg iudk lsrSuh' fjk;a jpk j,ska lsjfyd;a pointer tl B,. o;a;h
fj; “mekaksh” hq;=h'
Array tlla ;=, wvx.= o;a;, RAM u;lh ;=, ixjsOdkh we;af;a tl fm,la f,i ,S. ,S.ska jSu
fya;=fjka ^“Arrays” mrspsfpsofha “Array tll wvx.= o;a; iy tajdfha memory addresses” hk
fldgfiys fuh jsia;r flrsKs&" fuu “mekakSu” isoqlsrSu myiq fohla jS we;' Tn l,hq;af;a pointer
tlg 1la tl;= lsrSuhs' my; fuh isoqlrk whqre oelafjs:
==================================================
#include<iostream.h>
void main(){
56
int numbers[5] = { 23, 32, 44, 68, 11 };
int* number = numbers;
number++;
cout<< *number << endl;
}
==================================================
by;" number pointer tflys w.h 1lska by, oud we;s w;r" fus fya;=fjka th array tflys 2 jeks
o;a;h fj;g osYd.;jkq we;' oeka cout u.ska ksjeros f,iu 32 w.h print fjkq jevigyk ls%hd;aul
lsrSfusoS Tng fmfkkq we;' Tng array tflys uS,. o;a;h ,nd.ekSug wjYH kus isoql, hq;af;a
number ys w.h ;j;a 1lska by, oeuSuhs' tjsg th ;=, wka;rA.; jkq we;af;a 44ys address tlhs'
fuf,i Tng ,nd.ekSug wjYH o;a;h" array tflys msysgs ia:dkh wkqj pointer tflys w.h by,
oeush hq;=hs'
by; Tn oqgqfjs pointer arithmatics Ndjs;d lrkq ,enSfus tla wjia:djls' Tn tysoS pointer tll w.h
fjkia lsrSfuka array tlla ;=, we;s o;a; lrd ,.dfjk wdldrh oqgqjd' oeka wm pointer arithmatics
Ndjs;d fldg by; mrsos ,nd.kakd ,o o;a;hka iu." ir, .Ks;uh ls%hd isoqlrkafka flfiaoehs
mrSCId lr n,uq'
WodyrKhla f,i by; numbers keue;s array tflys f;jk o;a;fhys w.h 44 isg 50 olajd by,
oeush hq;=hehs is;uq' fuh isoqlrk wdldrh my; f,i oelajsh yel:
==================================================
#include<iostream.h>
void main()
{
int numbers[5] = { 23, 32, 44, 68, 11 };
int* number = numbers;
number+=2;
cout<< *number << endl;
(*number)+=6;
cout<< *number << endl;
}
==================================================
m%:ufhka isoqjkafka fmr mrsosu number array tl fj; pointer tlla ksmojSu u.ska nj Tng fmfka'
bkamiq" pointer tflys w.h 2 lska by, oud we;af;a th array tflys f;jk o;a;h fj; iusnkaO
lsrSu msKsih' uS,S.g tu o;a;fha w.h jk 44 print fjs' oeka" Tng yuqjkafka pointer arithmatics
Ndjs;d lsrSfuka ,shkq ,enQ expression tlls' tys jryka Ndjs;d jk nj Tng fmfka(expression tlloS
jryka Ndjs;h ms,sn`oj “Operators” mrspsfpsofha i`oyka jsKS&' fuu expression tflysoS isoqjkafka
l=ulaoehs n,uq'
hk wdldrhg by; expression tflys jryka bj;a fldg ,sjQfyd;a" precendence
kshduhkag wkqj m,uqj isoqjsh hq;af;a plus equal (+=) operator tl" pointer (*) operator tlg fmr
ls%hd;aul jSuh' * operator tl expression tflys uq,ska ;snqko th ls%hd;aul jsh hq;= jkafka +=
operator tlg miqjh' ta wkqj" m,uqj number ys w.hg 6 la tl;=jkq we;' kuq;a Tng fmfkk mrsos
wmg wjYH .Ks; lrAuh fuh fkdfjs' fuhska isoqjk tlu fohkus pointer tl array tflys ia:dk 6
*number+=6
57
la bosrsfhka jk ia:dkhg f.khduh' bkamiq tu ia:dkfha we;s w.h print fjkq we;' oeka
expression tl jryka fhdod ,shQjsg isoqjk foa n,kak' * iy number hq.,h jryka ;=, wka;rA.;
lsrSu u.ska (*number)" expression tflys m,uqj isoqjk foh f,i" number pointer tl fj; fus
fudfydf;aoS iusnkaOjS we;s w.h ,nd.ekSu isoqfjs' oeka += operator tl ls%hd;aul jS tu ,nd.;a
w.hg 6 la tl;=fjs' tkus 44 fj; 6 la tl;=jS array tflys tu ia:dkfha w.h 50 la njg m;afjs'
fuh jevigyfka fojeks cout jsOdkh u.ska ;yjqre jkq we;'
Tng by; lreKq ;joqrg;a meyeos,sjSu msKsi my; jevigyko bosrsm;a flfrA' th for loops Ndjs;d
lruska" arrays ;=, wvx.= jk o;a;h j,g we;=,afjuska tu o;a; fjkialus j,g Ndckh lrkq ,nkq
Tng oelshyels jkq we;:
=====================================================
#include<iostream.h>
void main()
{
int Lotto[10] = { 21, 2, 61, 20, 86, 44, 37, 10, 0, 1 };
int* number = Lotto;
// 1st, print the array
for(int a=0; a<10; a++){
cout<< *number++ << endl;
}
cout<< "\n";
// copy the array to a temporary array
int temp[10];
for(int b=0; b<10; b++){
temp[b] = Lotto[b];
}
number = Lotto; // point the pointer back to the
// 1st element of the array
// swap the data in the array
int c = 9;
for(int d=0; d<10; d++){
Lotto[d] = temp[c];
c--;
}
for(int e=0; e<10; e++){
cout<< *number++ << endl;
}
}
=====================================================
by; jevigyk Tn fuf;la oqgq jevigyka j,g jvd iq,q jYfhka ixlSrAK tlla nj Tng fmfkkq
we;' kuq;a fuh meyeos,s lr.ekSu b;d myiqhs' tys m,uqfjkau isoqjkafka Lotto kuska array tlla
ksrAudKh lsrSu nj Tng fmfka' bkamiq iqmqreoq mrsos tu array tl i`oyd pointer tlla ksujd we;'
bkamiq isoqjkafka array tflys we;s w.hka ish,a, for loop tlla u.ska print jSuh' wjidk w.h print
jQ miq th newline character (\n) tl u.ska Terminal window tflys kj ysia fma,shla fyj;a new
line tlla idohs' bka wk;=rej" Lotto array tflys length tlg iudk length tlla we;s temp kuska
w,q;a array tlla idod we;s w;r fuu temp array tl ;=,g Lotto array tflys ish,q o;a; for loop
tlla wdOdrfhka fldms lrkq ,efns' wk;=rej number pointer tl Lotto array tflys m,uq o;a;h
fj; kej;;a osYd.; lrkq ,efns' ulaksidoh;a tu pointer tl oeka osYd.;jS ;sfnkafka array tflys
wjidk o;a;hg fyhsks' uq,skau array tflys o;a; print lrk jsgoS pointer tflys w.h fjkiajQ nj
Tng u;l we;s' uS,.g jsfYaI fohla isoqfjs' uS,. for loop tlska Lotto ;=, we;s o;a; fm,.eiau
58
lKmsg fmr,hs' tkus" uq,ska we;s w.g;a" w. we;s o;a; uq,g;a f.khdu isoqfjs' fuh isoqjkafka by;
wm ksmojQ temp array tflys wOdrfhks' For loop tl ;=, we;s m,uq expression tflys wvx.= arrays
j, fldgq jryka ;=, we;s w.hka foi ne,Qjsg Tng fuh isoqjk wdldrh meyeos,s jkq we;' Lotto ys
jryka ;=,g d fhdod we;s w;r" temp ys jryka ;=,g c fhdod we;' d ys w.h for loop tl ls%hd;aul
jSu;a iu. 9 olajd jevsjk w;r" c ys w.h 0 olajd wvqjkq we;' temp ;=, we;af;a Lotto ;=, we;s
w.hkau nj Tng u;l we;s' fus ksid" Lotto ;=, we;s uq,a w.hka temp ;=, we;s miq w.hka iu.o
w. we;s w.hka uq,a w.hka iu.o udrejSfuka Lotto ;=, we;s w.hkaf.a fm,.eiau lKmsg fmrf,kq
we;' wjidkfhaoS fuu lKmsg yereKq w.hka print fjs' fuu jevigyk ls%hd;aul l, jsg my;
output tl ,efnkq we;:
==============================
21
2
61
20
86
44
37
10
0
1
1
0
10
37
44
86
20
61
2
21
==============================
uq,ska osiajkafka Lotto ys o;a; uq,ska msysgs wdldrh jk w;r bkamiqj we;af;a w.hka lKmsg fmr,S
we;s whqrehs'
59
8. Object Oriented Programming
^jia;= uQ,sl l%uf,aLKh&
^Object Oriented Programming ms,sn`oj fuu mrspsfpsofhys tkafka b;d uQ,sl ye`oskajSula muKla nj
i,lkak' th ms,sn`o iusmQrAK jsia;r i,ld ne,Sug tla mrspsfpsohlska fkdyelsh'&
Object oriented programming hkq [email protected]
fuu fmdf;ysoS fuf;la Tn oqgq l%uf,aLKhka wh;a jQfha procedural programming
>Khghs' fujeks mrs.Kl l%uf,aLKhka fndfydajsg l%shd;aul jkafka l%uf,aLKfha by, isg
my,g" wLKav wdldrhlskqhs' tfukau" fuf;la wm oqgq jevigyka j, moku jQfha functions h'
fusjd procedural programming ys ,CIKhs' oeka wm iQodkus jkafka fuu udkfhka Tnsng .uka lr
kj oDYa:SfldaKhlska mrs.Kl jevigyka ,sjSu foi ne,Sugh' fuu kj oelau fyj;a kj
l%uf,aLK jsOsh Object Oriented Programming kuska ye`oskafjs'
mrs.Kl jevigykla hkq l%uf,aLlfhl= jsiska lsishus .eg,qjlg bosrsm;a l, jsisoqu f,i
ye`oskajsh yel' fuu .eg,q fndfydajsg ienE f,dalfha jia;= uQ,sl fldgf.k we;sfjs' WodyrKhla
jYfhka lsisfjl= fudagrA Odjkh lsrSu mrs.Klh u.ska b.ekajSu i`oyd virtual reality >Kfha
mrs.Kl uDoqldx.hla ksrAudKh lsrSug is;hs kus" Tyqg fudgrA r:h keue;s jia;=jo" g%emsla ix{d
keue;s jia;=jo" moslhd hk jia;=jo" fjk;a fudgrA: Odjkh lsrSu yd iusnkaO fkdfhla foao ;u
jevigyk ;=, ksujSug isoqjkq we;s' tysoS isoqjkafka iEosh hq;= mrs.Kl jevigykg wod, iEnE
f,dalfhys mj;sk jia;+ka tu mrs.Kl jevigyk ;=, ksrAudKh lsrSuhs' fuf,i ienEf,dalh ;=,
we;s jia;= mdol fldgf.k mrs.Kl jevigyka ksrAudKh lsrSu object oriented programming f,i
uQ,sl jYfhka ie,lsh yelsh'
Object oriented programming Ndjs; lrkafka [email protected]
Procedural programming ysoS Tng lsishus function tll we;s mrs.Kl fla;hka kej; mdjspsps
lsrSug isoqjk wjia:djla i,lkak' kuq;a th kej; mdjspsps lsrSfusoS tu function tflys
ls%hdldrS;ajh u`ola fjkia lsrSug Tng wjYH heh`o is;kak' fujsg Tn lrkafka [email protected] Tn
Function tl copy lr" th fjk;a ia:dkhl Paste lr" thg wjYH fjkialus isoqlr" thg w,q;a
kula fokq we;' fuu ls%hdj u.ska fowdldrhl mdvq isoqfjs' m,uqjekak kus fuu copy-paste lsrSfusoS
l%uf,aLKh jv jvd jsYd, jSuh' ;jo fuhska jevigyk ieu;eku program bugs fyj;a jeros
f,i l%uf,aLKh lr we;s ;eka we;sjSfus iusNdjs;dj by, hhs' kuq;a object oriented programming
Ndjs;d lsrSfuka fuu copy-paste wjq, ,syShhs' tysoS Tng Inheritance keue;s object oriented
l%ufjsoh fujeks wjia:djka j,oS Ndjs;d l, yelsh' Inheritance hkq" mj;sk mrs.Kl fla;hka
copy-paste fkdlr" uq,a fla;hkago ydkshla isoq fkdlr" wjYH fjkialu`o iys;j h,s tu fla;hka
mdjspspshg .ekSfus jsYsIaG l%fudamdhls'
;jo" object oriented programming u.ska mrs.Kl l%uf,aLlhla jvd meyeos,s jk w;r" miqj
jevigyk kjSlrKh lsrSuo jvd myiq fohla njg m;a fjs'
60
jia;=jla fyj;a object tlla hkq [email protected]
ienE f,dalfha jia;=jla .;ajsg thg ysus jsfYaI ,CIK we;' tfukau tu jia;=jg ysus
ls%hdldrS;ajhkao we;' ienE f,dalfha jia;=jlg WodyrKhla f,i .=jkahdkhla i,lkak'
.=jkahdkhlg kshus; yevhla we;' thg jrAKhla we;' th u.ska .=jka hdkhlg jsfYaI jQ yvla
ksmojhs' fusjd .=jkahdkh keue;s jia;=fjys ,CIKh' wm fuu ,CIK properties hkqfjka y|qkajuq'
;jo" .=jkahdkhlg ksYaps; ls%hdldrS;ajhkao we;' thg ;u fjs.h md,kh l,yelsh' ;udf.a isg
fmdf,djg we;s oqr (altitude) md,kh l,yelsh' ;u .ukau. fjkia l,yelsh' fuu ls%hdldrS;ajhka
wm .=jkahdkfha functions f,i y|qkajuq' C++ u.ska jia;=jla ksrAudKh lsrSu hkq fuu properties
^,CIK& iy functions ^ls%hdldrS;ajhka& tla fldg tla tallhla ksujSuh' C++ ;=, jia;=jla ksrAudKh
lsrSug wm class keue;s keyword tl mdjspsps lruq'
oeka wm by; i`oyka .=jkahdkh keue;s jia;=j C++ u.ska ksujkafka flfiaoehs n,uq:
===========================
class Plane
{
};
===========================
by; oelajQfha .=jkahdkh keue;s object tl C++ u.ska ksmojSu wdrusN l, yels wdldrhh' tysoS
m,uqj class hk keyword tl fhdod we;' bkamiq ;kk jia;=fjys ku(plane) i`oyka lr we;'
bkamiqj function tlloS fuka i., jryka fhdod wjidkfhaoS colon tlla ;nd we;' fuf,i object
tlla ;ekSu Tnf.au o;a; jrA.hla ksrAudKh lsrSu f,io ye`oskafjs' tkus oeka Plane hkak int fyda
char fuka o;a; jrA.hls' th “user define data type” fyj;a “mdjspsps lrkakd jsiska ksujQ o;a;
jrA.hla f,i y`oqkajhs'
wm Plane hk object tl iEoqjo" Tng fmfkk mrsos ta;=, lsisjla wvx.= fkdfjs' ienE jia;=jlg kus
thg wh;a ,CIK we;' .=jkahdkhlg we;s property tlla jkafka tys fjs.hhs' ;jo" lsishus
fudfyd;loS th .uka lrk osYdjo tys ,CIKhla jkq we;s' thg fmdf,dfjys isg we;s oqr fyj;a
altitude tl ;j;a property tlls' fuu properties wm Plane ;=,g oeka wka;rA.; lrkafka fufiahs:
===========================
class Plane
{
private:
int Speed;
int Direction;
int Altitude;
};
===========================
61
oeka Plane i;= ls%hdldrS;ajhka thg wka;rA.; lruq' tajdkus" tys fjs.h md,kh lsrSfus yelshdj"
th .uka lrk osYdj md,kh lsrSfus yelshdj iy tys altitude tl md,kh lsrSfus yelshdj hk
ls%hdldrS;ajhkah:
==========================================
class Plane
{
private:
int Speed;
int Direction;
int Altitude;
public:
void ChangeSpeed(int spd);
void ChangeDirection(int dir);
void ChangeAltitude(int alt);
};
==========================================
oeka wm Plane object tl define lr wjidkhs' isoqjQfha [email protected]:
Procedural programming ysoS wm isoqlf,a lsishus fohla isoqlsrSu i`oyd fjk fjk u variables yd
function define lsrSuhs' kuq;a fuysoS lsishus jia;=jlg wh;a functions yd variables, tu jia;=j ;=,
wka;rA.; lr we;' tkus" wm Plane kuska class tlla fyj;a object tlla idod" thg wh;a o;a;
ta;=,g wvx.= lr we;'
;jo Tn olsk w,q;a fohla jkafka fuu private yd public keue;s keywords hq.,hs' fuu keywords
ye`oskajSug fmdoqfjs access control keywords hk ku Ndjs;fjs'
private u.ska isoqjkafka
jevigyfkys fjk ;ekl isg RDPQj" fuu Plane object tl;=, we;s variables Ndjs;d lsrSug fyda
fjkia lsrSug we;s yelshdj ke;s lsrSuhs' tajd fuysoS Plane object tflys “fm!oa.,sl” foa njg
m;afjs' public keyword tlska mjikafka tu keyword tl hgf;a we;s ish,a," ish,a,g jSjD; njhs'
oeka" fuf,i lsrSfus we;s wrA:h [email protected] Plane object tlg msg;ska we;s lsisjlg" object tl;=, we;s
variables ysgs wvsfha ;udg wjYH mrsos fjkia lsrSug bvfkdoSfuka tu object tflys iqrCIs; nj
;yjqrefjs' kuq;a tu variables fjkia lsrSug kS;Hdkql=, l%uhlao fuysoS Plane u.ska imhd we;'
tkus ta;=, we;s functions j, ls%hdldrS;ajhhs' ChangeSpeed() u.ska tys Speed variable tl
fjkia lrkq we;' tfukau ChangeDirection() u.ska tys Direction variable tlo
ChangeAltitude() u.ska tys Altitude variable tlo fjkia lrkq we;' fuu functions
fld;ekl isg jqj mdjspsps l,yelsh' ukaoh;a tajd declare lr we;af;a public keyword tl hgf;a
ksidh' fuh yrshgu .=jkahdkhl wNHka;r ls%hdldrS;ajhka tys kshuqjdg Rcqj md,kh lsrSug
fkdyels njg iudkh' Tyqg ysgswvsfha .=jkahdkhg hdyels Wmrsu fjs. iSudj fjkia l, fkdyelsh'
th isoql, yelafla .=jkahdkh ksmojQ iud.fuys ldrAusl Ys,amSka ygh' kshuqjdg l,yelafla
.=jkahdkfha cockpit tflys ys`of.k" Tyqg imhd we;s ,Sjr yd fnd;a;us mdjspsps lsrSfuka th md,kh
lsrSu muKh' fuysoS" .=jkahdkh ksmojQ Ys,amSkag wkqrEm jkafka Plane class tl ;ekQ Tnh' .=jka
kshuqjdf.a ldrAhhg wkqrEm jkafka Tn Plane ys functions Ndjs;hg .kakd wjia:djh'
Access control keywords ms,sn`oj ;j oqrg;a u`ola bosrsfhaoS i,ld n,uq'
oeka wm by; oelajQ functions, define lruq:
===============================================
62
void Plane::ChangeSpeed(int spd)
{
Speed = spd;
cout<< Speed <<endl;
}
void Plane::ChangeDirection(int dir)
{
Direction = dir;
cout<< Direction <<endl;
}
void Plane::ChangeAltitude(int alt)
{
Altitude = alt;
cout<< Altitude <<endl;
}
===============================================
Procedural programming ysoS function tlla define lrk wdldrhg jvd fuu function definitions j,
jsYd, fjkila oelsh yels fkdfjs' fjkilg we;af;a function name tlg uq,ska tu function tl
wh;a object tflys ku i`oyka jS ;snSu muKs' Object tflys kuo function name tlo fjkajkafka “
:: ” i,l=Ksks' idudkHfhka Tn Visual C++ ;=, object name tl type lr bosrsfhka :: i,l=K type
l,jsg tu object tlg wod, variables yd functions j, kus Visual C++ u.ska fmkakqus lrkq we;s'
Tn l,hq;af;a osiajk l=vd menu tlska Tng wjYH item tl u; click lsrSuh'
by; function definitions u.ska isoqjk foa Tng meyeos,s we;s' tajd u.ska Plane object tflys variables
j, w.hhka" pass lrkq ,nk arguments j, w.hka njg m;a flfrA' bkamiq tu fjkiajQ w.hka
print fjs'
oeka fuysoS isoqjQ foa h,s flgsfhka lsjfyd;a: Plane object tl u.ska tys wvx.= wNHka;r o;a; fyj;a
variables RPqj fjkia lsrSfus whs;sh Ndysr f,dalfhka bj;a lr we;s w;r" tajd ksis mrsos"
kS;Hdkql=,j muKla fjkia lsrSug functions define lr we;' C++ object tllg we;s fuu o;a;
msg; f,dalfhka ie.jSfus yelshdj Data Abstraction f,i ye`oskafjs' Object tlla i;= o;a;" class
keue;s tla tallhg f.dkq lsrSu Encapsulation f,i ye`oskafjs'
Encapsulation ys m%Odk m%fhdack 2la oelafjs:
^1& Modulraity - Object tllg iajdOSk tallhla f,i ls%hdlsrSug we;s yelshdj' object oriented
mrs.Kl jevigykl object tlla tu jevigyfkysu wfkla objects fj;ska fjkajQ tallhla f,i
mej;Sug yelsjSu ksid" wfkla objects u; r`od isgSug thg isoq fkdfjs - .=jkahdkhla
.=jkaf;dgqm,l we;s wfkla jia;= j,ska fjkajQ fohls'
(2) Information hiding – o;a; ie.jSfus yelshdj' Object tllg private fyj;a fm#oa.,sl o;a;
;nd.ekSug yels ksid tu o;a; wfkla objects j, iusnkaO jSulska f;drj" iajdOSkj fjkia lr.;
yel' - .=jkahdkhlg hdyels Wmrsu fjs.h tys ksYamdok iud.u jsiska fjkia lsrSu" .=jka u.shd f.a
^.=jka u.shd keu;s object tflys& yjq,a jSulska f;drj isoql, yelsh' tysoS hdkhg hdyels Wmrsu
fjs.h hdkfha fm#oa.,sl o;a;hla fjs'
oeka wm by; lshQ private o;a; j, yeisrSu ms,sn`oj WodyrKhla mrSCId lruq' fus i`oyd wm
.=jkahdkhl .=jka kshuqjd oelajSug Pilot hk object tl ;kd tu object tl u.ska Plane object tl
iusnkaOj l,yels foa iy l, fkdyels foa ms,sn`oj jsuiuq'
63
my; oelafjkafka Pilot object tl ksujd we;s wdldrhhs:
===============================================
class Pilot
{
public:
void ChangePlaneSpeed(Plane p, int spd);
void ChangePlaneDirection(Plane p,int dir);
void ChangePlaneAltitude(Plane p, int alt);
};
===============================================
uS,.g Pilot object tflys functions define lruq:
=================================================
void Pilot::ChangePlaneSpeed(Plane p, int spd)
{
p.ChangeSpeed(spd);
}
void Pilot::ChangePlaneDirection(Plane p,
int dir)
{
p.ChangeDirection(dir);
}
void Pilot::ChangePlaneAltitude(Plane p,
int alt)
{
p.ChangeAltitude(alt);
}
=================================================
by; iEu function tllau ;u argument tl f,i Plane object tlla ,nd.kakd nj Tng fmfkkq
we;s' fus wkqj by;skao lS mrsos object tlla hkq we;af;kau l%uf,aLlhd jsiska define lrk ,o data
type tlla fjs' Functions ;=,oS" tu pass lrkq ,enQ object tl mdjspsps lr we;s wdldrh n,kak' tysoS
p hkq Plane object tflys instance tlla f,i ye`oskafjs' th" int i f,i variable tlla declare
lsrSfusoS" i hkq int ys instance tlla hehs lSu yd iudkh' Plane ;=, we;s functions j,g call lr
we;af;a p iy function name tl w;r ;s;la(period) ;enSfuks' Visual C++ ;=," Tn object instance
tl type lr" ;s;la type l, jsg object tl ;=, we;s functions iy properties VC++ u.ska olajkq
we;'
fuf,i Pilot object tl u.ska Plane object tl;=, we;s o;a;" t;=, we;s functions udrA.fhka
fjkialsrSug yels nj Tng by; fmfkkakg we;' fus whqrska mrs.Kl jevigykl we;s tla object
tll isg fjk;a object tll functions j,g call lsrSu tu objects w;r massages send lsrSu fyj;a
mKsjqv yqjudre lr.ekSu f,i ye`oskafjs' Object oriented program tlla hkq we;af;kau objects
/ila w;r we;s mKsjqv yqjudre lr.ekSuls'
fusjk jsg objects iusnkaOj lghq;= lsrSfusoS Ndjs;d jk i,l=Kq 2 la Tn oqgqjd' tajdkus" “ :: ” iy “
. ” hk hq.,hs' “ :: ” mdjspsps jkafka by; oelajqKq mrsos" declare lrk ,o object tlla ;=, we;s
64
functions define lsrSfusoSh' “ . ” mdjspsps jkafka object instance tlla u.ska tu object tl ;=, we;s
function tllg call lsrSfusoS fyda object tl ;=, we;s variable tlla ,nd.ekSfusoSh'
oeka Tng main() ;=, isg Plane ys functions j,g" Pilot object tl u.ska my; mrsos call l, yelsh'
tysoS Tn l,hq;af;a m,uqj Pilot iy Plane object foflys instance folla iEoSu jk w;r bkamiqj “ .
” u.ska Pilot ys function j,g we;=,a jsh hq;=h:
=================================
void main()
{
Pilot pt;
Plane pl;
pt.ChangePlaneSpeed(pl, 500);
}
my; oelafjkafka by; Tn oqgq fla;hka ish,a, f.dkq lr ieoQ iusmQrAK jevigykhs' fuu
jevigyk VC++ ;=, type lr th ls%hdlrk wdldrh n,kak:
================================================
#include<iostream.h>
class Plane
{
private:
int Speed;
int Direction;
int Altitude;
public:
void ChangeSpeed(int spd);
void ChangeDirection(int dir);
void ChangeAltitude(int alt);
};
void Plane::ChangeSpeed(int spd)
{
Speed = spd;
cout<< Speed <<endl;
}
void Plane::ChangeDirection(int dir)
{
Direction = dir;
cout<< Direction <<endl;
}
void Plane::ChangeAltitude(int alt)
{
Altitude = alt;
cout<< Altitude <<endl;
}
class Pilot
{
65
public:
void ChangePlaneSpeed(Plane p, int spd);
void ChangePlaneDirection(Plane p, int dir);
void ChangePlaneAltitude(Plane p, int alt);
};
void Pilot::ChangePlaneSpeed(Plane p, int spd)
{
p.ChangeSpeed(spd);
}
void Pilot::ChangePlaneDirection(Plane p, int dir)
{
p.ChangeDirection(dir);
}
void Pilot::ChangePlaneAltitude(Plane p, int alt)
{
p.ChangeAltitude(alt);
}
class FighterPlane:Plane
{
};
void main(){
Pilot pt;
Plane pl;
pt.ChangePlaneSpeed(pl, 500);
}
==========================================
Inheritance
by; o i`oykajQ mrsos" Tng lsishus mrs.Kl fla;hka /ila fjkialus iys;j kej; kej; Ndjs;d
lsrSug wjYH kus object oriented programming ys wka;rA.; inheritance kus l%ufjsoh fhdod.; yel'
wm fuf;la oqgq .=jkahdkd WodyrKhu fhdodf.k fuh jgyd.ekSug W;aidy lruq'
WodyrKhla f,i Tng idudkH .=jkahdkhlg wu;rj hqO .=jkahdkhla o ;ekSug wjYHhehs
is;kak' idudkH .=jkahdkhlg fukau hqO .=jkahdkhlg o fmdoqjQ ,CIK fukau tlsfklska
fjkia jQ ,CIKo we;' kuq;a hqO .=jkahdkhlg we;af;ao idudkH .=jkahdkhl mokuu fjs'
fusksid Tng idudkH .=jkahdkh mokus fldgf.k hqO .=jkahdkhla ksujsh yelsh' Tn l,hq;af;a
idudkH .=jkahdkhla f.k thg w,q;ska wka;rA.; l,hq;= ,CIK tl;=lr hqO .=jkahdkh ;ekSuhs'
fuf,i tla object tll ,CIK wdfjsKs lruska fjk;a object tlla ksrAudKh lsrSu Inheritance
f,i ye`oskafjs' hqO hdkdj ;ekSfusoS Tng idudkH hdkfha fkdue;s usihs, jsOsfus yelshdj thg
wka;rA.; lsrSug wjYH jkq we;' fuu fjki fuu hdkd fol w;r we;s m%Odk fjki f,i
i,luska oeka wm hqO .=jkahdkh ;kkafka flfiaoehs n,uq:
=====================================
class FighterPlane: public Plane
{
};
=====================================
66
by; oelafjkafka hqO .=jkahdkh fyj;a FighterPlane object tl" Plane object tlska inherit
fyj;a m%fjsKs lsrSfus 1jk wOshrhs' Code tflys oelafjk mrsos inherit lsrSfusoS Ndjs; jk jHdlrK
ms,sfj, fufiahs: fuysoS wm declare lrkafka kj object tlla ksid m,uqj fhosh hq;af;a class
keyword tlhs' fojkqj fhosh hq;af;a kj object tflys kuhs' by; fuh FighterPlane fjs'
f;jkqj" “ : ” i,l=K fhosh hq;=h' fuu i,l=Kska oelafjkafka" tu i,l=Kg fmr we;s object
tflys(FighterPlane) ,CIK i,l=Kg miq we;s object tlska (Plane)wdfjsKsh(inherit)
jshhq;= njhs' y;rjkqj inherit l,hq;= merKs object tflys ku" ta i`oyd Ndjs; l, hq;= access type
tl iu. fhosh hq;=h' by;oS wdfjsKs l,hq;= object tl jkafka Plane h' Plane g bosrsfhka tys
access type tl f,i public fhdokafka" plane ys ;sfnk ish,q o;a;hka mdpspsps lsrSfus whs;sh
FighterPlane class tlg ;snsh hq;= nj oelajSugh' Tn tysoS private keyword tl mdjdpsps l,d kus
FighterPlane yg Plane ys we;s functions mdjspsps lsrSug fkdyelsjShhs'
fuysoS uq,a class tl “Base class” f,io" uq,a class tlska wdfjsKs lr ieoQ kj class tl “Derived
class” f,io object oriented programming ysoS y|qkajkq ,efns'
oeka Tn object oriented programming ys Ndjs; jk ;j;a i,l=Kla jk “ : ” oqgqjd' fuh y|qkajkafka
“is based on sign” jYfhks' tawkqj Tn fus jkjsg u;lfha ;nd.;hq;= i,l=Kq .kk 3 ls:
(1) “ :: ” (2) “ . ” (3) “ : ” kej;;a fuu i,l=Kq Ndjs;d jk wjia:djka flgsfhka isysl, fyd;a"
(1) Ndjs;djkafka object tll we;s functions define lrk jsghs' (2) Ndjs;djkafka object tlla ;=,
we;s function j,g call lrk jsg fyda variables ,nd .kakd jsghs' (3) Ndjs;djkafka fudfyd;lg fmr
oqgq mrsos merKs object tll wdfjsKsfhka kj object tlla ;kk jsgh'
fus wkqj" FighterPlane object tlg Plane object tlg ysus ish,qu o;a; ysush' hqO .=jkahdkhg
idudkH hdkhg l, yels fjs.h fjkia lsrSu" osYdj fjkia lsrSu" altitude tl fjkia lsrSu wdoS
ish,a, l,yelsh' ta wkqj Tng main() ys isg" Plane ;=, we;s functions j,g" tajd FighterPlane ;=,
we;a;d fia i,ld call l, yels jkq we;' tkus fuysoS wm Plane ;=, we;s functions u" FighterPlane
;=,oS kej; Ndjs;hg .ksuq' ta wkqj Inheritance Ndjs;d lsrSfuka wmg mrs.Kl fla;hka copy-paste
fkdlr fufia h,s Ndjs;hg .; yelsh' kuq;a fuh idudkH function tllg kej; kej; call
lsrSula fkdfjs' wm fuysoS Plane ;=, we;s functions kej; m%fhdackhg .;afka tajd FighterPlane g
wh;a w,q;au functions f,ihs' my; oelafjkafka fuhhs:
=====================================
void main(){
FighterPlane fp;
Pilot pl;
pl.ChangePlaneSpeed(fp, 200);
}
=====================================
by;oS Tn olsk fohla jkafka" ChangePlaneSpeed u.ska fuysoS ,nd .kafka Plane object tlla
fkdj FighterPlane object tlla njhs' kuq;a ChangePlaneSpeed u.ska b,a,kafka Plane object tlla
[email protected] Object oriented programming ys ;j;a jdishla kus fuhhs' tkus" Tng Base class object
tlla wjYH ;eklg" Derived class object tlla jqjo pass l, yelsh' tfiakus oeka
ChangePlaneSpeed ;=,oS call jkafka FighterPlane ;=, we;s ChangeSpeed function tllgh'
fuh is;a.kakd iq,q ,CIKhls' meyeos,sju" FighterPlane ;=, oekg lsisu function tlla w;a;rA.;j
fkdue;s nj Tng tys declaration tl foi ne,Sfuka meyeos,s fjs' fuysoS" main() ;=, isg
67
FighterPlane instance tlla idod" Pilot object tll udrA.fhka FighterPlane ys ChangeSpeed fj;
call lsrSu isoqfjs' kuq;a FighterPlane ;=, tjeks function tlla olakg ke;' tfiakus th Plane ;=,
we;s ChangeSpeed jshhq;[email protected] we;af;kau oeka th Plane ;=, we;s ChangeSpeed fkdfjs' th
FighterPlane u.ska Plane ;=, isg wdfjsKs lr.;a" oeka ;ud i;= lrf.k isgsk fjku function tlls'
taksid fuysoS ChangeSpeed u.ska isoqjkafka Plane ;=, we;s Speed ys w.h fjkia lsrSu fkdfjs'
isoqjkafka FighterPlane ;=, we;s Speed ys w.h fjkia lsrSuhs' Object oriented programming Ndjs;fha
we;s ldrAhCIu njg tla idOlhla kus fuhhs' fmr lshQ mrsosu fuf,i Inheritance u.ska Tng
mj;sk o;a;hka copy-paste lsrSulska f;drj kej; kej; mdjspsps lsrSfus yelshdj ,efns'
my; oelafjkafka by; Tn fuf;la oqgq fla;hka f.dkq fldg ;kkq ,enQ iusmQrAK jevigykhs' th
VC++ ;=, type lr ls%hd;aul lr isoqjk foa kej; meyeos,s lr.kak:
================================================
#include<iostream.h>
class Plane
{
private:
int Speed;
int Direction;
int Altitude;
public:
void ChangeSpeed(int spd);
void ChangeDirection(int dir);
void ChangeAltitude(int alt);
};
void Plane::ChangeSpeed(int spd)
{
Speed = spd;
cout<< Speed <<endl;
}
void Plane::ChangeDirection(int dir)
{
Direction = dir;
cout<< Direction <<endl;
}
void Plane::ChangeAltitude(int alt)
{
Altitude = alt;
cout<< Altitude <<endl;
}
class Pilot
{
public:
void ChangePlaneSpeed(Plane p, int spd);
void ChangePlaneDirection(Plane p, int dir);
void ChangePlaneAltitude(Plane p, int alt);
};
void Pilot::ChangePlaneSpeed(Plane p, int spd)
68
{
p.ChangeSpeed(spd);
}
void Pilot::ChangePlaneDirection(Plane p, int dir)
{
p.ChangeDirection(dir);
}
void Pilot::ChangePlaneAltitude(Plane p, int alt)
{
p.ChangeAltitude(alt);
}
class FighterPlane:public Plane
{
};
void main(){
FighterPlane fp;
Pilot pl;
pl.ChangePlaneSpeed(fp, 200);
}
==========================================
kuq;a hqO hdkhg idudkH .=jka hdkhg l,yels foag wu;rj usihs, jsOsfus wu;r yelshdj o we;'
fuu yelshdj FighterPlane object tl fj; ,ndfok wdldrh oeka n,uq:
=====================================
class FighterPlane: public Plane
{
public:
void FireMissile();
};
=====================================
Tn l,hq;af;a FighterPlane ;=,g usihs, jsoSsfus yelshdj ,ndfok kj function tlla by; oelafjk
mrsos we;=,a lsrSuh' my; FireMissile function tl define lr we;:
======================================
void FighterPlane::FireMissile(){
cout<< "Missile fired!" << endl;
}
======================================
tfukau" my; mrsos Pilot ;=,o kj Function tlla define lsrSu iqoqiqh:
=================================================
69
class Pilot
{
public:
void ChangePlaneSpeed(Plane p, int spd);
void ChangePlaneDirection(Plane p, int dir);
void ChangePlaneAltitude(Plane p, int alt);
void FireMissile();
};
void Pilot::FireMissile()
{
FighterPlane fp;
fp.FireMissile();
}
======================================
oeka Tng main() ys isgsuska" usihs,hla uqodyrsk f,i hqO hdkhg my; mrsos lsjyelsh:
======================================
void main(){
Pilot pl;
pl.FireMissile();
}
======================================
oeka uq,q jevigyk my; mrsosh:
======================================
#include<iostream.h>
class Plane
{
private:
int Speed;
int Direction;
int Altitude;
public:
void ChangeSpeed(int spd);
void ChangeDirection(int dir);
void ChangeAltitude(int alt);
};
void Plane::ChangeSpeed(int spd)
{
70
Speed = spd;
cout<< Speed <<endl;
}
void Plane::ChangeDirection(int dir)
{
Direction = dir;
cout<< Direction <<endl;
}
void Plane::ChangeAltitude(int alt)
{
Altitude = alt;
cout<< Altitude <<endl;
}
class Pilot
{
public:
void ChangePlaneSpeed(Plane p, int spd);
void ChangePlaneDirection(Plane p, int dir);
void ChangePlaneAltitude(Plane p, int alt);
void FireMissile();
};
void Pilot::ChangePlaneSpeed(Plane p, int spd)
{
p.ChangeSpeed(spd);
}
void Pilot::ChangePlaneDirection(Plane p,
int dir)
{
p.ChangeDirection(dir);
}
void Pilot::ChangePlaneAltitude(Plane p, int alt)
{
p.ChangeAltitude(alt);
}
class FighterPlane:public Plane
{
public:
void FireMissile();
};
void FighterPlane::FireMissile(){
cout<< "Missile fired!" << endl;
}
void Pilot::FireMissile()
{
FighterPlane fp;
fp.FireMissile();
71
}
void main(){
Pilot pl;
pl.FireMissile();
}
=============================
Access control - public, private, protected keywords
(Object tlla ;=, we;s o;a; fj; we;=,ajSu md,kh lsrSu)
lsishus object tlla ;=, we;af;a variables yd function nj Tn okshs' Object tlla ;=, we;s fuu o;a;
j,g" object tlska msg; isg ,.djSfus yelshdj md,kh lsrSu C++ object tll we;s uQ,sl ,CIKhls'
Tn by; ;ekloS oqgq information hiding keue;s ixl,amh h:drA:hla jkafka fuu yelshdj ksidh' ta
yelshdj kus" object tllg msg;ska we;s hulg" mdjspsps lsrSug yelshdj we;af;a object tl ;=, we;s
ljr o;aohkao iy mdjspsps lsrSfus yelshdj ke;af;a ljr o;a;hkao hkqfjka ;SrKh lsrSuhs' fus
i`oydh public, private yd protected hk access control keywords Ndjs;d jkafka' public yd private hk
hq.,h ms,sn`oj fus jk jsg Tn hus ;rula lshjd we;s kuq;a tys ls%hdldrS;ajh w;aoel ke;' my;
oelafjkafka Plane ;=, we;s private o;a;hla jk altitude ys w.h main() ys isg Rcqj fjkia lsrSug
.;a W;aidyhls:
================================================
void main(){
Plane p;
p.Altitude = 1300;
}
================================================
Tn fuu fjkialu jevigyfkys main() function tlg isoqlr jevigyk compile lsrSug W;aidy
l,fyd;a Tng error tlla ,efnkq we;s w;r jevigyk compile fkdjkq we;' tu error tlska
lshfjk foh kus main() ys isg ^fyj;a Plane object tlg msg;ska isg& Plane ;=, we;s private
variable tlla jk Altitude mdjspsps lsrSug fkdyels njhs' wm fuysoS isoq lsrSug W;aidy.;a foh"
yrshgu lsisfjl= .=jkahdkhl we;s md,l h;=re Ndjs;d fkdlr tys Wkak;dxYh md,kh lsrSug
is;Su jeksh' kuq;a ksielju th ls%hdfjs fhojSug Tyqg fkdyels fjhs' .=jkahdkfha Wkak;dxYh
md,kh lsrSug yelafla .=jkahdkfha we;s ksis h;=re fyda ,Sjr mdjspsps lsrSfuka muKla jsh hq;=h'
th wkqrEmS jkafka Plane ;=, we;s ChangeAlitutude function tlg lrk call lsrSulghs' by;
jevigyfkaoS fuh isoqlrkafka Pilot object tl u.sks' Tyq th lrkq ,nkafka ;ud ;=, we;s public
function tl jk ChangePlaneAltitude u.sks' fuu l%uh iy main() ;=, isg fl,skau Plane ys
functions i`oyd call lsrSu yefrkakg Altitude fyda Plane ys fjk;a lsisu private variable tlla fjkia
lsrSfus fjk;a lsisu l%uhla by; jevigyk ;=, fkdue;' Tng Plane ys we;s o;a; fjkia lsrSu
i`oyd wjir oSug wjYH jkafka w;sYhskau Pilot object tlg muKlau kus Tn l, hq;= jkafka"
Plane ys we;s functions bj;a lr" Pilot ys functions j, isg Rcqj Plane ys o;a; fjkia l, yels
wdldrhg jevigyk ieliSuh' tjsg main ys isg Plane ys o;a;hka fjkia lsrSug fkdyels jS hhs' fus
wkqj object oriented programming u.ska object tlla ;=, we;s o;a; j,g iemfhk wdrCIdj Tng
jegfykq we;'
72
Object tlla ;=, “private:” hkak hgf;a declare lr we;s o;a;hkag object tlg msgska isg we;=,ajSug
fkdyels njo “public:” hkak hgf;a we;s o;a; j,g we;=,a jSug ieugu yelsnjo by; Tn oqgqjd'
3ka jeks access control keyword tl jk protected o fndfyda oqrg private keyword tlg iudkhs'
Object tlla ;=, “protected:” hkak hgf;a we;s o;a; j,go msg;ska isg we;=,ajSfus wjia:djla ke;'
kuq;a protected keyword tflys we;s fjki kus" object tll protected hgf;a define lr we;s o;a;
j,g we;=,q jSfus wjirh" tu object tlska inherit lr ksmojkq ,enQ derived objects j,g muKla
,ndoSug thg yelsjSuhs' fuh meyeos,s lr .ekSu i`oyd my; oelafjk mrsos" Plane class tlg
protected: keyword tl hgf;a LandingGears keue;s bool variable tlla declare lrkak'
LandingGears ys w.h true lsrSfuka .=jka hdkfha f.dvneiSu i`oyd jk .shr moaO;sh ls%hd;aul lr
we;s nj olajk w;r th false lsrSfuka hdkh f.dvneiSug iQodkus ke;s nj oelafjkq we;:
===========================================
class Plane {
private:
int Speed;
int Direction;
int Altitude;
public:
void ChangeSpeed(int spd);
void ChangeDirection(int dir);
void ChangeAltitude(int alt);
protected:
bool LandingGears;
};
===========================================
oeka my; mrsos Tn fuu protected variable tflys w.h main() ;=, isg true lsrSug W;aidy .;fyd;a
thska error tlla ksmofjkq we;' fuksid protected o;a;o private fukau object tlg msg; isg Ndjs;
l, fkdyels nj fmfka'
===========================================
void main()
{
Plane p;
p.LandingGears = true;
}
===========================================
kuq;a Tn Plane ys derived object tl jk FighterPlane ;=, isg th lsrSug W;aidy lrkak' th
idrA:l jkq we;' my; oelafjkafka FighterPlane ;=, isg LandingGears ys w.h true lsrSug .;a
W;aidyhhs' fus i`oyd FighterPlane ;=, SetLandingGears keue;s kj function tl declare lr we;:
===========================================
73
class FighterPlane: public Plane
{
public:
void FireMissile();
void SetLandingGears(bool st);
};
===========================================
SetLandingGears() u.ska LandingGears variable tl kshus; true fyda false w.hg m;aflfrA:
==============================================
void FighterPlane::SetLandingGears(bool st){
LandingGears=st;
}
==============================================
Constructors
Plane object tl ;=, lsishus fudfyd;l .=jkahdkfha fjs.h ,nd.ekSug GetSpeed keue;s function
tl we;ehs is;kak' my;" GetSpeed function tl Plane class tl ;=,g we;=,a lr we;:
===========================================
class Plane
{
private:
int Speed;
int Direction;
int Altitude;
public:
void ChangeSpeed(int spd);
void ChangeDirection(int dir);
void ChangeAltitude(int alt);
int GetSpeed();
};
===========================================
GetSpeed function tl fufia define lruq:
===========================================
int Plane::GetSpeed(){
74
return Speed;
}
===========================================
GetSpeed() u.ska Speed ys oeka mj;sk w.h return lrkq we;'
oeka main() ;=, isg wmg .=jkahdkfha fjs.h fuf,i ,nd.; yel'
===========================================
void main(){
Plane p;
int sp = p.GetSpeed();
cout << sp << endl;
}
===========================================
kuq;a fuysoS tla .eg,qjla mekk.shs' Tn jevigyk ls%hd;aul lsrSfuka miq m,uqfjka isoqlrkafka
fuf,i fjs.h fijSu hehs is;kak' tjsg Speed ys w.h f,i ,efnkafka [email protected] nska`[email protected] ke;'
tjsg isoqjkafka Speed variable tl lsishus w.hlg define lsrSug fmr thska w.hla ,nd .ekSug
W;aidy lsrSuhs' Tn lsishus variable tlla define lr ke;sjsg tys lsisu wrA:hla we;s ixLHdjla
wka;rA.; fkdfjs' fusksid Tn by; fuka variable tlla define lsrSug fmr tys w.h ,nd.;fyd;a
th wrA:hla ke;s ls%hdjla jkq we;s w;r th jeros ls%hdjlao fjs' fusksid mrs.Kl jevigykla
wdrusNfhaoSu tys we;s variables ish,a, iajhxls%hj define lsrSug udrA.hla ;snsh hq;=h' fuu ls%hdj"
variable initialisation f,i ye`oskafjs' Class variables, initialise lsrSfus tu l%uh kus constructor
functions Ndjs;d lsrSuh'
we;af;kau constructor functions Tn oekgu;a tfia lrkjdoehs fkdoek mdjspspshg f.k we;' Tn
C++ mrs.Kl jevigykla ;=, class tlla iEoq jsg Tng fkdfmfkk f,i tys constructor function
tl define fjs' fuh isoqjkafka Tn class tflys ku type lrk wjia:dfjsoSh' tu class tflys
instance tlla iEoQ jsg isoqjk m,uq jk foh kus fuu fkdfmfkk constructor function tlg
fkdfmfkk mrsosu call jSuh' tkus constructor function tlg call jSu isoqjkafka iajhxls%hj" class
instance tl ksmojk wjia:dfjsoShs' ;jo" constructor tll ku jkafka tys class tflys kuuh' ta
wkqj Plane class tl ;=, we;s constructor function tflys ku jkafkao Plane h' Constructor function
tllg return type tlla fkdue;' tfukau uq,a (default) wjia:dfjsoS thg lsisu argument tlla o
fkdue;' fus whqrska" Plane class tflys constructor tl declare jkq we;af;a my; mrsosh:
Plane();
kuq;a Constructor tl jevigyk ;=, ryfiau mj;sk w;r thg ryfiau call fjhs' tfiakus by;
i`oyka jQ mrsos wm fuu constructor tl wmf.a class tl ;=, we;s variables initialise lsrSu msKsi
fhdod.kafka [email protected] b;d myiqhs' wm l,hq;af;a constructor tl wmg wjYH wdldrfhka fjkia lr
th wm jsiskau define lsrSuhs' Constructor tlla Ndjs;d lr Plane class tflys we;s Speed, Direction,
Altitude hk variables, initialise lrk wdldrh my; jsia;rfjs'
m,uqfjkau l, hq;= jkafka Plane class tl ;=, wm jsiskau constructor tl declare lsrSuhs :
===========================================
class Plane {
75
private:
int Speed;
int Direction;
int Altitude;
public:
Plane(); // The constructor
void ChangeSpeed(int spd);
void ChangeDirection(int dir);
void ChangeAltitude(int alt);
int GetSpeed();
protected:
bool LandingGears;
};
===========================================
Plane ys public - access controller keyword tl hgf;a constructor tl declare lr we;s whqre Tng
fmfka' oeka l,hq;af;a constructor tl define lsrSuh:
===========================================
Plane::Plane(){
Speed = 0;
Direction = 0;
Altitude = 0;
}
==========================================
Constructor definition tl ;=, ish,q variable j,g uq,a w.hka ,nd oS tajd initialise lr we;s wdldrh
by; oelafjs' oeka Tn main() ys fyda jevigyfkys fjk;a ljr fyda w|qre uq,a,l isg Plane ys
instance tlla iEoQjo tu idok fudfydf;aoS m,uqj isoqjk foh kus iajhxls%hj fuu constructor
tlg call jS tys we;s variables ksis mrsos initialise jSuh' fus ksid Tn uq,skau lrk foh jkafka
GetSpeed() u.ska .=jkahdkfha fjs.h ,nd.ekSu jQjo Tng tys fjs.h f,i wrA:hla iys; w.hla
jk 0 ,efns' fjs.h nskaoqj jSfuka mejfikafka .=jkahdkh .uka fkdlrk nj Tng meyeos,sh' Tn
constructor tl mdjspsps fkdl,d kus Tng Speed f,i fndfydajsg ,efnkq we;af;a wrA: jsrys; RK
w.hls'
my; oelafjkafka constructor tl fhdod fjkia lrk ,o uq,q mrs.Kl jevigykhs' oeka
GetSpeed() u.ska uq,a fjs.h f,i 0" return lrkq we;'
================================================
#include<iostream.h>
class Plane
{
private:
int Speed;
int Direction;
int Altitude;
76
public:
Plane();
int GetSpeed();
void ChangeSpeed(int spd);
void ChangeDirection(int dir);
void ChangeAltitude(int alt);
protected:
bool LandingGears;
};
Plane::Plane(){
Speed = 0;
Direction = 0;
Altitude = 0;
}
void Plane::ChangeSpeed(int spd)
{
Speed = spd;
cout<< Speed <<endl;
}
void Plane::ChangeDirection(int dir)
{
Direction = dir;
cout<< Direction <<endl;
}
void Plane::ChangeAltitude(int alt)
{
Altitude = alt;
cout<< Altitude <<endl;
}
int Plane::GetSpeed(){
return Speed;
}
class Pilot
{
public:
void ChangePlaneSpeed(Plane p, int spd);
void ChangePlaneDirection(Plane p, int dir);
void ChangePlaneAltitude(Plane p, int alt);
};
void Pilot::ChangePlaneSpeed(Plane p, int spd)
{
p.ChangeSpeed(spd);
}
void Pilot::ChangePlaneDirection(Plane p, int dir)
{
p.ChangeDirection(dir);
}
void Pilot::ChangePlaneAltitude(Plane p, int alt)
{
77
p.ChangeAltitude(alt);
}
class FighterPlane:public Plane
{
public:
void FireMissile();
void SetLandingGears(bool st);
};
void FighterPlane::FireMissile(){
cout<< "Missile fired!" << endl;
}
void FighterPlane::SetLandingGears(bool st){
LandingGears=st;
}
void main(){
Plane p;
int sp = p.GetSpeed();
cout << sp << endl;
}
================================================
Header files
by; jevigyk Tn ksulrkq ,enqfha tlu C++ file tllskah' kuq;a fndfydajsg Object oriented
programe tll wvqu jYfhka files 2 la fyda wvx.=h' m,uqjekak kus C++ file tl jk w;r fojekak
y`oqkajkafka Header file tlla jYfhks' ^we;af;kau Tn Library tlla f,i jevigykg include
lrkafka fuu header files h& Header file tll extension jkafka “ .h ” hkakh' fuu file tl
jevigykl classes declare lsrSu i`oyd fhdod.; yel' by; oS kus Tn lf,a classes ish,a, tu
classes define jSu isoqjk C++ file tl ;=,u declare lsrSuhs' kuq;a idudkHfhka tu classes fjku
header file tlla ;=, declare lsrSu isoqfjs' fuh isoq lsrSug Tn Visual C++ ys File>new menu tlska
“C/C++ Header File” keue;s item tl u.ska kj header file tlla Tfns jevigykg we;=,a l,
hq;=h (?mh 8'1&
(?mh 8'1&
78
bkamiq fuu kj header file tl ;=, Tfns ish,q classes declare l, yel' tfukau Tfns jevigykg
#include l,hq;= ish,q libraries o fuu header file tl ;=,g #include l, yel' by; jevigyfkys
Plane class tl i`oyd Tn “Plane.h” hkqfjka header file tlla iEoqjfyd;a tys wvx.= l, hq;= oE my;
mrsos jkq we;'
===========================================
// Plane.h
#include<iostream.h>
class Plane
{
private:
int Speed;
int Direction;
int Altitude;
public:
Plane();
int GetSpeed();
void ChangeSpeed(int spd);
void ChangeDirection(int dir);
void ChangeAltitude(int alt);
protected:
bool LandingGears;
};
===========================================
oeka Tn l, hq;af;a my; mrsos fuu header file tl Tfns jevigyfkys C++ file tl ;=,g #include
lsrSuhs' Tn jsiska idok ,o header file tlla include lsrSfusoS isoql, hq;= fjki jkafka library tlla
include lsrSfusoS #include iu. fhfok ‘<’ iy ‘>’ hk i,l=Kq fjkqjg double quaots(“ ”) fhoSuhs:
================================================
#include “Plane.h” // including a header file
Plane::Plane(){
Speed = 0;
Direction = 0;
Altitude = 0;
}
void Plane::ChangeSpeed(int spd)
{
Speed = spd;
cout<< Speed <<endl;
}
..............
..............
..............
79
void main(){
Plane p;
int sp = p.GetSpeed();
cout << sp << endl;
}
================================================
by; oelafjk mrsosu" FighterPlane iy Pilot classes j, wvx.= declarations o header files ;=,g we;=,q
lr tajdo C++ file tlg include l, fyd;a" C++ file tl ;=, uS,S.g wvx.= l, hq;af;a function
definitions iy main() function tl muKs' Class declarations iy include lrk ,o libraries we;af;a
C++ file tlg include lrk ,o header files ;=,h' idudkHfhka jevigykl we;s iEu class tlla
i`oydu header file tlla ;ekSu iqoqiqh'
9. C++ Libraries
fuu fmdf;ys uq,a Nd.fhaoS Libraries .ek Tn hka;uska bf.k .;a;d' Libraries ;=, wvx.=
jkafka ,shd .nvd lr ;nk ,o C++ mrs.Kl fla;hka njo libraries wmf.a jevigyka ;=,
80
mdjspspshg .ekSfuka tu fla;hka wmf.a jevigyka ;=, Ndjs;d lsrSug yelshdj ,efnk njo Tn
oqgqjd'
Visual C++ ;=, wmg mdjspspshg .; yels libraries reila u we;' kuq;a fndfydajsg fusjd wm
mdjspspshg .kafka tajd wm m%fhdackhg .kakjdoehs fyda fkdoekh' tf,i isoqjkafka" wm lsishus
library tlla wmf.a jevigykg we;=,;a l, jsg tu library tlg iusnkaO l, fjk;a libraries o
bfnsu jevigyk ;=,g meusKsuhs' tfiakus wm tjeks libraries fndfydajsg mdjspsps lrkafka Rcqj
fkdfjs' kuq;a Rcqju wm ,shk jevigyka j,g we;=,a l, hq;= libraries we;'
C++ Library tll we;af;kau wvx.= jkafka ,shd .nvd lr ;nk ,o classes h' fus wkqj lsishus
C++ library tlla Ndjs;d lrk jsg wm isoqlrkafka tu library tl ;=, wvx.= Classes ;=, wka;rA.;
functions iy fjk;a o;a; mdjspspshg .ekSuh' kuq;a C jrA.fha libraries ;=, classes fkdue;'
oeka wm Visual C++ ;=, wka;rA.; jk Libraries lsysmhla ms,sn`oj i<ld n,uq'
fstream fyj;a file stream library
File stream library tl ;=, wka;rA.; jkafka oDV ;egsfha we;s files iusnkaOj lghq;= lsrSfusoS
Ndjs;hg .efkk functions wdoshhs' Tn jsiska ,sjSug woyia lrk lsishus mrs.Kl jevigykloS oDV
;egsfha we;s files iusnkaOj lghq;= lsrSug wjYH kus" Tn fstream library tl tu jevigykg
wka;rA.; l, hq;=h' fstream ;=, fstream (file stream), ofstream (output file stream) iy ifstream
(input file stream) hk classes ;s%;ajh wka;rA.;h' wm files iusnkaO lghq;= isoqlrkafka fuu classes
;=, wvx.= functions Ndjs;fhks'
my; oelafjkafka fstream Ndjs;d lruska ,shkq ,enQ l=vd jevigykls' tu jevigyk m%:ufhka “
test “ kuska txt file tlla Tfns mrs.Klfha C:\ ;egsfhys ksmojkq we;s w;r bkamiq th l=vd message
tlla tu file tl ;=, ,shkq we;:
==============================================
#include<fstream.h>
void main()
{
fstream file;
file.open("C:\\test.txt", ios::app);
file << "...Appending data";
}
==============================================
fuu jevigyk compile lr Run lrkak' bkamiqj C:\ drive tl jsjD; lr tys test kuska text file
tlla ksujS we;s whqre n,kak' tu file tl jsjD; l,fyd;a "...Appending data" hkak tys
,shjS we;s nj Tng fmfkkq we;s'
oeka jevigyfkys isoqjkafka l=ulaoehs n,uq' m,uqj #include keyword tl u.ska fstream.h fyj;a
file stream library tl jevigyk ;=,g wka;rA.; lr we;'
fstream file; hk fla;h u.ska isoqjkafka fstream ys class instance tlla file kuska ;ekSuh'
oeka file.open("C:\\test.txt", ios::app); hk fla;h u.ska" fstream class tflys
open functions tlg call fjs' open u.ska C:\ u; “test.txt” file tl ksmojhs' tys 1jeks argument tl
jkafka ksmojsh hq;= file tflys path tlhs' by; th "C:\\test.txt" h' fojeks argument tl
jkafka ksmojk file tlg o;a; we;=,a lrk jsg th isoqjsh hq;= wdldrhhs' by; ios::app u.ska
olajkafka o;a; we;=,a ;rk iEu jsgu tu o;a;" oekg file tl ;=, we;s o;a; j,g append lrk
f,i fyj;a “w.g wuqKk” f,ihs' th isoqjkafka file << "...Appending data"
81
fla;fhks' fuysoS file tl ;=,g o;a; output lrkafka << operator tl u.sks' ios::app u.ska o;a;
append jSu isoqjk fyhska" Tn by; jevigyk ls%hd;aul lrk iEu wjia:djloSu “…Appending
data” hkak file tl;=, os.ska os.g tl fm,la f,i ,shefjkq we;' ios::app ys app hkq ios keue;s
class tl ;=, wvx.= jk" ixLHd;aul w.hla /.;a variable tlls' by; mrsos th ofstream ;=,g pass
l, jsg file tlg o;a; append jSu isoqfjs' Tng wjYH kus fuu ls%hdldrS;ajh fjkia lsrSug yelsh'
WodyrKhla f,i ios::out variable tl fhoSfuka jkafka" by; jevigyk ls%hd;aul jSfusoS" oekgu;a
wod, kuska file tlla we;akus tys oekg we;s o;a; uldoeuSuh' my; oelafjkafka fus wdldrfhka
open fj; pass l, yels variables h' fusjdg fmdoqfjs “File creation flags” keue;s ku Ndjs; fjs'
Ios::app
w,q;a o;a; file tflys w.g append lrhs'
Ios::binary
File tl binary o;a; f,i jsjD; lrhs'
Ios::nocreate
olajd we;s file tl oekgu;a oDV
;egsh u; mj;shs kus muKla th jsjD;
lrhs'
Ios::noreplace
olajd we;s file tl oekg oDV
;egsh u; fkdmj;shs kus muKla
th jsjD; lrhs'
Ios::out
o;a; ,sjSu i`oyd jsjD; lrhs' oekg we;s
o;a; uldouhs'
Ios::trunk
o;a; ,sjSu i`oyd jsjD; lrhs' oekg we;s
o;a; uldouhs'
by; fhdod we;s “jsjD; lsrSu” hkafkka oelafjkafka file tl mdjspsps lrkakd jsiska jsjD; lsrSula
fkdjk nj i,lkak' File tllg C++ u.ska lsisjla isoqlsrSug fmr th C++ jevigyk jsiska
“jsjD; l, hq;=hs”' by; jsjD; lsrSu f,i ye`oskafjkafka thhs'
File tlla jsjD; lsrSug yels fyda fkdyel`oehs oek.ekSu
fus i`oyd my; fla; fldgi Ndjs;d l, yelsh' my; fail function tl u.ska" file tl open l,
fkdyelskus true w.h ksmojkq we;:
=========================================
fstream file;
file.open("C:\\test.txt", ios::app);
if(file.fail()){
cout<< “File open error!” << endl;
}
=========================================
Files lshjSu
82
my; oelafjk jevigyfkaoS" while loop tlla u.ska file tll wvx.= characters ish,a,, tla jrloS
character tl ne.ska char variable tlla ;=, .nvd lrKq ,efns' tysoS file tlska o;a; ,nd .efkkafka
fyj;a file tlska o;a; lshjkq ,nkafka get function tl u.sks' get u.ska file tflys we;s characters
tl ne.ska lshjd th c ;=,g we;=,a lrhs' bkamiq cout u.ska tu character tl Terminal window tl
u;g output lrhs'
=================================================
#include<fstream.h>
#include<iostream.h>
void main()
{
fstream file;
file.open("C:\\test.txt",ios::in|ios::out);
char c;
while(!file.eof()){
file.get(c);
cout<< c <<endl;
}
}
=================================================
string library
string library tl Tn ,shk jevigyklg wka;rA.; lsrSfuka" Tng jevigyk ;=, string data type
tlg wh;a variables Ndjs;hg .ekSug yelshdj ,efns' Tn uSg by;oS char arrays Ndjs;d lrk wdldrh
oqgqjd' char arrays Ndjs;d jkafka text strings iu. lghq;= lrk jsgoS njo oqgqjd' kuq;a Tn oqgq mrsos
char arrays Ndjs;d lsrSu u`ola oqYalr h' Text strings hkq mrs.Kl jevigykloS Ndjs;hg .efkk
iq,N fohls' fujeks iq,Nj Ndjs;d jk fohla oqYalr whqrska mdjspspshg .ekSug isoqjSu kqiqoqiqh'
fuhg ms,shuls fuysoS wm mdjspsps lsrSug iQodkus jk string data type tl'
string tlla hkq we;af;kau wNHka;r f,i ilik ,o char array tllah' kuq;a string tlla hkq
yqfola char array tlla muKlau fkdjk w;r th C++ class tlls' fus nj string kuska library tlla
C++ ;=, ;snSu u.ska meyeos,s fjs'
fuu string class tl ;=, we;af;a char arrays Ndjs;fhaoS Tng uqyqKmEug isoqjk wmyiq;d u.yerSug
ksrAudKh lrk ,o functions h' oeka wm fuu function fudkjdo iy tajdhska isoqlsrSug yelafla
ljrlaoehs jsuid n,uq'
my; oelafjkafka string library tl jevigyklg we;=,a lr string type variable tlla define lrk
wdldrhhs:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
83
string str = "The Millenium";
cout<< str << endl;
}
=========================================
string data type tl Ndjs;d lrkjsg uq,ska l,hq;= foa 2la we;' m,uqjekak kus fuysoS jevigyk
;=,g #include u.ska we;=,a l,hq;= jkafka iostream.h file tl fkdj iostream file tl jSuhs' fojkqj"
string library tl #include lsrSfuka miqj by; jevigyfkys oelafjk mrsos" using namespace std;
keue;s fla;h type l, hq;=h' fus ls%hd fol isoqlsrSfuka wk;=rej Tfns jevigyk ;=, strings
mdjspspshg .; yel' by; jevigyk Run l, jsg string str ys .nvd jS we;s “The Millenium” hk
text string tl print jkq we;'
by; jevigyfkys isoqlr we;af;a str kuska string class tflys instance tlla iEoSuh' string class tl
;=, we;s functions j,g call lsrSug kus wm idudkHfhka classes iusnkaOfhka isoqlrk mrsosu" fuu
str instance tl u.ska function calls isoql, hq;=h' oeka wm fu;ekaisg string class tl ;=, we;s fuu
functions y`oqkd .ekSu wrUuq'
append function
append function tl Ndjs;d lrkafka lsishus string tllg ;j;a string tlla iusnkaO lsrSug isoqjk
wjia:dj,oSh' append function tflys wjia:d lSmhla we;' fufia jkafka class string ;=, append
kuska hq;a functions lSmhla ;snSu fya;=fjks' fus wjia:d w;=rska 2la ms,sn`o i,ld ne,Su iqoqiqh'
1. append (string str)
append function tflys fuu wjia:dfjsoS wm thg pass hq;= jk str keue;s argument tl jkafka" uq,a
string tlg append jsh hq;= fojeks string tlhs' my; WodyrKh n,kak:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = " & The X-Files";
str1.append(str2);
cout<< str1 << endl;
}
by;oS str1 yd str2 keue;s strings 2 la idod we;' wmg fuysoS wjYH jkafka str1 string tflys w.g
str2 string tl iusnkaO lsrSuhs' fus i`oyd str1 u; append function tlg call lr we;s wdldrh n,kak:
str1.append(str2). append fj; pass lr we;s argument tl jkafka str2 string tlh' fuhska isoqjkafka
str1 ;=, we;s text string tl jk “The Millenium” hkak w.g str2 ;=, we;s “ & The X-Files” hk text
string tl append fyj;a iusnkaO jSuh' tjsg function call tflys m%:sm,h f,i str1 string tflys
wvx.= jk string tl “The Millenium & The X-Files” hkak njg m;a jkq we;'
kuq;a fuh append function tlg call lsrSulska f;drjo isoql, yelsh' fus i`oyd wm mdjspspshg
.kafka iqmqreoq “ + ” operator tlhs' my; fuu wjia:dj oelafjs:
=========================================
#include<iostream>
84
#include<string>
using namespace std;
void main(){
string str1
string str2
str1 = str1
cout<< str1
}
= "The Millenium";
= " & The X-Files";
+ str2;
<< endl;
=========================================
tkus wm integer data fyj;a ixLHd iussnkaOfhka Ndjs;d lrkq ,nk + operator tl fuysoS strings
iusnkaOfhkao Ndjs;djg .; yelsh'
oeka append ys 2 jk wjia:dj i,ld n,uq'
2. append (string str, int position, int numberOfChars)
append function tflys fuu 2 jeks wjia:dfjsoS tla string tllg fjk;a string tll f;dard.;a
fldgila muKla iusnkaO lsrSfus yelshdj we;sfjs' Function tlg pass l, hq;= m,uq argument tl
jkafka uq,a string tlg iusnkaO l, hq;= 2 jeks string tlhs (string str)' 2 jeks argument tl kus str
string tflka append lsrSu wdrusN l, hq;= ia:dkhhs (int position)' f;jeks argument tl kus"
position argument tlska oelafjk ia:dkfha isg str string tlska append l, hq;= characters
ixLHdjhs(int numberOfChars)' fuh jvd;a meyeos,s lrk WodyrKhla my; oelafjs' tysoS isoqjkafka
str1 ys wka;hg" str2 ys 2 jk character tflys isg 8 jeks characters tl olajd we;s fldgi iusnkaO
lsrSuhs:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = " & The X-Files";
str1.append(str2, 2,8);
cout<< str1 << endl;
}
=========================================
by;" str1 fj; str2 ys “The X-F” hk fldgi append fjs' fuh str2 ys 2 jk character tflys isg 8
jk character tl olajd we;s fldgihs' str2 ys 2 jk character tl hkq tys “T” g fmr we;s ysveihs'
we;eus jsg fuu ysvei we;af;a 2 jk ia:dkfha fkdj f;jk ia:dkfha fkdfjsoehs Tng isf;kakg
mq,qjk' kuq;a we;af;kau string tll character index tl wdrusN jkafka nskaoqfjks' tfia jkafka
string tlla hkq we;a; jYfhkau character array tlla jSu ksidh' Tn okakd mrsosu array tll index
tl wdrusN jkafka 0 fjks' fusksid wmg str2 ys “T” g fmr we;s ysvei yuqjkafka tys 2 jk
ia:dkfhaoShs' fus wkqj wjidkfhaoS str1 ys w.h jkafka “The Millenium” yd “The X-F” tl;= jSfuka
iefok “The Millenium The X-F” hkakh'
assign function
85
assign function tl u.ska isoqjkafka mj;sk string tll we;s o;a;h uld oud thg fjk;a string tlla
fyda string tlska fldgila wdfoaY lsrSuh' assign function tlgo wjia:d lSmhla we;'
1. assign (string str)
assign ys fuu wjia:dfjsoS isoqjkafka str ys we;sfoa yqfola fjk;a string tlla fj; wdfoaY lsrSuh'
fuh my; oelafjs:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = " & The X-Files";
str1.assign(str2);
cout<< str1 << endl;
}
=========================================
by;oS str2 ys we;s foa str1 g wdfoaY fjs' fufyhska" assign jSu wjika jQ jsg str1 yd str2 tlsfklg
iudk jkq we;'
2. assign (string str, int position, int numberOfChars)
assign function tflys arguments ms,sn`o jsia;r my; oelafjs'
Arguments
string str:
uq,a string tlg assign l, hq;= 2 jeks string tl'
int position:
str string tlska assign lsrSu wdrusN l, hq;= ia:dkh'
int numberOfChars:
int position argument tlska oelafjk ia:dkfha isg" uq,a string tlg" str string tlska append l, hq;=
characters ixLHdj'
Wod:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = " & The X-Files";
86
str1.assign(str2,2,8);
cout<< str1 << endl;
}
=========================================
by; WodyrKfhaoS str1 fj; str2 ;=, we;s text string tflys index 2 - 8 fldgi wdfoaYfjs' ta wkqj
str1 = “ The X-F” jkq we;'
at function
at u.ska string tll lsishus ia:dkhl we;s character tlla ,nd foa'
at (int position)
Arguments
int position:
Character tl ,nd.; hq;= ia:dkfha index number tl'
my; WodyrKfhaoS str1 ys 4 jk ia:dkfha we;s character tl(M) ,nd f.k th Terminal window tl
u; print flfrA:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
cout<< str1.at(4) << endl;
}
=========================================
compare function
1. compare (string str)
fuu function tlska strings hq.,la iudk oehs mrSCId l, yel' strings hq., iudk kus function tl
u.ska 0 w.h return lrkq ,nk w;r" iudk fkdfjs kus 1 fyda -1 w.h return lrkq ,efns'
Arguments
string str:
m,uq string tlg iudkoehs mrSCId l, hq;= 2 jeks string tl'
Wod:
=========================================
87
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = " & The X-Files";
string str3 = "The Millenium";
int check = str1.compare(str3); //(1)
cout<< check << endl;
check = str1.compare(str2); //(2)
cout<< check << endl;
}
=========================================
by; (1) wjia:dfjsoS int check ys w.h 0 jkq we;af;a str1 yd str3 iudk ksidh' (2) wjia:dfjsoS str1 yd
str2 iudk fkdjk ksid check ys w.h 1 fjs'
kuq;a kej;;a fuh string function tlla Ndjs;d fkdlr == operator tl Ndjs;d lr o isoql, yelsh'
kuq;a fuysoS ,efnkafka fmr wjia:djg jsreoaO jk output tlla nj Tng jegfykq we;:
Wod:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = " & The X-Files";
string str3 = "The Millenium";
bool check = (str1 == str3); //(1)
cout<< check << endl;
check = (str1 == str2); //(2)
cout<< check << endl;
}
=========================================
2. compare (int pos, int charN, string str)
fuhska tla string tll fldgila ;j;a string tllg iudk oehs mrSCId l, yel' strings hq., iudk
kus function tl u.ska 0 w.h return lrkq ,nk w;r" iudk fkdfjs kus 1 fyda -1 w.h return lrkq
,efns'
Arguments
int pos:
str ys" mrSCIdj i`oyd Ndjs;d l, hq;= fldgfia wdrusNl ia:dkh'
88
int charN:
str ys mrSCId lsrSu i`oyd .efkk characters .Kk'
string str:
mrSCIdjg ,la flfrk 2 jeks string tl'
my; WodyrKfhaoS isoqjkafka str2 ys index 14 isg we;s characters 13 (“The Millenium”) " str1 g
iudkoehs mrSCId lsrSuh' fuysoS th iudk jk ksid check ys w.h 0 fjs'
=================================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = "The X-Files & The Millenium";
int check = str2.compare(14, 13, str1);
cout<< check << endl;
}
=================================================
empty function
fuu function tl u.ska mrSCIdj, ,la lrkafka lsishus string tlla ysia string tllao hkakhs' fuhg
lsisu argument tlla pass l, hq;= ke;' mrSCId lrkq ,nk string tl ysia kus" empty u.ska true,
return fjs'
Wod:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = "";
bool check = str1.empty();
cout<< check << endl; //(1)
check = str2.empty();
cout<< check << endl; //(2)
}
=========================================
by; (1) wjia:dfjsoS" str1 ;=, text string tlla we;s ksid" check = false (0) jk w;r (2) wjia:dfjsoS"
str2 ;=, lsisjla fkdue;s ksid check = true (1) fjs'
89
erase function
erase function tl u.ska string tll fldgila uld oeush yel'
erase (int pos, int charN)
Arguments
int pos:
wod, string tlska o;a; uld oeuSu werUsh hq;= ia:dkh'
int charN:
uld oeush hq;= characters .Kk'
Wod:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
str1.erase(4,4);
cout<< str1 << endl;
}
=========================================
by;" str1 ys 4 jk character tflys isg characters 4 la uld oeuSu isoqfjs' tjsg b;srs jkafka “The
enium” hkakhs'
find function
find u.ska string tll we;s ;ks character tlla fyda character string tlla i`oyd mrSCId l, yel'
wod, string tl ;=, mrSCIdjg ,la flfrk character string tl ;sfns kus" function tl tu character
string tl yuqjk ia:dkfha index number tl return lrkq we;'
find (string str)
Arguments
string str:
wod, string tflys ;sfn`oehs mrSCId l, hq;= character string tl'
Wod:
=========================================
#include<iostream>
#include<string>
using namespace std;
90
void main(){
string str1 = "The Millenium";
string str2 = "Millenium";
int pos = str1.find(str2);
cout<< pos << endl;
}
=========================================
fuysoS str1 ;=, str2 mj;shs oehs mrSCId flfrk w;r" str1 ;=, “Millenium” hkak yuqjk ia:dkh jk
index 4 w.h pos ;=, .nvd jkq we;'
insert function
fuu.ska tla string tll TskEu ia:dkhlg fjk;a string tlla we;=,a l, yel'
insert (int pos, string str)
Arguments
int pos:
uq,a string tlg str string tl we;=,a l, hq;= ia:dkh'
string str:
we;=,a l, hq;= string tl'
Wod:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = "new ";
str1.insert(4,str2);
cout<< str1 << endl;
}
=========================================
by;" str1 ;=, 4 jk ia:dkhg str2 we;=,a fjhs' ta wkqj kj str1 jkq we;af;a “The new Millenium”
hkakh'
91
length function
fuu.ska string tll we;s characters ixLHdj fyj;a tys length tl ,nd.; yel'
length ()
Arguments
fuys arguments ke;'
Wod:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
int len = str1.length();
cout<< len << endl;
}
=========================================
by;" str1 ys characters ixLHdj jk 13 ,efns'
[] operator
fuu operator tl Ndjs;fhka string tll hus ia:dkhl we;s character tlla ,nd.; yelsh'
Wod:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
char c = str1[6];
cout<< c << endl;
}
=========================================
by;" c ;=,g str1 ys 6 jk ia:dkfha we;s “l” ,nd .efka'
replace function
92
replace function tl u.ska tla string tll TskEu ia:dkhl we;s character string tlla fjkqjg
fjk;a string tlla wdfoaY l, yel'
1. replace (int pos, int charN, string str)
Arguments
int pos:
str string tl u.ska" uq,a string tl replace lsrSu wdrusN l, hq;= ia:dkh'
int charN:
replace jk jsg m,uq string tlska bj;a l, hq;= characters .Kk'
string str:
wdfoaY l, hq;= string tl'
my; WodyrKfhaoS" str1 ys “Millenium” hkak fjkqjg “X-Files” hkak wdfoaY jkq we;' tksid str1 =
“The X-Files” njg m;ajkq we;:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = "X-Files";
str1.replace(4,9,str2);
cout<< str1 << endl;
}
=========================================
2. replace (int pos1, int charN1, string str, int pos2, int charN2)
Arguments
int pos1:
str string tl u.ska" uq,a string tflys replace lsrSu wdrusN l, hq;= ia:dkh'
int charN1:
replace jk jsg uq,a string tlska bj;a l, hq;= characters .Kk'
string str:
wdfoaY l, hq;= string tl'
int pos2:
str string tflys wdfoaYh wdrusN l, hq;= ia:dkh'
int charN2:
93
str string tlska wdfoaY l, hq;= characters ixLHdj'
my; WodyrKfhaoS" str1 ys “Millenium” hkak fjkqjg str2 ys “X-Files” hk fldgi thg wdfoaY jkq
we;' tksid str1 = “The X-Files” njg m;ajkq we;:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = "The X-Files";
str1.replace(4,9,str2,4,7);
cout<< str1 << endl;
}
=========================================
substr function
substr function tl u.ska string tll fldgila kj string tlla f,i ,nd.; yel'
substr (int pos, int charN)
Arguments
int pos:
wod, string tl fjka lsrSug wjYH ia:dkh'
int charN:
string tlska fjka lr.; hq;= characters ixLHdj'
Wod:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = str1.substr(4,9) ;
cout<< str2 << endl;
}
=========================================
by; WodyrKfhaoS str1 ys “Millenium” hk fldgi" substr u.ska fPaokh lr" th kj string tlla
f,i str2 fj; wdfoaY flfrA'
94
swap function
swap function tl u.ska tla string tll we;s o;a;h fjk;a string tll we;s o;a;h iu. yqjudre
l, yel'
substr (string str)
Arguments
string str:
uq,a string tl iu. o;a; yqjudre l, hq;= 2 jeks string tl'
Wod:
=========================================
#include<iostream>
#include<string>
using namespace std;
void main(){
string str1 = "The Millenium";
string str2 = "The X-Files";
str1.swap(str2);
cout<< str1 << endl;
cout<< str2 << endl;
}
=========================================
by;oS" str1 yd str2 hk strings foflys wvx.= text strings hq., yqjudre flfrA' ta wkqj wjidkfhoS
str1 ys ;snsh hq;af;a “The X-Files” jk w;r str2 ys ;snsh hq;af;a “The Millenium” fjs'
ctime library
ctime library tl ;=, wvx.= jkafka ld,h iusnkaO o;a; iu. lghq;= lsrSu i`oyd ksrAudKh flreKq
functions h' WodyrKhla f,i my; jevigyk wo oskh iy ld,h console window tl u;
fmkajhs:
=========================================
#include<ctime>
#include<iostream.h>
void main()
{
time_t tnow = time(0);
cout<< asctime(gmtime(&tnow));
}
=========================================
95
by;" time_t hkq ld,h .nvd lsrSu msKsi C++ ;=, we;s o;a; jrA.hhs' ta wkqj" oeka ld,h .nvd
lsrSug time_t type tlska tnow variable tl idod we;' time function tl u.ska oeka fjs,dj ,ndf.k
th tnow ;=, .nvd flfrA' bkamiq gmtime yd asctime u.ska now ;=, we;s ld,h text string tlla
njg m;a fldg Terminal tl u; osia lrjhs'
10' C++ u.ska ir, Media Player tlla iEoSu
fuh wmf.a wjika mrspsfpsohhs' fuysoS wm i,ld ne,Sug hkafka C++ u.ska ir, Media
Player tlla idok wdldrhhs' fuys wm iEoSug woyia lrk mrs.Kl jevigyk we;a; jYfhkau
“iqmsrs” uDoqldx.hla fkdjkq we;' Tng fuh wka;rAcd,h ;=, wf,js fldg uqo,a bmhSu jeks fohla
l, yels fkdjkq we;! fuh bosrsm;a lsrSfus woyi yqfola by; mrspsfpso j,oS bf.k .;a ixl,am
mdjspsps lr m%fhdackj;a mrs.Kl jevigyka idok wdldrh ms,sn`o uq,a jegySula Tng ,ndoSuhs'
tfukau C++ ms,sn`o Tnf.a WfoHda.h jevss lsrSuhs'
oeka" wm iEoSug iQodkus jk media player tl u.ska Tng mp3, wav, mpeg, avi iy wmv hk
formats j,ska hq;= media files Odjkh lsrSug yelsjkq we;' fuh ir, Object oriented program tlla
jkq we;s w;r" inheritance iy wfkl=;a ixlSrAK Object oriented l%ufjsohka Ndjs;d lr fkdue;s ksid
kjl l%uf,aLlhkag jevigyk jgyd .ekSug myiq jkq we;'
wmf.a Media player program tlg we;af;a my; oelafjk wdldrfha ir, DOS interface tlls ^u;=
msgls&'
(?mh 10'1)
Tn jevigyk Odjkh l, jsg m,uqfjkau osiajk menu tl by; wdldrh .kq we;' Tng audio fyda
video file tlla play lsrSug wjYH jQ jsg l, hq;af;a keyboard tflys A h;=r tnSuhs' tjsg jevigyk
u.ska my; oelafjk mrsos file path tlla jsuikq we;'
96
(?mh 10'2)
Tn file path tl jevigykg ksjerosj imhd Enter button tl press l, jsg tu file tl play jSu"
jevigyk u.ska “Playing…” keue;s message tl osia lruska" ?mh 10'3 ys fmfkk f,i wrTkq
we;' File tl video tlla kus video tl osialsrSu i`oyd wu;r window tlla iajhxls%hj jsjD; jkq
we;'
(?mh 10'3)
[fuysoS ie,lsh hq;= fohla kus media file tflys ku tla fldgilska muKla hq;= jSug wjYH jSuhs'
fldgia lSmhlska hq;= kula we;s file tlla(Wod: Tears in heaven.mp3) Odjkh lsrSug fuu
jevigykg fndfydajsg fkdyels jkq we;' fuhg m%;slrAuhka we;s kuq;a fuysoS tu Wml%u .ek
idlpspd lsrSug fkdhkafkuq']
kej; main menu tl ,nd .ekSug Tn Enter button tl press l, hq;=h' tjsg my; f,i kej;;a
menu tl jevigyk u.ska osia lrhs'
(?mh 10'4)
oeka" oekg play fjuska mj;sk media tl" D tnSu u.ska pause l, yelsh' tjsg “Paused…” hkqfjka
message tlla print fjs ^?mh 10'5&'
97
(?mh 10'5)
kej;;a play jSu resume lsrSug kus A o kj file tlla play lsrSug kus S Tnd bkamiqj A o tnsh
hq;=h' Resume l, jsg “Resumed…” message tl print fjs ^?mh 10'6&'
(?mh 10'6)
S u.ska file tl stop l, yelsh' tjsg “Stoped…” message tl print fjs ^?mh 10'7&'
(?mh 10'7)
jevigykska bj;ajSug kus F tnsh hq;=h' tjsg “Bye!” hk message tl osia lruska jevigyk ksu
jkq we; ^?mh 10'8&'
98
(?mh 10'8)
jevigyk ls%hdlrkafka by; oelajqkq wdldrhghs' oeka wm fuu jevigyk ,shkafka flfiaoehs
n,uq'
Project Media Player
1. Visual C++ u.ska Media Player kuska kj Win32 Console Application project tlla idokak'
Project tl idok jsg “An empty project” keue;s option tl select lsrSug u;l ;nd.kak'
2. oeka “MediaPlayer” kuska kj C++ file tlla project tl ;=,g we;=,a lrkak'
3. “MediaPlayer” keue;s C/C++ header file tllao project tlg we;=,a lrkak' ^header files ms,sn`oj
“Object Oriented Programming” keue;s mrspsfpsofha wjika Nd.fhaoS jsia;r flrsKs&' oeka Tfns
Project workspace tflys FileView window tl ?mh 10'9 wdldrh .kq we;'
(?mh 10'9)
4. oeka MediaPlayer.h file tl double click lr th jsjD; lr .kak'
5. MediaPlayer.h ;=,g my; oelafjk mrsos" conio, Shlobj, iostream, string hk libraries include
lrkak'
99
====================================
#include<conio.h>
#include<Shlobj.h>
#include<iostream>
#include<string.h>
====================================
conio library tl wm fuysoS include lrkafka ta ;=, we;s getch() keue;s function tl wmf.a
jevigyfkaoS mdjspspshg .ekSu msKsih' Shlobj library tlska wm ,nd .kafka wsprintf,
mciSendString yd MessageBox hk functions h' fuu functions ms,sn`oj miqj jsia;r flfrA'
6. wm string library tl mdjspsps lrk ksid uS,.g “using namespace std” hkak fhosh hq;=h'
====================================
#include<conio.h>
#include<Shlobj.h>
#include<iostream>
#include<string.h>
using namespace std;
====================================
7. oeka my; ;o l,q wl=rska oelafjk code tl type lrkak'
====================================
#include<conio.h>
#include<Shlobj.h>
#include<iostream>
#include<string.h>
using namespace std;
#pragma comment(lib, "winmm.lib")
====================================
fuu kj code tl u.ska isoqlrkafka winmm.lib keue;s library file tl jevigykg we;=,a
lsrSuh' fuh oekg wm include lr we;s Shlobj library tl iu. ls%hdlrk w;r winmm.lib
fkdue;s kus wmg bosrsfhaoS yuqjSug kshus; mciSendString function tl ls%hd fkdlrkq we;'
8. Media Player jevigyk ir, object oriented program tlla f,i ielish hq;= ksid" jevigyk
;ks object tllska muKla hq;=j ksujSug woyia flfrA' fuu object tl Player kuska kus lruq'
Player class tl ;=, MediaFunc kuska hq;=" ;u argument tl f,i integer variable tlla ,nd
.kakd function tlla o Stop, Play, Pause hk boolean variables ;s%;ajho mediaCommand iy
pathSrc hk character arrays fkdfyd;a character strings hq.,o wm wk;rA.; l, hq;=hs ^tu
variables j, yd function tflys ldrAhhka miqj jsia;r fjs&' Player class tl oeka MediaPlayer.h
header file tl ;=, my; mrsos declare lrkak'
====================================
#include<conio.h>
#include<Shlobj.h>
100
#include<iostream>
#include<string.h>
using namespace std;
#pragma comment(lib, "winmm.lib")
class Player{
public:
void MediaFunc(int x);
bool Stop;
bool Play;
bool Pause;
char mediaCommand[50];
char pathSrc[50];
};
====================================
by; player class ys declaration tl type lsrSu wjika jQ jsg workspace tl ;=, we;s classView
window tflys kj Player class tl yd th ;=, wka;rA.; function tl iy variables my; f,i
osiajkq we;'
(?mh 10'10)
Player class tflys wka;rA.; function tflys yd variables j, ldrAhhka fudkjdoehs oeka wm
uQ,sl jYfhka y|qkd.ksuq'
•
MediaFunc(int x) – fuu function tflys ldrAhh kus thg pass lrkq ,nk integer tlg wkqj
ls%hdjka isoqlsrSuh' tkus" Tn thg 1 w.h pass l,fyd;a function tl u.ska Tn olajk media file
tl play lsrSu wdrusN l, hq;=h' Tn pass lrkafka 2 kus" th media file tl Odjkh jSu
keje;ajsh hq;=h (stop)' Pass lrk w.h 3 kus media file tl pause l, hq;=h'
•
pathSrc – fuu char array tl wm Ndjs;d lrkafka play lsrSug wjYH media file tflys file path
tl .nvd lsrSugh'
•
mediaCommand – fuys ldrAhh miqj i,ld n,uq'
•
Play, Pause, Stop – fuu Boolean variables ;s%;ajh Ndjs;d jkafka media player tflys state tl
fyj;a oekg th media tlla play lruska isgskjdo" Pause lr we;`o tfia;a ke;skus lsisjla
fkdlruska isgskjdo hkqfjka jdrA:d lsrSu msKsih'
by; oelajQ functions iy variable ms,sn`oj bosrsfhaoS Tng jvd fyd`o jegySula ,efnkq we;'
9. uS,.g l, hq;af;a by; declare lrk ,o MediaFunc function tl define lsrSuhs' fuh isoqlsrSu
i`oyd wm MediaPlayer.cpp file tlg hdhq;=h' ulaksidoh;a Header files ;=, lsisjla define l,
101
fkdyels w;r tajd we;af;a declarations i`oyd muKla fyhsKs' MediaPlayer.cpp file tl open lsrSu
i`oyd workspace tflys MediaPlayer.cpp file tl u; double click lrkak'
10. MediaFunc function tl define lsrSug m,uqfjka MediaPlayer.h header file tl MediaPlayer.cpp
file tl ;=,g we;=,a fldg isgsh hq;=h' fuu.ska MediaFunc ys declaration tl jevigyk ;=,g
we;=,a fjs' my; code tlska fuh isoqfjs' th MediaPlayer.cpp ;=, type lrkak:
====================================
#include "MediaPlayer.h"
====================================
11. oeka function tl define lsrSu wrUuq' my; oelafjkafka tys m,uq wOshrhs ^class functions define
lsrSu ms,sn`oj wm “Object oriented programming” mrspsfpsofha i,ld ne,qfjuq&'
====================================
#include "MediaPlayer.h"
void Player::MediaFunc(int x){
}
====================================
oekg MediaFunc function tl ysia function tlla nj Tng fmfka'
12. Tn by; lshjQ mrsos MediaFunc function tflys ldrAhh kus thg pass lrkq ,nk argument
tflys w.hg wkqj ldrAhhka isoqlsrSuhs' fusksid function tl ;=, m,uqfjka u l,hq;= foa kus
function tl ;=,g pass lrk ,o variable tflys w.h l=ulaoehs mrSCId lr ne,Suhs' tjsg tla tla
w.hka pass l,jsg isoql, hq;af;a l=ulaoehs wmg ;SrKh l,yel' fus i`oyd wm fuysoS
fhdod.kafka switch – case program flow control statement tlh ^program flow control statements
ms,sn`oj wm 4 jk mrspsfpsofhaoS bf.k.;a;d&' switch-case fhoQ jsg MediaFunc osiafjk wdldrh
my; oelafjs:
====================================
#include "MediaPlayer.h"
void Player::MediaFunc(int x){
switch(x){
case 1:
break;
case 2:
break;
case 3:
break;
}
}
====================================
102
fuysoS switch-case u.ska isoqjkafka MediaFunc function tlg pass lrkq ,nk int x argument
tflys w.h 1 o 2 o tfia ke;skus 3 fjso hkak msrslaiSuhs'
13. oeka wm fuu switch-case ys case statements ;=,g wod, codes fhosh hq;=h' my; oelafjkafka
switch-case ;=, type l,hq;= mrs.Kl fla;hkaf.ka fldgils'
========================================================
#include "MediaPlayer.h"
void Player::MediaFunc(int x){
switch(x){
case 1:
// if paused or playing, no menu
if(Pause!=true && Play!=true){
cout<< "Enter the path: " << endl;
cin>> pathSrc;
wsprintf(mediaCommand, "play %s", pathSrc);
cout<<"\nPlaying..."<<endl;
}
Pause = false;
Stop = false;
Play = true;
break;
case 2:
Pause = false;
Stop = true;
Play = false;
break;
case 3:
Pause = true;
Stop = false;
Play = false;
break;
}
}
========================================================
oeka fuu kj codes j,ska isoqjkafka l=ulaoehs y|qkd .ekSug lrk W;aidyh' by,skau we;s case
1: fj;ska wrTuq'
uq,skau we;s if statement tl u.ska Pause yd Play hk boolean variables, true fjsoehs mrSCId lrk
nj Tng fmfka' fuu.ska" oekg lsishus media file tlla play fjuska fyda pause jS fyda mj;shs oehs
y|qkd.ekSu isoqfjs' th isoqjkafka fufiahs :
by;skao Tn lshjQ mrsos Pause, Stop yd Play variables j, ldrAhh lsishus fudfyd;l Media
Player tl isoqlrk ldrAhh jdrA:d lr ;enSuhs'
tkus"
•
•
Media Player tl file tlla play lruska isgskjdkus Play variable tflys w.h true jsh hq;= w;r
Pause yd Stop variables j, w.hka false jsh hq;=h'
Media Player tl jsiska Odjkh fjuska mej;s file tlla pause lr we;akus Pause variable tflyss
w.h true jsh hq;= w;r wfkla variables hq.f,ys w.hka false jsh hq;=h'
103
•
Media Player tl lsis;a fkdlr k`oyiaj we;akus Stop variable tflys w.h true jsh hq;= w;r
wfkla variables hq., false jsh hq;=h'
oeka" Tng by; code tflys fmfkk mrsos iEu case statement tlla ;=,u fuu Pause, Stop yd Play
variables j, w.hhka wod, f,i true fyda false lr we;' tkus" (MediaFunc function tl ;=,g 1
w.h pass l, jsg) case 1 ;=,oS isoqjsh hq;af;a file tlla play lsrSu jk fyhska" t;=,oS Play ys w.h
true lr wfklajd false lr we;' tfukau (MediaFunc function tl ;=,g 2 pass l, jsg) case 2 ;=,
isoqjsh hq;af;a file tlla stop lsrSu ksid tysoS Stop ys w.h muKla true lr we;' wjidk jYfhka
(MediaFunc function tl ;=,g 3 pass l, jsg) case 3 ;=, file tlla pause jSu isoqjk ksid Pause ys
w.h true lr we;' (fuu play, pause yd stop jSu isoqjkafka flfiaoehs bosrsfhaoS Tng oel.; yelsh)
tfiakus" wm i,ld n,uska isgs case 1 ;=, we;s if statement tlska isoqjkafka l=ulaoehs Tng jgyd.;
yel' tkus" fuu if ;=, mrSCId lrk ;;ajh i;H jkafka MediaPlayer tl jsiska file tlla play
lruska fyda pause lr fkdue;sjsg fyj;a" Stop ys w.h true jk jsgoShs' ^fuu if u.ska Play yd Pause
ys w.hka false oehs mrSCId lsrSu fjkqjg Stop ys w.h true fjsoehs hkak o mrSCId l, yelshs'&
if u.ska mrSCId lrkq ,nk ;;ajh i;H fjs kus uS,.g jydu isoqjkajkafka “Enter the path:” hk
message tl Terminal window tl ;=, print lruska file path tlla jevigyk mdjspsps lrkakdf.ka
jsuiSu nj Tng code tflys B,. fma,sh foi ne,Sfuka jegfya' tkus" jevigyk Tfnka file path
tlla jsuikafka th oekgu file tlla play lruska fkdue;skus fyda pause lr fkdue;s kus muKls'
fuysoS Tn" play lsrSu wjYH media file tflys file path tl console window tl ;=,g type lr" Enter
button tl press l, jsg" tu type l, path tl cin u.ska pathSrc string tl ;=,g we;=,a lrkq ,nk
nj code tflys B,. fm,ska osiafjs ^pathSrc keue;s ku idodf.k we;af;a path iy source hk jpk
hq., tla lr" flgs lsrSfuks&'
uS,.g yuqjkafka wsprintf keue;s function tl iys; fla;hhs' wsprintf function tl wvx.= jkafka
Shlobj library tl ;=, nj Tn uSg l,ska oqgqjd' oeka wm fuu wsprintf function tl ls%hd lrkafka
flfiaoehs jgyd .ksuq:
wsprintf function
int wsprintf (char* buffer, char* format, char* source)
fuu function tl u.ska isoqlrkafka source keue;s text string tl format lr ^wjYH wdldrhg ilid&
th buffer keue;s char array tl ;=, .nvd lsrSuh'
Arguments
char* buffer:
Format lrk ,o text string tl .nvd flfrk char array tlg fhduqjqk pointer tlla'
char* format:
Text string tl format l, hq;= wdldrh oelafjk char array tlg fhduqjqk pointer tlla' ^fuu
format jSu isoqjk wdldrh fudfyd;lska i,ld nef,a&
char* source:
Format lsrSug ,la jk char array tlg fhduqjqkq pointer tlls'
wsprintf u.ska isoqjk foa meyeos,s lr.ekSug fmr thg arguments pass lsrSu yd iusnkaO lreKq .ek
u`ola i,ld ne,Su iqoqiqh' my; oelafjk lreKq ms,sn`o wjOdkh fhduq lrkak'
104
by; Tn oqgq mrsos wsprintf fj; call lsrSfusoS wm thg pass l, hq;= jkafka char arrays fyj;a text
strings i`oyd pointers h' lsishus array tllg pointer tlla idod.; yels l%uh jkafka tu array
tflys index 0 ia:dkfha we;s o;a;fha memory address tl" wod, data type tlska hq;= pointer tllg
wdfoaY lsrSu nj Tn “Pointers” keue;s mrspsfpsofha “Arrays i`oyd pointers iEoSu” kus fldgfiaoS
oqgqjd' by; wsprintf function tflys argument pass lsrSfusoS mdjspsps l, hq;af;a fuu uQ,OrAuhhs'
wsprintf ;u ish,q arguments f,i ,nd.kafka char arrays i`oyd pointers h' fus ksid wm thg pass
l, hq;= jkafka by; lshQ mrsos char arrays j, index 0 ia:dk j, addresses h' fuh yqfola tu char
array tflys ku wsprintf ;=,g pass lsrSfuka isoq l, yelsh' ulaksidohla array pointer tlla iEoSu"
pointer tllg yqfola wod, array tflys ku wdfoaY lsrSfuka isoql, yels jSuhs ^fuh “Pointers”
mrspsfpsofhys oelajqKs&' Code tflys isoqlr we;af;a fuhhs' Tn oeka code tl foi ne,qjfyd;a
wsprintf fj; tys 1 jk yd 3 ka jeks arguments f,i pass lr we;af;a mediaCommand iy pathSrc
hk char arrays hq.f,ys kus nj oelsh yelshs'
kuq;a tys 2 jeks argument tl f,i fl,skau text string tlla fhdod we;' we;af;kau fuysoS
isoqjkafkao by; fohu fjkia wdldrhlghs' tysoS 2 jk argument tl f,i variable tlla Ndjs;d
fkdlr Rcqju text string tlla pass lr we;' ^pass lr we;s “play %s” hkafkys wrA:h l=ulaoehs Tn
fudfyd;lska olskq we;&' fuh" “play %s” hk w.h .nvd lrk ,o char array tll address tlla
pass lsrSug yrshgu iudkh'
wsprintf u.ska we;af;kau isoqlrkq ,nkafkaa by; source kuska oS we;s char pointer tlska olajk
char array tl ;=, we;s text string tl wjYH whqrska format lr buffer kuska oS we;s pointer tlska
olajk char array tl ;=, .nvd lsrSuh' fuu format jSu isoqjkafka format kuska oS we;s pointer tlska
olajk char array tl ;=, .nvdj we;s text string tlg wkqjh' tkus" format u.ska olajk string tl
isoqlrkafka source keue;s string tl format lrk wdldrh wsprintf function tlg fmkajd oSuh'
fujeks text strings y|qkajkq ,nkafka format-control strings hk kusks' fuh Media player
jevigyfkys “play %s” f,i oelsh yelsh' fuysoS format-control string tl f,i .efkkafka “ %s ”
hk fldgih'
Format-control string tlla yeujsgu werfTkafka % ,l=Ksks' bkamiqj tk ;ks character tlska
oelafjkafka wsprintf ys f;jeks argument variable tl ;=, we;s o;a;hg ie,lsh hq;= wdldrhhs' by;
format control string tflys ;ks character tl f,i s fhoSfuka oelafjkafka" 3ka jk argument tl
string tlla f,i ie,lsh hq;= njhs' wsprintf function tl u.ska format – control string tflys %
i,l=Kg fmr we;s string fldgi" tkus “play” hkak w.g pathSrc string tflys wvx.= text string tl
wuqKd th mediaCommand ;=, .nvd lsrSu isoqfjs' ta wkqj" WodyrKhla jYfhka Tn pathSrc ;=,g
,ndoS we;af;a “D:\X-Files\track3.wmv” hk file path string tl kus wsprintf function tl
u.ska th “play D:\X-Files\track3.wmv” hk string tl njg yrjd th csCommnad ;=,
.nvd lrkq we;' tkus" format-control string tl jk “%s” hkak bj;ajS t;ekg wsprintf ys 3 jk
argument tl jk string tl wdfoaY fjs' (mediaCommand ;=, .nvd l, fuu text string tflys wrA:h
iy m%fhdackh wm fuu jevigyk bosrshg ,shk jsg Tng meyeos,s jkq we;') wsprintf function tl
execute jQ jydu play jSu werTS we;s nj oelajSug “Playing…” hkak print fjs'
fyd`ohs' by; fldgfiaoS isoql, foa kej; flgsfhka i`oyka l,fyd;a" wm isoqlf,a: Media Player
jevigyk mdjspsps lrkakd jsiska jevigyk ;=,g 1" 2 fyda 3 w.hhka we;=,a l,jsg" tu w.hhkag
wkqj ls%hd;aul jSu msKsi switch-case code tlla ieliSuh' tysoS wm m,uq case statement tl ;=,oS
program tl mdjspsps lrkakdf.ka file path tlla ,nd f.k" tu file path tl pathSrc ;=, .nvd
lf,uq' bkamiqj" wsprintf function tl u.ska “Play” hk string tl w.g pathSrc ys we;s w.h(file
path tl& wuqKd th mediaCommand hk string tl ;=, .nvd lf,uq' fus ish,a, isoqjkafka oekg
jevigyk jsiska file tlla play lruska fyda pause lr ke;s jsg njo if statement tlla u.ska ;yjqre
lf,uq'
oeka wm play l, hq;= media file tl mrs.Klfha oDV ;egsfhys mj;sk ia:dkh oksuq' tys file path tl
wm pathSrc variable tl ;=, .nvd lr we;' oeka l, hq;af;a fuu file tl Odjkh lsrSu i`oyd wjYH
codes ,sjSuh' (fuh idrA:l jSu i`oyd Tfns mrs.Klhg Sound card tlla install lr ;snsh hq;= njo"
mrs.Klhg iusnkaO lr we;s speaker tlla Tn i;=j ;snsh hq;= njo fuysoS i`oyka lsrSu wkjYHh)
105
my; oelafjkafka media file tlla play lsrSu i`oyd wjYH mrs.Kl fla; wvx.= lr ,shkq ,enQ kj
code tlhs'
=============================================================
#include "MediaPlayer.h"
void Player::MediaFunc(int x){
switch(x){
case 1:
// if paused or playing, no menu
if(Pause!=true && Play!=true){
cout<< "Enter the path: " << endl;
cin>> pathSrc;
wsprintf(mediaCommand, "play %s", pathSrc);
cout<<"\nPlaying..."<<endl;
}
Pause = false;
Stop = false;
Play = true;
break;
case 2:
Pause = false;
Stop = true;
Play = false;
break;
case 3:
Pause = true;
Stop = false;
Play = false;
break;
}
if(mciSendString(mediaCommand, NULL, 0, NULL)!=0){
}
}
============================================================
by;oS" kj function tlla jk mciSendString Tng yuqfjs' my; oelafjkafka fuu function tl
ms,sn`o ye`oskajSuhs'
mciSendString function
mciSendString function tl u.ska isoqjkafka thg imhkq ,nk media file tlg media
commands(play, stop, pause, etc) iemhSuhs'
int mciSendString (char* command, char* returnInfo, int bsReturn,
HANDLE callback)
Arguments
char* command:
106
fuu string tflys wvx.= jsh hq;af;a media file tl i`oyd imhk command string tlhs'
command string tl my; format tlg" fyj;a ie,eiaug wkqj ieliS ;snsh hq;=h:
“ MediaCommand, FilePath ” – tkus" m,uqfjka wod, command tl(play, stop, pause, etc) i`oyka
jsh hq;= w;r fojkqj" tu command tl iemhsh hq;= media file tflys path tl i`oyka jsh hq;=h'
char* returnInfo:
int bsReturn:
HANDLE callback:
by; oelafjk arguments ;s%;ajh wm fuu jevigyfkaoS Ndjs;d fkdlrk fyhska tajd ms,sn`o jsia;r
fuysoS fkdi,ld yrskq ,efns' tu arguments Ndjs;d fkdlrk nj oelajSug" code tflys
mciSendString function call tl ;=,oS tajd pass l, hq;= ia:dk i`oyd NULL fyda 0 hk w.hhka
fhdod we;' fuu w.hka fhoSfuka woyia lrkafka arguments i`oyd pass flfrkafka Y=kH jQ w.hka
njhs'
oeka code tflys mciSendString function tlg call lr we;s wdldrh n,kak' ^call jSu if statement
tlla ;=, isoqjkafka wehs oehs miqj jsia;r fjs& m,uqfjka thg pass lr we;af;a oeka media command
tl .nvd lr we;s mediaCommand string tlhs' fuh Tn by;oS oqgq mrsosu m,uqj media command
tllskao" fojkqj file path tllskao iukajs;h (Wod- “play D:\X-Files\track3.wmv”).
mciSendString function tl execute jQ js.i command string tflys oelafjk file tl" command string
tflys we;s media command tl wkqj play, stop fyda pause jkq we;'
fus wkqj" code tl ;=, we;s swich-case block tflys m,uq case statement tl true jkafka kus"
mediaCommand string tflys wvx.= media command tl “play” hkak njg m;a jk ksid" tjsg
mciSendString function tl u.ska wod, file tl play lsrSu wrUkq we;'
mciSendString function tlg call lr we;af;a if statement tlla ;=, isg nj ksrSCIKh lrkak' fuhg
fya;=j kus mciSendString function tl ksis mrsos ls%hd;aul fkdjkjd kus th tiekska u
y|qkd.ekSug fuu.ska yelshdj ,efnk ksihs' mciSendString function tl u.ska return lrkq
,nkafka integer tlla nj by; tys ye`oskajSu ;=,oS Tn olskakg we;s ^Functions u.ska w.hka return
lrkq ,nk wdldrh ms,sn`oj wm “Functions” mrspsfpsofhaoS i,ld ne,qfjuq&' mciSendString idrA:lj
ls%hd;aul jqjfyd;a" tkus thg Tn ,nd fok media file tl play lsrSug yels jqjfyd;a" th return
lrkq ,nkafka 0 w.hhs' mciSendString ys ls%hdldrS;ajh widrA:l jqjfyd;a" tkus th file tl play
lsrSug wfmdfydi;a jqjfyd;a th u.ska return lrkq ,nkafka 0 g jevs w.hls' fuu 0 g jevs w.h"
file tl play lsrSug ork W;aidyfhaoS Woa.; jk .eg,qjg fyj;a Runtime error tl u; r`od mj;shs'
^Runtime errors hk kuska ye`oskafjkafka lsishus mrs.Kl jevigykla ls%hd;aul jk fudfydf;aoS
tu jevigyfkys ls%hdldrS;ajh wvd, jSug fya;=jk errors h&' fusysoS mciSendString function tl
ls%hd;aul fkdjSug jsj`o fya;~ka n,mE yelshs' WodyrKhla jYfhka Tn iemhQ file tl media file
tlla fkdjSug bv we;' tfia;a ke;skus tu file tflys media format tl mciSendString function tl
fkdy`oqkkjd jsh yel' ke;fyd;a yqfola Tn type lrk ,o file path tflys fodaYhla ;sfnkjd jsh
yel'
if statements tl u.ska isoqjkafka tys jryka ;=, we;s ;;ajh i;H jqjfyd;a" tys i`., jryka ;=,
we;s code tl execute lsrSu nj Tn “mrs.Kl jevigykl ls%hdldrS;ajh md,kh lsrSu” hk
mrspsfpsofhaoS oqgqjd' fuu “i;H jSu” hkqfjka we;a;jYfhkau ye`oskafjkafka if ;=, we;s expression
tlska 0 g jvd jevs w.hla bmoSuhs' tfiakus “wi;H jSu” hkq expression tlska 0 w.h ksmojSuhs
^fuh Boolean data type tl ms,sn`oj idlpspd lsrSfusoS i`oyka jsKS&' tfiakus mciSendString ksis mrsos
ls%hd fkdl, js.i fuu if ys jryka ;=, w.h true jk ksid ^mciSendString u.ska 0 g jevs w.hla
ksmofjk ksid& if hgf;a we;s codes, execute fyj;a l%shd;aul jkq we;' fufyhska" wm uS,.g l,
hq;af;a fuu if, true fjs kus fyj;a file tl ksis mrsos Odjkh lsrSug fkdyels kus" B,.g jevigyk
jsiska l, hq;af;a l=ulaoehs ;SrKh lsrSuh' fuu jevigyfkys ir,;ajh Wfoid" wm fuysoS isoq lsrSug
hkafka ir, message box tlla jevigyk mdjspsps lrkakd yg osia lrjSuh' fuu message box tl
;=, "Sorry, can't play your file." hk message tl osiajsh hq;=h' fusi`oyd oeka if ;=,
wka;rA.; jshhq;= codes fudkjdoehs mrSCId lr n,uq:
107
=============================================================
#include "MediaPlayer.h"
void Player::MediaFunc(int x){
switch(x){
case 1:
// if paused or playing, no menu
if(Pause!=true && Play!=true){
cout<< "Enter the path: " << endl;
cin>> pathSrc;
wsprintf(mediaCommand, "play %s", pathSrc);
cout<<"\nPlaying..."<<endl;
}
Pause = false;
Stop = false;
Play = true;
break;
case 2:
Pause = false;
Stop = true;
Play = false;
break;
case 3:
Pause = true;
Stop = false;
Play = false;
break;
}
if(mciSendString(mediaCommand, NULL, 0, NULL)!=0){
system("cls");
MessageBox(NULL,"Sorry, can't play your file.","HALT!",0);
Pause = false;
Stop = false;
Play = false;
}
}
==========================================
if ;=, m,uqfjkau isoqjkafka Terminal window tflys oekg osiajk ish,q foa uld oeuSuh' fuh
system kuska we;s function tl u.ska isoqfjs' system function tl ;u argument tl f,i ,nd.kafka
DOS commands fjs' ^DOS commands hkq Tn Disk Operating System tl hgf;a mrs.Klhla
ls%hdlrjk jsg thg iemhsh hq;= jsOdkhkah&' system function tl u.ska isoqlrkafka thg pass lrkq
,nk DOS command tlg wod, DOS ls%hdldrS;ajh isoq lsrSuhs' wmf.a jevigyfkys system function
tl ;=,g pass lr we;af;a “cls” keue;s DOS jsOdkhhs' Tng DOS commands ms,sn`o oekqula we;af;a
kus" “cls” hkq Terminal window tflys osiajk oE uldouk(clear screen) jsOdkh nj okakjd we;s'
fus ksid fuysoS isoqjkafka fmr lS mrsosu fus jk jsg jevigyk u.ska Terminal window tl ;=,
osiafjk ish,a, uld oeuSuhs'
uS,.g we;s code tl fmr i`oyka message box tl osia lrkq ,nk mrs.Kl fla;hhs' my;
oelafjkafka MessageBox function tflys ye`oskajSuhs'
108
MessageBox function
MessageBox function tl u.ska isoqjkafka thg imhkq ,nk message string tl wka;rA.; jk mrsos
Windows message box tlla jsjD; lsrSuhs'
int MessageBox (HWND hwnd, char* text, char* caption,
int boxType)
Arguments
HWND hwnd:
fuu jevigyfkysoS fuu argument tl mdjspspshg fkd.efkk ksid fuh fkdi,ld yefrA (code
tflys fuu argument tl pass l, hq;= ia:dkhg NULL w.h fhdod we;)'
char* text:
fuu char array tflys wvx.= jsh hq;af;a message box tl ;=, osiajsh hq;= text string tlhs'
char* cpation:
fuh message box tflys title bar tflys osiajsh hq;= title tlhs'
int boxType:
fuu integer tlska oelafjkafka message box tflys type tl fyj;a jrA.hhs'
Windows message boxes jrA. lSmhls' fus iEu message box type tllgu int w.hla we;' boxType
argument tlska oelafjkafka fuu jrA.hhs' my; oelafjkafka message boxes jrA. iy tajdg wod,
boxType w.hhkah'
Message box jrA.h
boxType w.h
Ok button tl muKla iys; message box tl
Ok iy Cancel buttons iys;
Abort, Retry iy Ignor buttons iys;
Yes, No iy Cancel buttons iys;
Yes, No buttons iys;
Retry iy Cancel buttons iys;
Cancel, Try again iy Continue buttons iys;
-
0
1
2
3
4
5
6
jevigyfkys wm boxType argument tl fj; pass lr we;af;a 0 ksid fuysoS jsjD; jkq we;af;a OK
button tlska muKla iukajs; message box tlhs' th ;=," wm jsiska text argument tl i`oyd pass
lrkq ,nk string osia jkq we;s w;r" tys title tl jkq we;af;a caption argument tl fj; pass lrkq
,nk string tlhs'
uS,.g code tflys isoqjS we;af;a Pause, Play iy Stop w.hka false jSuhs' fufia lsrSug fya;=j kus"
oekaa file tl play lsrSug Media Player jevigyk wiu;a jSu fya;=fjka kj file tlla load lsrSu i`oyd
fuu variables reset lsrSu ^kej; uq,a w.hhkag f.k tau& iqoqiq fyhsKs'
fuf;la wm ,shd we;s jevigykska isoqjk ldrAhhka flgsfhka i`oyka l,fyd;a:
wm switch-case statement tlla u.ska" MediaFunc function tlg pass lrkq ,nk w.h l=ulaoehs
ne,Suq' Pass lrkq ,enQ w.h 1 jQ jsg ls%hd;aul jSu i`oyd case 1 ;=, codes ,sjqfjuq' tu code tlska
isoqjQfha hus wjia:djl file tlla play fjuska fyda pause jS fkdue;skus jevigyk mdjspsps lrkakdg
109
jevigykg kj file tlla we;=,a lsrSug bv ,nd oSu;a" file tl play lsrSu i`oyd wjYH command tl
ieliSu;a" Play variable tflys w.h true lsrSu;ah' kuq;a we;af;kau tu command tl ,ndf.k file
tl play lrkq ,nkafka switch-case block tlg msg;ska we;s mciSendString function tl u.sks' fuu
function tlg call lsrSfusoS function tl ksis mrsos ls%hd;aul fkdjqkfyd;a isoql, hq;= foh wm if
statement tlla u.ska ;SrKh lf,uq'
fu;eka isg wm isoql, hq;= foa kus case 2 iy case 3 i`oyd wjYH fla;hka ,sjSuh' case 2 ;=, ,sjsh
hq;af;a media file tlla stop lsrSu i`oyd jk fla;hka jk w;r" case 3 ;=, media file tlla pause
lsrSug wod, fla;hka ,sjsh hq;=h' fuu kj fla;hka tla l, jsg Media Player jevigyk my; mrsos
osiafjkq we;' w,q;ska tla lr we;s fla;hka ;o l,q wl=rska oelafjs:
=============================================================
#include "MediaPlayer.h"
void Player::MediaFunc(int x){
switch(x){
case 1:
// if paused or playing, no menu
if(Pause!=true && Play!=true){
cout<< "Enter the path: " << endl;
cin>> pathSrc;
wsprintf(mediaCommand, "play %s", pathSrc);
cout<<"\nPlaying..."<<endl;
}else{
if(Pause){
cout<<"\nResumed... "<<endl;
wsprintf(mediaCommand, "play %s", pathSrc);
}
}
Pause = false;
Stop = false;
Play = true;
break;
case 2:
if(!Stop){
wsprintf(mediaCommand, "stop %s", pathSrc);
}
cout<<"\nStoped... "<<endl;
Pause = false;
Stop = true;
Play = false;
break;
case 3:
if(!Pause){
wsprintf(mediaCommand, "pause %s", pathSrc);
cout<<"\nPaused... (a- resume/s>a- New file)\n"<<endl;
}
Pause = true;
Stop = false;
Play = false;
break;
}
if(mciSendString(mediaCommand, NULL, 0, NULL)!=0){
system("cls");
MessageBox(NULL,"Sorry, can't play your file.","HALT!",0);
Pause = false;
Stop = false;
Play = false;
}
}
110
=============================================================
m,uqj isoqlr we;af;a case 1 ys if fj; else statement tlla tl;= lsrSuh' fuh isoqlr we;af;a Media
Player tl file tlla play lruska fyda pause lr we;skus ^Play fyda Pause ys w.h true kus& tu
wjia:dj y|qkd.ekSu i`oydh' tu else ;=, uS,.g" if statement tlla u.ska file tlla pause jS we;aoehs
^Pause ys w.h true oehs& mrSCId flfrA' fuh i;H kus if ;=, we;s cout u.ska “Resumed…” hkak
print lrkafka wm uS,.g isoqlrkafka pause jS we;s file tl h,s Odjkh lsrSu werTSu jk ksihs'
Resume lsrSug wjYH command string tl uS,S.g we;s wsprintf function tl u.ska ksmojhs' File tll
pause lsrSfuka miqj resume jSug mciSendString function tlg pass l, hq;= command tlo “Play
filename” hk format tlska u hq;=fjs' tu command tl u.ska audio / video file tl kej;=kq ;ek isg
Odjkh jSu werfTkQ we;' kuq;a fuysoS file tl we;af;a play fjuska kus we;=,a if tl false jSu ksid
by; i`oyka lsisjla isoqfkdjkq we;' wjidkfhaoS Play variable tflys w.h muKla true lr we;af;a
oekg file tlla play fjuska we;s nj jdrA:d lr ;enSugh'
case 2 ;=,oS isoqjkafka Media Player tl ;=, lsishus Media file tlla play fjuska fyda pause jS
we;skus muKla" th stop lsrSug wjYH command string tl wsprintf u.ska ksrAudKh jSuhs' tu string
tl fuu format tl .kq we;s nj Tng jegfya: “Stop fileName” (wsprintf u.ska fuysoS isoqlrkafka
“stop” hk string tl w.g pathSrc wuqKd th mediaCommand ;=, .nvd lsrSu fjs). B,.g th
“Stoped…” hk message tl print fjkq we;' bkamiq" l,hq;= mrsosu" oeka file tl stop jS we;s nj
jdrA:d lsrSug" Stop ys w.h muKla true lr we;'
case 3 statement tl ;=,oS" file tlla pause jS we;`oehs nef,k w;r" tfia ke;s kus ^pause jS we;s file
tlla kej; pause lsrSfus wrA:hla ke;s ksid& pause lsrSug wjYH command string tl fmr mrsosu
ksmojSu isoqfjhs' tys format tl: “pause fileName” jkq we;.
fyd`ohs' oeka wm fuu jevigyk ksujSfus wjika wOshrhg meusK we;af;uq' Tn jsiska oeka l, hq;=j
we;af;a tla fohls' tkus" fuu jevigyk Odjkh lsrSug w;HjYH jk main function tl ,sjSuh'
main function tflysoS wm isoq lrkafka" Media Player jevigyk ls%hd;aul lrjkakdf.ka wod, input
tl ,nd .ekSuh' fuu input tl u.ska Media Player jevigyk u.ska isoql, hq;= foh ;SrKh jkq
we;' fuysoS input tl f,i ls%hdlrjkakd jsiska jevigykg iemhsh hq;af;a character tlls' tu
characters wm my; mrsos Ndjs;d lruq:
•
•
•
•
‘a’ character tl jevigykg we;=,a lrkq ,enqjfyd;a Media Player jevigyk jsiska media
file tlla play lsrSug wod, ls%hdudrA.h .; hq;=h' fuu ls%hdudrA.h kus MediaFunc function
tlg 1 hk integer w.h pass lsrSu jkq we;'
‘s’ character tl jevigykg we;=,a lrkq ,enqjfyd;a Media Player jevigyk jsiska media
file tlla stop lsrSug wod, ls%hdudrA.h .; hq;=h' fuu ls%hdudrA.h kus MediaFunc function
tlg 2 hk integer w.h pass lsrSu jkq we;'
‘d’ character tl jevigykg we;=,a lrkq ,enqjfyd;a Media Player jevigyk jsiska media
file tlla pause lsrSug wod, ls%hdudrA.h .; hq;=h' fuu ls%hdudrA.h kus MediaFunc function
tlg 3 hk integer w.h pass lsrSu jkq we;'
‘ f ’ character tl jevigykg we;=,a lrkq ,enqjfyd;a Media Player jevigyk ksujsh hq;=h'
oeka wm fuu main function tl we;=,;a lrk ,o iusmQrAK" ls%hdldrS Media Player mrs.Kl
jevigyk krTuq:
=============================================================
#include "MediaPlayer.h"
void Player::MediaFunc(int x){
switch(x){
case 1:
// if paused or playing, no menu
111
if(Pause!=true && Play!=true){
cout<< "Enter the path: " << endl;
cin>> pathSrc;
wsprintf(mediaCommand, "play %s", pathSrc);
cout<<"\nPlaying..."<<endl;
}else{
if(Pause){
cout<<"\nResumed... "<<endl;
wsprintf(mediaCommand, "play %s", pathSrc);
}
}
Pause = false;
Stop = false;
Play = true;
break;
case 2:
if(!Stop){
wsprintf(mediaCommand, "stop %s", pathSrc);
}
cout<<"\nStoped... "<<endl;
Pause = false;
Stop = true;
Play = false;
break;
case 3:
if(!Pause){
wsprintf(mediaCommand, "pause %s", pathSrc);
cout<<"\nPaused... (a- resume/s>a- New file)\n"<<endl;
}
Pause = true;
Stop = false;
Play = false;
break;
}
if(mciSendString(mediaCommand, NULL, 0, NULL)!=0){
system("cls");
MessageBox(NULL,"Sorry, can't play your file.","HALT!",0);
Pause = false;
Stop = false;
Play = false;
}
}
void main(){
char c;
Player pl;
while(c!='f'){
cout<<"\nWhat you wanna do? \na - Play.\ns - Stop.\nd –
Pause.\nf - quit.\n"<<endl;
c=getch();
if(c=='a'){ // play
pl.MediaFunc(1);
}else if(c=='s'){ // stop
pl.MediaFunc(2);
}else if(c=='d'){ // pause
pl.MediaFunc(3);
}
}
cout<<"\nBye!\n"<<endl;
exit(0);
}
============================================================
112
Main ys isoqjkafka [email protected] m,uqfjka tys char c variable tl declare lr we;' fuu variable tl wm
mdjspsps lrkafka jevigyka ls%hdlref.ka ,nd.kakd input character tl .nvd lsrSugh' bkamiq
isoqjkafka pl kuska Player class tflys instance tlla fyj;a Player object tlla iEoSuhs ^Objects,
classes, instances wdosh ms,sn`oj wm “Object Oriented Programming” mrspsfpsofhaoS i,ld ne,qfjuq&'
jevigyk wjika lsrSug kus ‘ f ’ character tl jevigykg we;=,a l, hq;= nj by; i`oyka jsKs'
tfukau c variable tflys wka;rA.; jkafka jevigykg we;=,a lrk ,o character tl njo by;
oelaajsKs' tfiakus jevigyk wjika lsrSug" th mdjspsps lrkakdg wjYH oehs oek.ekSug l, hq;=
jkafka ksrka;rfhkau fuu c ys w.h mrSCIdjg ,la lsrSuhs' fuh main ;=, isoqjkafka while loop
tlla u.sks' Tng code tflys oelsh yels mrsos while u.ska tys iEu loop tlloSu c ys w.h ‘ f ’ oehs
mrSCId flfrA' w.h ‘ f ’ fkdfjs kus loop tl fkdkej;S ls%hd;aul jkq we;s w;r w.h ‘ f ’ fjs kus
loop tl ns`os my,ska we;s cout command tl ls%hd;aul jS “Bye!” hkak print lruska jevigyk
wjika jk nj ls%hdlreg okajkq ,efns' ta iu.u execute jk exit() function tl u.ska jevigyk
wjika lrkq we;' exit function tl u.ska b;d ir, f,i mrs.Kl jevigyklska bj;a jSug Tng
wjia:dj ,efns' fuysoS thg pass lr we;af;a 0 w.h nj Tng fmfka' fuu w.fhka oelafjkafka
jevigyk ;=, exit function tl mdjspspshg f.k we;s wjia:d .Kkhs' tkus" Tn fuu jevigyfkys
;j;a ia:dkhloS exit function tl fj; call lrkjd kus tysoS thg pass l, hq;af;a 1 w.hhs' ;j;a
;ekloS call lrkjd kus pass l, hq;= w.h 2 hs' fuf,i call lrkq ,nk wjia:d .Kk iudkj pass
l,hq;= w.ho by, hhs'
c ys w.h ‘ f ‘ fkdjSfuka loop tl ls%hd;aul jkafka kus" m,uqj isoqjkafka jevigyfkys m%Odk
menu tl osiajSuh ^rEmh10'1&' fuu menu tlska jevigyk mdjspsps lrkakdg olajkq ,nkafka Media
Player jevigyk ls%hd;aul lrjsh hq;= wdldrhhs' bkamiqj yuqjkafka getch() function tlh' fuu
function tl u.ska isoqlrkafka console window tl yryd Tnf.ka lsishus input tlla ,nd .ekSuh'
tf,i ,nd.kakd input data tl" getch function tl u.ska wdmiq return lrhs' Code tflys isoqlr
we;af;a fuu return lrkq ,nk o;a;h c variable tl ;=,g ,nd .ekSuh' uS,.g isoqjkafka if-else if
statements lSmhla u.ska tu ,nd.kakd ,o o;a;h mrSCId lsrSuhs' m,uqfjkau we;s if u.ska c ys w.h ‘
a ’ fjsoehs mrSCId flfrk w;r th i;H kus pl object tl mdjspsps lsrSfuka MediaFunc function
tlg 1 w.h pass lruska call flfrkq we;' ta wkqj fuysoS MediaFunc function tl ls%hd;aul jSfuka
by; meyeos,s lrkq ,enQ mrsos" jevigyk mdjspsps lrkakdf.ka media file tlla ,ndf.k th play
lrkq we;' bkamiq we;s else if statements hq.,ska ms,sfj,ska ‘s’ yd ‘d’ w.hka i`oyd c mrSCIdjg ,la
flfrk w;r ms,sfj,ska 2 yd 3 pass lruska MediaFunc fj; call flfrA' ta wkqj ms,sfj,ska media
file tl stop fyda pause fjkq we;'
Appendix
Visual C++ 6.0 ia:dmkh (Install) lsrSu
113
my; oelafjk wdldrhg Visual C++ 6.0 install lrkak:
•
Tn us,oS Visual C++ 6.0 ia:dmkh lsrSfus ixhqla; ;egsh (CD) Tfns mrs.Klfha ixhqla; ;egs
Odjlhg (CD driver) we;=,a lrkak' tjsg u`o fjs,djlska 11'1 rEmfha oelafjk dialog box tl
iajhxls%hj jsjD; jkq we;'
(?mh 11'1)
• by; Dialog box tl iajhxls%hj jsjD; jQfha ke;skus ixhqla; ;egshg msjsiS tys we;s Setup.exe
keue;s file tl ls%hd;aul lrjkak ^11'2 ?mfha oelafjkafka tu file tlhs&' tjsg by; Dialog box tl
jsjD; fjs'
•
(?mh 11'2)
?mh 1'1 dialog box tflys “Next” button tl click lrkak' tjsg ?mh 11'3 ys oelafjk dialog box tl
jsjD; fjs'
•
(?mh 11'3)
fuys wka;rA.; “I accept the agreement” keue;s radio button tl select fldg Next button tl
click lrkak' tjsg u;=jkafka ?mh 11'4 ys oelafjk dialog box tlhs'
114
(?mh 11'4)
•
by; dialog box tflys by,skau we;s text box tl ;=,g Tfns CD Key tl we;=,a l, hq;=h' fuh
fndfydajsg Tn us,oS .;a Visual C++ ixhqla; ;egs ljrfha i`oykaj ;sfnkq we;'
•
Bg my;ska we;s text box tlg Tfns ku we;=,a lrkak'
•
oeka Next button tl click lrkak' tjsg ?mh 11'5 ys oelafjk dialog box tl jsjD; jkq we;'
(?mh 11'5)
•
•
fuys we;s “Products” keue;s option tl select fldg Next button tl select lrkak' tjsg ?mh 11'6 ys
oelafjk dialog box tl jsjD; fjs'
(?mh 11'6)
fuys Next button tl click lrkak' tjsg osiafjkq we;af;a ?mh 11'7 ys oelafjk dialog box tlhs' tys
“Visual C++ Enterprise Edition” keue;s check box tl select lr th Next lrkak'
115
(?mh 11'7)
•
tjsg 11'8 ys oelafjk dialog box tl jsjD; fjs'
•
(?mh 11'8)
fuys Continue button tl click lrkak' tjsg u;=fjk" ?mh 11'9 ys oelafjk Message box tl OK
lrkak'
•
(?mh 11'9)
tjsg ?mh 11'10 ys osiafjk dialog box tl jsjD; jkq we;'
116
•
•
•
(?mh 11'10)
Tng wjYH kus fuys options hgf;a we;s check boxes j,ska Microsoft Visual C++ 6.0, ActiveX iy
Data Access hk tajd yer wfkla check boxes ish,a, clear lr oeuqjdg jrola jkafka ke;'
wjidkfhaoS Continue button tl click lrkak' fujsg ?mh 11'11 oelafjk dialog box tl osiajqjfyd;a
th OK lrkak'
(?mh 11'11)
tjsg ?mh 11'12 ys oelafjk ;srh osiajS Visual C++ ia:dmkh jSu werfTkq we;'
(?mh 11'12)
Tfns mrs.Klh i;= fjs.h wkqj fuu ia:dmkh jSug .kakd ld,h fjkia fjkq we;' ia:dmkh ksujQ
miq u;=fjk message boxes ish,a, OK l, miq ?mh 11'13 ys oelafjk dialog box tl jsjD; fjs' th
Next lrkak'
117
•
(?mh 11'13)
tjsg osiajk" ?mh 11'14 ys oelafjk dialog box tflys Register now keue;s check box tl uncheck
lr “Finish” button tl click lrkak'
(?mh 11'14)
Tn oeka Visual C++ ia:dmkh lr wjidkhs' oeka Tng th Desktop tl u;ska fyda Start menu tflys
“All Programs > Microsoft Visual Studio 6.0 > Microsoft Visual C++ 6.0” hk shortcut tl u.ska
ls%hd;aul l, yels jkq we;'
Debug lsrSu iy Errors y`oqkd.ekSu
Program tlla debug lsrSu hkqfjka ye`oskafjkafka lsishus mrs.Kl jevigykla ls%hd;aul
jk wdldrh ksrSCIKh lsrSug yd tys wvx.= fodaY y|qkd.ekSug Ndjs;d lrk l%ufjsohhs' fuu
debugging myiqlu Tng imhkq ,nkafka debugger uDoqldx.h u.sks' Visual C++ ;=, debugger
software tlla wka;rA.;hs' Visual C++ ys we;s fuu myiqlu m%fhdackhg .ekSug kus compile jSu
isoqjk" programming errors fkdue;s mrs.Kl jevigykla wka;rA.; jk Visual C++ project tlla
;snsh hq;=h'
jevigykla debug lsrSug kus m%:ufhka isoql, hq;af;a Visual C++ project tlla load lr .ekSuhs'
bkamiq Visual C++ ;=, we;s Debug window tl jsjD; l, hq;=h' th jsjD; lsrSug" Visual C++ ys
menu bar tl u; right click lr" u;=fjk pop-up menu tlska “debug” keue;s whs;uh select lrkak'
?mh 12'1 ys oelafjkafka fuu debug window tlhs'
(?mh 12'1)
118
oeka fuu debug window tflys we;s step into iy step over buttons mdjspsps lsrSfuka Tng mrs.Kl
jevigykla ls%hd;aul jk wdldrh kerTsh yelsh' Tn step into fyda step over button tl tnQ jsg
Visual C++ ;=, Program execution pointer tl kuska ye`oskafjk Wmdx.h u;=fjkq we;' 12'2 rEmfha
ly meye;s B;,hla f,i oelafjkafka program execution pointer tlhs'
(?mh 12'2)
lsishus fudfyd;loS jevigykl ls%hdldrS ,CIh olajkafka fuu program execution pointer tl
u.skqhs' Tng step into button tl u.ska" function call tlla we;s ia:dkhl isg tu function tl
wNHka;rhg .uka lr" tu function tl ;=, we;s codes ls%hdldrS jk wdldrh oel.; yels w;r" step
over button tl u.ska function tl ;=,g .uka fkdlr thg Wvska mek" l%uf,aLKhl B,. fldgi
ls%hdldrS jkq kerTsh yelsh' by; oelafjk B;,h u.ska program tll Tn isgsk ia:dkh yeujsgu
oek.; yel' fuh f;reus .ekSug kus Tn l,hq;af;a jevigykla debug lr ne,Suhs'
fus i`oyd 10 mrspsfpsofhaoS wm i,ld ne,Q mrs.Kl jevigyk Tng fhdod .; yel' fus i`oyd m,uqj"
Tn iEoQ Media Player project tl Visual C++ ;=, jsjD; lr .kak' oeka" debug window tflys we;s
step over button tl fyda tys keyboard shortcut tl jk F10 h;=r Tnkak' tjsg jevigyfkys
l%shdldrS ,CIh fmkajk B;,h" jevigyfkys main function tl ;=, u;=jS fmfkkq we;' oeka"
l%uf,aLKfhys pl.MediaFunc(1); hk fla;h bosrshg B;,h meusfKk f;la F10 Tnkak' oeka"
MediaFunc function tlg we;=,q jSu msKsi" step in button tl fyda F11 h;=r fyda Tnkak' tjsg B;,h
main ;=,ska bj;aj" MediaFunc function tl ;=,g .uka lrkq Tng oel.; yels jkq we;' uS,S.g
MediaFunc ys wjidkh jk f;lau" F10 u.ska B;,h .uka lrjkak' Function tl wjidkh f;la
.uka lsrSfuka wk;=rej B;,h kej;;a main ;=,g we;=,a jkq we;' fus wdldrfhka" Tng
jevigykla ls%hd;aul jk wdldrh th debug lsrSu u.ska oel.; yelsh'
mrs.Kl jevigyka j, fodaY - Programming errors
my; oelafjkafka fuu fmdf;ys uq,a Nd.fha we;s “Tfns m,uq C++ mrs.Kl jevigyk” keue;s
fldgfia wvx.= jk jevigyku fodaY iys;j ,shd we;s wdldrhhs:
==============================================
#include<iostream.h>
void main()
{
<< "Hello beautiful world!" << endl;
}
===============================================
by; jevigyk foi ne,qjfyd;a tys cout command tl fkdue;s nj Tn olskq we;s' fuh error
tlls' fuu jevigyk VC++ ;=, type lr compile lsrSug W;aidy .;fyd;a" output window tl
u.ska tu fodaYh fmkajd fokq ,nk nj Tn olSsjs' Tng output window tl;=, my; oelafjk
wdldrfha output tllaa ,efnkq we;'
119
(?mh 12'3)
Output window tflys f;jk fm,ska ^hello.cpp hkakg miqj we;s fma,sfhka& fuu error tl olajkq
,efns' tu f;jk fma,sh u; double click lr isoqjkafka l=ulaoehs n,kak' tjsg VC++ ys editor
window tl;=, tu error tl ksrAudKh l, fla;h highlight jk nj Tn olskq we;' fus whqrska"
jevigykl lsishus fodaYhla we;skus th y`oqkdf.k th ksjeros l, yelsh' Tn l,hq;af;a"
^1& Output window tflys Tng wjYH error tl olajk fmf,ys ioyka jkafka l=ulaoehs mrSCId lr
n,kak'
fodaY iys; mrs.Kl fla;hla ksjeros lsrSug kus Tn m,uqj output window tflys error tl u.ska
lshfjkafka l=ulaoehs jgyd.; hq;=h' ulaksidoh;a Tn .eg,qjlg ms,;=rla fijSug m,uqj .eg,qj
yrsyegs jgyd.; hq;= fyhsks' .eg,qj l=ulaoehs y`oqkd.;a miq Tng fodaY iys; fla;h ksjeros l,
yel'
Output window tflys error tl ioyd fya;=jqKq lreK ioykajS ;sfnkq we;' we;eusjsg tu
jsia;rfhys error tl l=ulaoehs Rcqju ioyka fkdfjs' we;eusjsg i`oyka jkq we;af;a tu error tl
ksid u;=jqKq fjk;a jrola ms,snojh' WodyrKhla f,i by; WodyrKfha output window tflys we;s
error tl ioyka j.ka;sfhka lshfjkafka jevigyfkys “<<“ ,l=Kg uq,ska “;” i,l=K fkdue;s
njh' ienE jro jkafka cout command tl fkdue;s jSu nj tys i`oyka fkdfjs' kuq;a tys i`oyka
foh we;sjQfha tys cout jsOdkh fkdue;s jSu ksidh' tysoS Rcqju error tl l=ulaoehs ioyka fkdjqKs'
iudkH f,iu Tng ;ju output window tl foi n,d lsishus error tlla ksjerosj y`oqkd.ekSug
fkdyelsjkq we;s' fuu yelshdj Tn fj; meusfKkafka mrs.Kl jevigyka ,sjSfuka yd compile
lsrSfuka Tn ,nk woaoelSus u;h' fusksid oekg compiler errors jgyd .ekSu ms,snoj jevsmqr lror
fkdjkak' yels;rus mrs.Kl jevigyka ,shuska woaoelSus tl;= lr.ekSug W;aidy lrkak' tjsg
fuu yelshdjka bfnsu Tn lrd meusfKkq we;'
^2& Error tlu; double click lruska error tl ksmojQ fla;h l=ulaoehs y`oqkd.kak'
fodaYh l=ula oehs oek.;a miq Tng tu fodaY iys; ia:dkh fj; .uka l, yelsh' tfia lr cout
jsOdkh ksis;ek type lr jevigyk ksjeros l,yelsh'
120
--------------------------------------------------------------------
fuu fmdf;ys wvx.= lreKq ms,sn`oj Tnf.a woyia we;akus" tfia;a ke;skus fmdf;ys
wvx.= jQ lreKq ms,sn`oj Tng wmyeos,s;djka" .eg,q we;akus" lreKdlr my;
oelafjk B-fus,a ,smskhg Tfns woyia" .eg,q fhduqlrkak:
[email protected]
--------------------------------------------------------------------
121
Download