You are on page 1of 70

OBJECT-

ORI
ENTEDPROGRAMMI
NGLAB
OBJECTI VE:
•To st rengthenthei
rproblem sol
vi
ng abi
l
itybyappl
yi
ng t
hechar
act
eri
sti
csofan
objectori
ented
approach.
•Toi ntr
oduceobjectori
ent
edconceptsi
nC++andJava.
Programmi ng:

Exer
cise–1( Basi
cs)
Writ
eaSi mpleProgr
am onpr
int
ing“
Hel
l
oWor
ld”and“
Hel
l
oName”wher
enamei
sthe
i
nputfrom t
heuser

Pr
ogr
am:
#include<iostream>
#include<conio.h>
usingnamespacest d;
mai n()
{
charst[50]
;
cout<<"\nent
ert
hegi
venst
ri
ng "
;
gets(st
);

cout<<"\
nt hegi
venst
ri
ngi
s "
;
cout<<st;
getch()
;
}

Out
put
:
a)Conv
ertanyt
wopr
ogr
amst
hatar
ewr
it
teni
nCi
ntoC++

Pr
ogr
am i
nC[
Fibonacci
ser
ies]
:

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

mai
n()

I
nta,
b,
c,
I;

Pr
int
f(“
%d%d”
,a,
b);

whi
l
e(i
<10)

C=a+b;

pr
int
f(“
%d”
,c)
;

a=b;

b=c;

i
++;

get
ch(
);

Out
put
:
Pr
ogr
am i
nC++:
#i
ncl
ude<i
ost
ream>

#i
ncl
ude<coni
o.h>

usi
ngnamespacest
d;

mai
n()

i
nta,
b,
i,
c;

a=0;
b=1;

cout
<<"
thef
ibonacci
ser
iesi
s\n"
;

cout
<<a<<endl
;

cout
<<b<<endl
;

whi
l
e(i
<15)

c=a+b;

cout
<<c<<endl
;

a=b;

b=c;
i
++;

get
ch(
);

Out
put
:

Gi
vennoi
sPr
imeornot

Pr
ogr
am i
nC:
#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

mai
n()

i
ntn,
c=0,
i
;

pr
int
f("
\nent
ert
henumber
");
scanf
(“%d”
,&n)
;

f
or(
i=1;
i
<=n;
i
++)

i
f(
n%i
==0)

c=c+1;

i
f(
c==2)

Pr
int
f("
\nt
hegi
vennoi
spr
ime"
);

el
se

Pr
int
f("
\nt
hegi
vennoi
snotapr
ime"
);

get
ch(
);

Pr
ogr
am i
nC++:
#i
ncl
ude<i
ost
ream>

#i
ncl
ude<coni
o.h>

usi
ngnamespacest
d;

mai
n()

i
ntn,
c=0,
i
;

cout
<<"
\nent
ert
henumber
";

ci
n>>n;
f
or(
i=1;
i
<=n;
i
++)

i
f(
n%i
==0)

c=c+1;

i
f(
c==2)

cout
<<"
\nt
hegi
vennoi
spr
ime"
;

el
se

cout
<<"
\nt
hegi
vennoi
snotapr
ime"
;

get
ch(
);

Out
put
:

b)Wr
it
eadescr
ipt
ionofusi
ngg++(
150Wor
ds)

Tostartwit
h,yourun 
g++ 
simpl
ybyt ypi
ng 
g++ 
atacommandpr ompt( f
orMacOSX
thatnor
mallyinv
olv
esstar
ti
ngTerminal.
appandonLi nuxyoucanst artupasimi
l
ar
termi
nalpr
ogram,GnomeandKDEbot hprov
ideone).Whenyourun 
g++ onit
sownwi
th
noargumentsyoushoul
dgetsomet
hingsimil
art
ot hi
sasaresponse:

g++:
noi
nputf
il
es
Toact ual
lycompil
eaC++f il
e,youneedt ocall
 
g++ withthefi
lenamecont ainingtheC++
code.Soi fyourcodeiscontai
nedinaf il
ecalledmain.cpp,t
ocompi leit
,justt y
pe'g++
main.cpp'.Assumingyouhav enoer rors,thi
swi l
lpr oduceanexecut ablef i
lecall
ed
'
\1 (
onLi nuxandMacOSX) .Toruny ourresul
t i
ngexecutabl
e,justt
ype'./a.out'
.Thi
sis
aboutt hemostbasicthingyoucandot oproduceanexecut ablefr
om y ourC++code.
Howev er,
 g++ i
sactual
l
yav er
ypower fuloptimizi
ngcompi lerandpr ovidesy ouwith
manypossi bleopt
ions.

Thefir
stoptiontolookatist he-oswi
tchthat 
g++ provides.Intheexampl eabov e,the
compil
erextractedthecodef rom y
ourfil
eandcr eatedanew f i
lecalled'a.out'
.Ifyou
wanttooverridethi
sdefaultnameandinsteadcalltheout putexecutablefil
emai n,you
wouldexecutethefoll
owingcommand' g++mai n.
cpp- omai n'
.When  g++ f
ini
shes, i
twil
l
havecreatedanout putfil
ecalled'
main'i
nsteadof' a.out'andtor uni t
,youj usttype
'
./mai
n'.

Exer
cise–2(
Expr
essi
onsCont
rol
Flow)

a)WriteaProgram thatcomput esthesimpleinterestandcompoundi nt


erestpayable
onprinci
palamount( i
nRs.)ofloanbor r
owedbyt hecust omerfrom abankforagi v
en
per
iodoftime(i
ny ears)atspecifi
crateofint
erest.Furt
herdeter
minewhetherthebank
wil
lbenefi
tbychar
gingsimpl ei
nterestorcompoundi nter
est.

Pr
ogr
am:
#i
ncl
ude<i
ost
ream>
#i
ncl
ude<mat
h.h>
#i
ncl
ude<coni
o.h>
usi
ngnamespacest
d;
mai
n()
{
f
loatp,
r,t
,si
,ci
;
cout
<<"
\nEnt
ert
hev
alueofPr
inci
ple,
Rat
e,Ti
me:
";
ci
n>>p>>r
>>t
;
si
=(p*
r*t
)/100;
ci
=p*
pow(
(1+r
/100)
,t
)-
p;
cout
<<"
\nSi
mpl
eint
eresti
s"<<si
;
cout
<<"
\nCompoundi
nter
esti
s"<<ci
;
i
f(
si<ci
)
{
cout
<<"
\nbanki
sbenef
it
edbyCOMPOUNDI
NTEREST"
;
}
el
se
{
cout
<<"
\nbanki
sbenef
it
edbySI
MPLEI
NTEREST"
;

}
get
ch(
);
}

Out
put
:

b)Wr i
teaPr ogram tocal
culatet
hefarefort
hepassengerstravel
i
nginabus.Whena
Passengerent ersthebus,t heconduct
orasks“ Whatdist
ancewi l
lyoutrav
el?
”On
knowingdistancefr
om passenger(asanapproxi
mateint
eger)
, t
heconduct
ormenti
ons
thefar
etot hepassengeraccordi
ngtofol
lowi
ngcri
ter
ia.

Pr
ogr
am:

#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<coni
o.h>
#i
ncl
ude<math.
h>

main()
{
intpdist1,
pdist2,pdi st
3;
floatkm, f
are1,f
ar e2,fare3;
cout<<"enterper son1, person2,person3di
st"
;
cin>>pdist1>>pdist 2>>pdist3;
km=1. 50;
fare1=km* pdist1;
cout<<"fareofper son1i s:"
<<fare1<<endl
;
fare2=km* pdist2;
cout<<"fareofper son2i s:"
<<fare2<<endl
;
fare3=km* pdist3;
cout<<"fareofper son3i s:"
<<fare3<<endl
;
getch();
}

Out
put
:

Exerci
se–3(
Vari
abl
es,
Scope,
All
ocat
ion)
a)Wr i
teapr
ogr
am toimpl
ementcal
lbyv al
ueandcal
lbyr
efer
enceusi
ngr
efer
ence
vari
abl
e.

Pr
ogr
am:
[Cal
lbyv
alue]

#incl
ude<i
ostr
eam.h>
#incl
ude<coni
o.h>
voidswap(i
nta,
intb)
{
inttemp;
temp=a;
a=b;
b=temp;
cout<<"Aft
erswapingt
hev al
uesare"<<endl;
cout<<"Thevalueofai
s"<<a<<endl
;
cout<<"Thevalueofbi
s"<<b<<endl
;
}
main()
{
inta,
b;
a=50,b=100;
cout<<"Befor
eswapingthevaluesare"<<endl
;
cout<<"Thevalueofai
s"<<a<<endl
;
cout<<"Thevalueofbi
s"<<b<<endl
;
swap( a,
b);
getch();
}

Out
put
:

Pr
ogram:[
Call
byRefer
ence]
#i
ncl
ude<i
ostream.
h>
#i
ncl
ude<coni
o.h>

v
oidswap(i
nt*a,i
nt*
b)
{
i
nttemp;
temp=*a;
*a=*
b;
*b=t
emp;
cout<<"
Aft
erswappingtheval
uesar
e"<<endl
;
cout<<"
Thevalueofais"
<<*
a<<endl
;
cout<<"Theval
ueofbis"
<<*b<<endl
;
}
main()
{
inta,
b;
a=75,b=50;
cout<<"Bef
oreswappi
ngthev al
uesare"
<<endl
;
cout<<"Theval
ueofais"
<<a<<endl;
cout<<"Theval
ueofbis"
<<b<<endl;
swap( &a,
&b);
getch();
}

Out
put
:

b)Wr
it
eapr ogr
am toi
l
lust
rat
escoper
esol
uti
on,new anddel
eteOper
ator
s.(
Dynami
c
MemoryAl
l
ocati
on)

Scoperesolut
ion:
#incl
ude<i
ostream.h>
#incl
ude<coni
o.h>

i
nta=5;
cl
assdemo
{
publ
ic:
stat
ici
ntb;
};
i
ntdemo:
:
b=10;
main(
)
{
intc;
c=10;

cout<<"
\nGlobalvar
iableis"<<:
:
a;
cout<<"
\nStati
cvari
ableis"<<demo:
:
b;
cout<<"
\nLocalvari
ableis"<<c;
getch()
;
}

Out
put
:

Dy
nami
cOper
ator
:

#include<i ostream.h>
#include<coni o.h>
main( )
{
intn,* pointer,
c;
cout <<"\ nEnternumberofelement
s:"
;
cin>>n;
point er=newi nt
[n];
cout <<"\ nEnterelements:
";
for(c=0; c<n;c++)
cin>>poi nter[
c];
cout <<"\ nElement sent
eredbyy
ouare:"
;
for
(c=0;c<n;
c++)
cout<<point
er[
c]<<endl
;
del
ete[]
pointer
;
getch()
;
}

Out
put
:

c)Wr
it
eapr
ogr
am t
oil
l
ust
rat
eSt
oragecl
asses

Pr
ogr
am:

#include<i ostr
eam. h>
#include<coni o.h>
#include" Exter
nexampl e. cpp"
externi nttest;
voidf un( )
{
stat i
ci ntcount;
count ++;
cout <<" Functi
oncal l
ed" <<count<<"t
imes";
}
mai n()
{
aut oi nta=0;
cout <<" \nTheautov ariablev al
ueis:
"<<a;
regist erintb=0;
cout <<" \nTheregisterv ari
ablevalueis:
"<<b;
cout <<" \nThestaticvariableis:"
;
fun( )
;
cout <<" \nTheexternv ar i
ableis:
";
mul tiply(10);
cout <<t est;
get ch( )
;
}
Ext
ernexampl
e.cpp

#i
ncl
ude<i
ostr
eam.h>
#i
ncl
ude<coni
o.h>

i
nttest=100;
v
oidmul ti
ply
(intn)
{
test=t
est*n;
getch();
}

Out
put
:

d)Wr
it
eapr
ogr
am t
oil
l
ust
rat
eEnumer
ati
ons

Program:
#include<iost
ream.
h>
#include<conio.
h>
enum shape
{
Ci r
cle,
Rect angle,
Squar e,
};
main()
{
intcode, r,l
,
b,a;
cout<<" Entert heshapecode" ;
cin>>code;
while((code>=Ci rcl
e)&&( code<=Squar e)
)
{
switch(code)
{
case0: cout <<"\n**
Circle**";
cout <<"\ nEnterradius:";
cin>>r ;
cout <<"\ nAreaofCi rcleis:"
<<3.14* r
*r;
break;
case1: cout <<"\n**
Rect angle**";
cout <<"\ nEnterl
engt h,breadth"<<endl;
cin>>l >>b;
cout <<"\ nAreaofr ectangleis:"<<l*
b;
break;
case2: cout <<"\n**
Squar e* *
";
cout <<"\ nEnterav alue"<<endl;
cin>>a;
cout <<"\ nTheareaofsquar eis:"
<<a*a;
break;
}
cout<<"\ nEnt ershapecode" ;
ci
n>>code;
}
cout<<" \nOutofchoi ce";
getch();
}

Out
put
:
Exer
cises–4( Funct
ions)
Writ
eapr ogr
am ill
ustr
atingInl
i
neFuncti
ons
a)Wr i
teapr ogram il
lustr
atefunct
ionover
loadi
ng.Wr
it
e2ov
erl
oadi
ngf
unct
ionsf
or
power.

Pr
ogr
am:

#incl
ude<iost
ream>
#incl
ude<conio.
h>
usingnamespacestd;
#incl
ude<math.h>
cl
assdemo
{
i
ntc;;
f
loatb,
d;

publ
ic:
voi
dpower (
inta,
i
ntb)
{
c=pow( a,
b);
cout<<"\nthepowerofi
ntegeri
s"<<c;
}
voi
dpower(floata)
{
b=3.0;
d=pow( a,
b);
cout<<"\nthepoweroff
loatis"
<<d;
}
}
;
mai
n()
{
demod;
d.power(
2,4)
;
d.power(
2.0)
;
getch(
);
}

Out
put
:

b)Wr
it
eapr
ogr
am i
l
lust
rat
etheuseofdef
aul
tar
gument
sforsi
mpl
eint
erestf
unct
ion.

#incl
ude<iostream>
#incl
ude<conio. h>
usingnamespacest d;
cl
asssi mplei
nt erest
{
charn[50] ,
add[
20]
;
i
ntp,t,
age, no,
si
;

publ
i
c:v
oidgetdat
a()
{
cout
<<"\
nent
ercust
omername";
ci
n>>n;
cout
<<"\
nent
ercust
omernum";
ci
n>>no;
cout
<<"\
nent
ercust
omeradd"
;
ci
n>>add;
}

v
oidcalcul
atesi
mpl ei
nt(
intp=2000,
i
ntt
=2,
fl
oatr
=8.
5)
{
si=(
p*t*r
)/100;
cout
<<"
\ncust
omernameis"<<n;
cout
<<"
\ncust
omernum i
s"<<no;
cout
<<"
\ncust
omeraddr
essis"<<add;
cout
<<"
\nsi
mplei
nter
esti
s"<<si;
}
}
;
mai
n()
{
simplei
nterests;
s.get
data()
;
s.cal
culat
esimplei
nt(
);
getch()
;

Out
put
:

Exer
cise-5(
Functi
ons–Exer
ciseConti
nued)
a)Writeaprogr
am toi
ll
ustr
atefunct
ionoverl
oadi
ng.Wr
it
e2ov
erl
oadi
ngf
unct
ionsf
or
addi
ngt wonumbers

Pr
ogr
am:

#incl
ude<iostream.h>
#incl
ude<coni o.
h>
#incl
ude<mat h.
h>
i
ntadd(inta,intb);
fl
oatadd(floatc,fl
oatd)
;
doubleadd( doublee,
doubl
ef)
;
main( )
{
inta,b,g;
floatc,d,h;
doubl ee, f
,i
;
cout <<"entera, bval
ues:";
cin>>a>>b;
g=add( a,
b);
cout <<g<<endl ;
cout <<"enterc,dv al
ues:";
cin>>c>>d;
h=add( c,
d);
cout <<h<<endl ;
cout <<"entere, fv
alues:"
;
cin>>e>>f ;
i=add( e,f)
;
cout <<i<<endl ;
getch( );
}
intadd( i
nta,intb)
{
i
ntg;
g=a+b;
returng;
}
floatadd( floatc,f
loatd)
{
fl
oath;
h=c+d;
returnh;
}
doubl eadd( doublee,doubl
ef)
{
doublei ;
i
=e+f ;
returni ;
}

Out
put
:
b)Wr
it
eapr
ogr
am i
l
lust
rat
efunct
iont
empl
atef
orpowerofanumber
.

Pr
ogr
am:

#incl
ude<iost
ream>
#incl
ude<conio.
h>
usingnamespacestd;
#incl
ude<math.h>
cl
assdemo
{
i
ntc;;
f
loatb,
d;

publ
ic:
voi
dpower (
inta,
i
ntb)
{
c=pow( a,
b);
cout<<"\nthepowerofi
ntegeri
s"<<c;
}
voi
dpower(floata)
{
b=3.0;
d=pow( a,
b);
cout<<"\nthepoweroff
loatis"
<<d;
}

}
;
mai
n()
{
demod;
d.power(
2,4)
;
d.power(
2.0)
;
getch(
);
}

Out
put
:

c)Wr
it
eapr
ogr
am t
oil
l
ust
rat
efunct
iont
empl
atef
orswappi
ngoft
wonumber
s.

Pr
ogr
am:

#include<iostream.h>
#include<coni o.
h>
templ ate<t
y penameT>
voidswap( Tn,Tp)
{
Tt emp;
cout <<"\nBeforeswappingtheval
uesare:
"<<n<<p;
temp=n;
n=p;
p=t emp;
cout <<"\nAfterswappingt
hev al
;
uesare:
"<<n<<p;
}
main( )
{
swap( 2,3)
;
getch( );
}

Out
put
:
Exerci
se-6(ClassesObj ect
s)
CreateaDistanceclasswi t
h:
 feetandi nchesasdat amember s
 memberf uncti
ont oinputdist
ance
 memberf uncti
ont ooutputdist
ance
 memberf uncti
ont oaddt wodistanceobj
ect
s
a).Writ
eamai nfuncti
ont ocreateobjectsofDI
STANCEcl
ass.I
nputt
wodi
stancesand
outputthesum.

#include<i ostr
eam. h>
#include<coni o.h>
classdi stance
{
floatf eet;
intinches;
publ i
c: voi
di nputdi
stance( )
{
cout <<"\nEnterthef eet:
";
cin>>f eet;
cout <<"\nenterinches:";
cin>>inches;
}
publ i
c: voi
dout putdistance( )
{
cout <<"\nFeet="<<"\t"
<<f eet;
cout <<"\nInches="<<"\t"
<<i nches;
}
publ i
c: voi
dadddi stance( distanceob1,
dist
anceob2)
{
i
nches=ob1. i
nches+ob2. i
nches;
feet=inches/ 12;
i
nches=i nches%12;
feet=ob1. feet+ob2.feet+f eet;
}
};
main()
{
dist
anceob1, ob2,
ob3;
cout<<"\nDistance1";
ob1.i
nput di
stance();
cout<<"\nDistance2";
ob2.i
nput di
stance();
ob3.adddistance(ob1,
ob2)
;
ob3.outputdistance(
);
getch()
;
}

Out
put
:

b).Wri
teaC++Progr
am t
oil
l
ust
rat
etheuseofConst
ruct
orsandDest
ruct
ors(
uset
he
aboveprogr
am.
)

Progr am:
#include<math.h>
#include<iost
ream>
usingnamespacest d;
cl
assDi st
ance
{
fl
oatf eet;
i
nti nch;
publ i
c:

v
oidadd(
Dist
ance,
Dist
ance)
;

Dist
ance()
{
cout<<"
Enterdi
stance(
feetandi
nches)
:"
;
ci
n>>f
eet
>>i
nch;
}

Distance( Distanced1,Dist
anced2)
{
i
nch=d1. i
nch+d2. i
nch;
feet=d1. f
eet +d2.f
eet;
i
f(inch>=12. 0)
{
feet=d1. f
eet +d2.f
eet+(i
nch/12.0)
;
i
nch=i nch%12;
}
cout <<feet;
cout <<endl<<inch;
}
~Di stance()
{
cout<<" \
ndest r
uctorexe";
}
};
mai n()
{
Distanced1, d2,
d3(d1,d2);

d1.
Dist
ance:
:
~Di
stance(
);

/
/d3.
add(d1,
d2);
/
/d3.
disp_di
st(
);
}

Out
put
:
c)Wri
teaprogram f
oril
l
ustrat
ingf
unct
ionov
erl
oadi
ngi
naddi
ngt
hedi
stancebet
ween
obj
ect
s(usetheaboveprobl
em)

Progr am:
#incl ude<i ostr
eam>
#incl ude<coni o. h>
usi ngnamespacest d;
classdi stance
{
floatf eet ;
inti nches;
publ i
c: voidread( )
{
cout <<"\nEntert hef eet:
";
cin>>f eet;
cout <<"\nEnteri nches:";
cin>>i nches;
}
publ i
c: voidread( distanceob1,di
stanceob2)
{
i
nches=ob1. i
nches+ob2. i
nches;
feet =inches/ 12;
i
nches=i nches%12;
feet =ob1. feet+ob2. feet+f
eet;
cout <<"\nFeet ="<<feet;
cout <<"\nInches=" <<inches;
}
};
mai n()
{
di stanceob1, ob2, ob3;
cout <<" \nDist
ance1" ;
ob1. read( );
cout <<" \nDist
ance2" ;
ob2. read( );
ob3. read( ob1,ob2) ;
get ch();
}
Out
put
:

d).Wri
teaC++pr
ogr
am demonst
rat
ingaBankAccountwi
thnecessar
ymet
hodsand
vari
abl
es

#include<iostream>
#include<conio. h>
usingnamespacest d;
classaccount
{
charename[ 30] ;
inteno;
publ i
c:intdep,wi thdr
aw, bal;
publ i
c:voidget account details()
{
cout <<"\
nEntercust omername: "
;
cin>>ename;
cout <<"\
nEntercust omernumber :
";
cin>>eno;
}
publ i
c:voiddispl ayaccount detail
s()
{
cout <<"\
nCust omernamei s:"
<<ename;
cout <<"\
nCust omernumberi s:
"<<eno;
}
publ i
c:voidbal ance()
{
bal=0;
cout <<"\nBalanceis: "
<<bal;
}
publ i
c: voidaccount deposit(
)
{
cout <<"\nEntertheamountt obedeposi
ted:
";
ci
n>>dep;
bal=bal +dep;
cout <<bal;
}
publ i
c: voidaccount withdraw()
{
cout <<"\nEntertheamountt obewit
hdraw:"
;
ci
n>>wi thdraw;
i
f(wit hdraw<bal )
{
bal=bal -
withdraw;
cout <<bal ;
}
el
se
cout <<"Accessdeni ed";
}
}
;
main( )
{
accountob1;
ob1. get accountdetails()
;
ob1. displayaccount detail
s();
ob1. balance( )
;
ob1. account deposit();
ob1. account withdraw( );
getch( )
;
}
Out
put
:

Exer
cise–7( Access)
Writ
eaprogram foril
l
ustr
ati
ngAccessSpeci
fi
erspubl
i
c,pr
ivat
e,pr
otect
ed
a)Writ
eaprogram implementi
ngFr
iendFunct
ion

#include<i ostream>
#include<coni o.
h>
usingnamespacest d;
classf rndfnexp
{
pr i
vat e:
charname[ 30] ;
pr i
vat e:
intacno;
pr i
vat e:
intbal ;
publ i
c: voi
dr eaddet ail
s()
{
cout <<"\nEnt ername: ";
cin>>name;
cout <<"\nEnt eracno: "
;
cin>>acno;
cout <<"\nEnt erbalance:";
cin>>bal ;
}
friendv oidshowdet ail
s(f
rndf
nexp)
;
};
voidshowdet ail
s(fr
ndf nexpf)
{
cout <<f .
name<<endl ;
cout <<f .
acno<<endl ;
cout <<f .
bal<<endl;
}
main()
{
frndfnexpfe;
fe.r
eaddetail
s()
;
showdet ai
ls(
fe);
getch();
}
Output:

b)Wr
it
eapr
ogr
am t
oil
l
ust
rat
ethi
spoi
nter

#include<i ostr
eam>
#include<coni o.h>
usingnamespacest d;
classname
{
charst r
[20];
intage;
publ i
c:voidi nput()
{
cout <<"\nEntername: "
;
cin>>str;
cout <<"\nEnterage: ";
cin>>age;
}
publ i
c:voidshow( )
{
cout <<"\nElderper son";
cout <<"\nNamei s"<<str
;
cout <<"\nNamei s"<<age;
}
namedi splay(namex)
{
cout <<"\nCont entsofobject
sn1(
thi
spoi
nter
)";
cout <<"\nname: "<<this->str;
cout <<"\nage:"<<this->age;
cout <<"\nCont entsofobj ects(
n2)";
cout <<"\nname: "<<x.str;
cout <<"\nage:"<<x.age;
i
f(this->age>x.age)
{
ret urn* t
his;
}
else
returnx;
}
}
;
v
oi dmai n()
{
namen, n1,n2;
cout <<"\nEnt erfir
stper sondet ail
s";
n1. i
nput ();
cout <<"\nEnt ersecondper sondetail
s";
n2. i
nput ();
n=n1. display(n2);
n.show( );
get ch();
}

Out
put
:
c)Wr iteaPr ogram t oil
lustr
atepoint
ertoacl
ass
#include<iostream>
#include<conio. h>
usingnamespacest d;
classempl oy
{
charname[ 20] ;
intage;
floatsal;
publ i
c:voidget data()
{
cout <<"
\nEnterempl oyname, age,
sal
:"
;
cin>>name>>age>>sal ;
}
publ i
c:voiddispl ay(
)
{
cout <<"
Empl oyname: "<<name<<endl;
cout <<"
Age: "
<<age<<endl ;
cout <<"
sal:
"<<sal <<endl;
}
};
mai n()
{
empl oyobj;
empl oy*ptr;
pt r
=&obj ;
pt r
->getdata();
pt r
->displ
ay (
);
get ch();
}

Out
put
:
Exerci
se-
8( OperatorOver
loadi
ng)
a).Wri
teapr ogram toOverl
oadUnary,andBi
nar
yOper
ator
sasMemberFunct
ion,and
NonMemberFunct ion.
i
.Unaryoperatorasmemberf unct
ion
i
i.Bi
naryoperatorasnonmemberfunction

Program:
#include<i ostream. h>
#include<coni o.h>
classnum
{
inta,b, c,d;
publ i
c: voidi nput ()
{
cout <<" \nEnt era,b,c,
dvalues"
;
cin>>a>>b>>c>>d;
}
publ i
c: voidshow( )
{
cout <<" Thev aluesare:"
<<a<<endl
<<b<<endl
<<c<<endl
<<d;
}
publ i
c: voidoper ator++()
{
++a;
++b;
++c;
++d;
}
num oper ator+( num);
};
num num: :oper ator+(num T)
{
num t emp;
temp. a=a+T. a;
temp. b=b+T. b;
temp. c=c+T. c;
temp. d=d+T. d;
returnt emp;
}
mai n()
{
num x, y,z,w;
x.input ();
y.input ();
z=x+y ;
z.show( )
;
w.i
nput()
;
++w;
w.show();
getch(
);
}

Out
put
:

b)
.Wr
it
eac++pr
ogr
am t
oimpl
ementt
heov
erl
oadi
ngassi
gnment=oper
ator
#incl
ude<iostream>
#incl
ude<coni o.
h>
usingnamespacest d;
cl
assdemo
{
inta,b;
public:
demo( intx,
inty);
public:
voi
ddi splay()
;
public:
voi
doper at
or=(demoob2)
;
};
demo: :
demo( i
ntx,i
nty )
{
a=x;
b=y ;
}
voiddemo: :
display
()
{
cout<<a<<endl<<b;
}
voi
ddemo: :
operator
=(demoob2)
{
a=ob2. a;
b=ob2. b;
}
main()
{
demoob1( 2,
3),ob2(
4,5);
ob1.operator=(
ob2);
ob1.displ
ay();
getch();
}

Out
put
:

c)
.Wr
it
eacasest
udyonOv
erl
oadi
ngOper
ator
sandOv
erl
oadi
ngFunct
ions(
150Wor
ds)

Operatoroverl
oadi
ngi sani mpor
tantconcepti
nC++.I tisat ypeofpolymorphism in
whichanoper at
orisoverl
oadedtogiveuserdef
inedmeaningtoi t
.Over
loadedoper at
or
i
susedt oper
form operat
iononuser-
defi
neddatatype.Forexample'
+'operatorcanbe
overl
oaded t o per f
orm addi t
ion on v ari
ous dat a types, l
ike f or Integer,
St
ring(concat
enati
on)etc.

Oper
atorov
erl
oadi
ngcanbedonebyi
mpl
ement
ingaf
unct
ionwhi
chcanbe:

1.MemberFunct
ion
2.Non-
MemberFunct
ion
3.Fr
iendFunct
ion

Oper
atorov
erl
oadi
ngf
unct
ioncanbeamemberf
unct
ioni
ftheLef
toper
andi
sanObj
ect
oft
hatclass,buti
ftheLeftoperandi
sdi
ff
erent
,thenOper
atorov
erl
oadi
ngf
unct
ion
mustbeanon-memberfunct
ion.

Operatorov
erloadi
ngfunct
ioncanbemadef
ri
endf
unct
ioni
fitneedsaccesst
othe
pr
ivateandprotect
edmembersofcl
ass.

Exer
cise-
9(I
nher
it
ance)
a)Wr
it
eC++Pr
ogr
amsandi
ncor
por
ati
ngv
ari
ousf
ormsofI
nher
it
ance
i
)Si
ngl
eInher
it
ance
i
i
)Hi
erar
chi
cal
Inher
it
ance
i
i
i)Mul
ti
pleI
nher
it
ances
i
v)Mul
ti
-l
evel
inher
it
ance
v
)Hy
bri
dinher
it
ance
SI
NGLEI
NHERI
TANCE

#i
ncl
ude<i
ost
ream.
h>

#i
ncl
ude<const
ream.
h>

cl
assABC

pr
otect
ed:

charname[
15]
;

i
ntage;

}
;

cl
assabc:
publ
i
cABC/
/publ
i
cder
ivat
ion

f
loathei
ght
;

f
loatwei
ght
;

publ
i
c:
v
oidget
dat
a()

cout
<<“
\nEnt
erNameandAge:
”;

ci
n>>name>>age;

cout
<<“
\nEnt
erHei
ghtandWei
ght
:”
;

ci
n>>hei
ght>>wei
ght
;

v
oidshow(
)

cout
<<“
\nName:
”<<name<<“
\nAge:
”<<age<<“Year
s”;

cout
<<“
\nHei
ght
:”
<<hei
ght<<“Feet
s”<<“
\nWei
ght
:”
<<wei
ght<<“
Kg.
”;

}
;

i
ntmai
n()

cl
rscr
();

abcx;

x.
get
dat
a()
;//Readsdat
athr
oughkey
boar
d.

x.
show(
);/
/Di
spl
aysdat
aont
hescr
een.

r
etur
n0;

}
OUTPUT

Ent
erNameandAge:
Sant
osh24

Ent
erHei
ghtandWei
ght:
4.550

Name:
Sant
osh

Age:
24Year
s

Hei
ght:
4.5Feet
s

Wei
ght:
50Kg.

MULTI
PLEI
NHERI
TANCE

#include<i
ostr
eam.h>
#include<coni
o.h>
classA
{
protected:
inta;
};
classB
{
protected:
intb;
}
;

classc
{
protect
ed:
intc;
}
;
classD
{
protect
ed:
intd;
}
;

cl
assE:
publ
i
cA,
B,C,
D
{
i
nte;

publ
ic:
voi
dget
data()
{
cout
<<“\nEnterval
uesfora,
b,
c,
d,
e“;
ci
n>>a>>b>>c>>d>>e;
}

voi
dshow( )
{
cout<<“a=”
<<a<<”
b=“
<<b<<”c=“
<<c<<”
d=”
<<d<<”
e=”
<<e;
}
};
voi
dmai n()
{
cl
rscr(
);
Eobj;

Obj
.getdat
a()
;
Obj
.show();
}

OUTPUT

Ent
erv
aluesf
ora,
b,
c,
d,
e
10
20
30
40
50

a=10b=20c=30d=40e=50

HI
ERARCHI
CALI
NHERI
TANCE
#include<iost
ream.
h>
#include<conio.
h>
classA
{
Protect
ed:
i
nta;
};
cl
assB: publ i
cA
{
Protected:
i
ntb;
voidgetdata()
{
Cout<<”\nent eraandbv alues”;
Cin>>a>>b;
}
Voiddisplay data(
)
{
Cout<<”\nt hev al
uesar
e“ <<a<<b;
}
};
cl
assC: publ i
cA
{
Protected:
i
ntc;
voidgetdata()
{
Cout<<”\nent eraandcv alues”;
Cin>>a>>c;
}
Voiddisplay data(
)
{
Cout<<”\nt hev al
uesar
e“ <<a<<c;
}

};
Main()
{
Bob1;
Ob1.getdat
a();
Ob1.di
splaydat
a()
;
Cob2;
Ob2.getdat
a();
Ob2.di
splaydat
a()
;
Getch()
;
}

MULTI
LEVELI
NHERI
TANCE

#incl
ude<iostr
eam.h>
#incl
ude<conio.h>
classA1
{
protect
ed:
charname[ 20];
i
ntage;
};
classA2: publi
cA1
{
protect
ed:
fl
oatheight;
fl
oatweight ;
};

cl
assA3: publi
cA2
{
protect
ed:
charsex;
public:
voidget
data()
{
cout<<“\nEnternameandage: “;
ci
n>>name>>age;
cout<<“\nEnterhei ghtandweight:“;
ci
n>>height>>weight ;
cout<<“\nEntersex: “
;
ci
n>>sex ;
}
voidshow( )
{
cout<<”\
nname: “<<name<<”age: “
<<age<<”year
s”;
cout<<”\
nheight: “<<height
<<”feetweight:
“<<weight
<<”
kgs”
;
cout<<“
sex: “<<sex;
}
};
voi
dmai n()
{
cl
rscr
();
A3obj ;
Obj.
data()
;
Obj.
show();}

OUTPUT

Ent
ernameandage:Ramya19
Ent
erhei
ghtandwei
ght:
5.342
Ent
ersex:
female

Name:
Ramy aAge:19Hei
ght
:5.
3feet
Wei
ght
:42kgssex:
femal
e

HYBRI
DINHERI
TANCE

#i
ncl
ude<i
ostr
eam.h>
#i
ncl
ude<coni
o.h>

cl
assPl ay
er
{
prot
ected:
charname[20];
chargender;
i
ntage;
};

cl
assPhy si
que:
publ
i
cPl
ayer
{
prot
ected:
fl
oatheight
;
fl
oatweight;
}
;

cl
assLocat
ion
{
pr
otect
ed:
charcity
[15] ;
charpin[7]
;
};
cl
assGame: publicPhysi
que,
locat
ion
{
protect
ed:
chargame[ 20];
public:
voidget
dat a();
{
cout<<“\nEnt erplayername,age&gender:“
;
ci
n>>name>>age>>gender ;
cout<<“\nEnt erheightandweight:“;
ci
n>>height >>wei ght
;
cout<<“\nEnt erlocati
on,pi
ncode,game:“;
ci
n>>city>>pin>>game;
}

voi
dshow()
{
cout
<<“\
nName=“ <<name;
cout
<<“\
nage=“ <<age;
cout
<<“\
ngender=“ <<gender;
cout
<<“\
nheight=“ <<height;
cout
<<“\
nwei ght=“ <<weight
;
cout
<<“\
nci
t y=“ <<cit
y;
cout
<<“\
npincode=“ <<pi
n;
cout
<<“\
ngame=“ <<game;
}

voi
dmai n(
)
{
Gameobj;
Obj.
getdat
a()
;
Obj.
show();
}
OUTPUT
Ent
erpl
ayername,age&gender:r
ani21female
Ent
erhei
ghtandweight:5.
446
Ent
erl
ocati
on,pi
ncode,game: El
uru521102kabaddi

Name=r ani
age=21
gender=femal
e
hei
ght=5.4
weight=46
ci
ty=elur
u
pi
ncode=521102
game=kabaddi

b)Wr
it
eapr
ogr
am t
oshowVi
rt
ual
BaseCl
ass

#i
ncl
ude<i
ost
ream.
h>

#i
ncl
ude<coni
o.h>

cl
assA1

pr
otect
ed:

i
nta1;

}
;

cl
assA2:
publ
i
cvi
rt
ual
A1/
/vi
rt
ual
decl
arat
ion

pr
otect
ed:

i
nta2;

}
;

cl
assA3:
publ
i
cvi
rt
ual
A1/
/vi
rt
ual
decl
arat
ion

{
pr
otect
ed:

i
nta3;

}
;

cl
assA4:
publ
i
cA2,
A3/
/vi
rt
ual
decl
arat
ion

i
nta4;

publ
i
c:

v
oidget
()

cout
<<“
Ent
erv
aluesf
ora1,
a2,
a3anda4:
”;

ci
n>>a1>>a2>>a3>>a4;

v
oidput
()

cout
<<“
a1=”
<<a1<<“
a2=”
<<a2<<“
a3=”
<<a3<<“
a4=”
<<a4;

}
;

i
ntmai
n()

cl
rscr
();

A4a;

a.
get
();
//Readsdat
a

a.
put
();
//Di
spl
aysdat
a
r
etur
n0;

OUTPUT

Ent
erv
aluesf
ora1,
a2,
a3anda4:
5873

a1=5a2=8a3=7a4=3

c)Wr
it
eacasest
udyonusi
ngv
irt
ual
classes(
150Wor
ds)

Anambi
gui
tycanar
isewhensev
eralpat
hsexi
stt
oacl
assf
rom t
hesamebasecl
ass.
Thi
smeanst
hatachi
l
dcl
asscoul
dhav
edupl
i
cat
eset
sofmember
sinher
it
edf
rom a
si
ngl
ebasecl
ass.
C++sol
vest
hisi
ssuebyi
ntr
oduci
ngav
irt
ualbasecl
ass.Whenacl
assi
smadev
irt
ual
,
necessar
ycar
eist
akensot
hatt
hedupl
i
cat
ioni
sav
oidedr
egar
dlessoft
henumberof
pat
hst
hatexi
stt
othechi
l
dcl
ass.
Whent
woormor
eobj
ect
sar
eder
ivedf
rom acommonbasecl
ass,wecanpr
event
mul
ti
plecopi
esoft
hebasecl
assbei
ngpr
esenti
nanobj
ectder
ivedf
rom t
hoseobj
ect
s
bydecl
ari
ngt
hebasecl
assasv
irt
ualwheni
tisbei
ngi
nher
it
ed.Suchabasecl
assi
s
knownasv
irt
ualbasecl
ass.Thi
scanbeachi
evedbypr
ecedi
ngt
hebasecl
ass’name
wi
tht
hewor
dvi
rt
ual
.

Exer
cise-
10(
Inher
it
ance–Cont
inued)
a)Wr i
teaPr ogram i
nC++t
oil
l
ust
rat
etheor
derofexecut
ionofconst
ruct
orsand
dest
ructor
sininher
it
ance

#incl
ude<iost
ream.h>
#incl
ude<conio.
h>
cl
assbase
{
publi
c:
base()
{
cout<<"
\nINSIDEBASECONSTRUCTOR"
;
}
~base()
{
cout
<<"
\nI
NSI
DEBASECONSTRUCTOR"
;
}
};
cl
assder i
ved:
publ
icbase
{
publi
c:
deri
ved()
{
cout<<"
\nINSI
DEDERIVEDCONSTRUCTOR"
;
}
~derived()
{
cout<<"
\nINSI
DEDERIVEDCONSTRUCTOR"
;
}
};
main()
{
der i
vedx;
getch()
;
}
Output:

b)Wri
teaPr ogr
am toshowhowconst
ruct
orsar
einv nder
okedi ivedcl
ass
#i
ncl
ude<iostr
eam.h>
#i
ncl
ude<conio.h>
cl
assalpha
{
public:
i
nta;
alpha(intx)
{
a=x;
}
voiddi splay()
{
cout <<" \
nthev alueofai
s"<<a;
}
};
cl
assbet a:
publi
cal pha
{
public:
i
ntb;
beta(intx, i
nty):
alpha(x)
{
b=y ;
}
voidpr int()
{
cout <<" \
nthev alueofbi
s"<<b;
}
};
mai n()
{
bet ap( 90,99);
p.displ ay();
p.pr i
nt (
);
getch( );
}

Out
put
:
Exer
cise-
11(
Pol
ymor
phi
sm)
a)Wr
it
eapr
ogr
am t
oil
l
ust
rat
erunt
imepol
ymor
phi
sm

#incl
ude<iostr
eam>
#incl
ude<conio.h>
usingnamespacest d;
cl
assbase
{
publi
c:
i
nta;
publi
c:

v
irt
ualv
oidshow(
)
{

cout
<<"
\nBASECLASS"
;
}
};
cl
assder i
ved:
publ
i
cbase
{
publ
ic:
vi
rtualvoi
dshow()
{
cout
<<"
\nDERI
VEDCLASSS"
;
}
}
;
mai
n()
{

deriv
edd,*
p;
p=&d;
p->show(
);
/
/pt
->dis()
;

get
ch(
);
}

Out
put
:

b)Wr i
teaprogr
am i
ll
ustr
atespur
evi
rt
ualf
unct
ionandcal
cul
atet
hear
eaofdi
ff
erent
shapesbyusi
ngabst
ractcl
ass.

#incl
ude<i
ostr
eam>
#incl
ude<coni
o.h>
usingnamespacestd;

cl
assShape /
/Basecl
ass
{

pr
otected:
i
ntwidth;
i
ntheight
;
i
ntside;

publ
i
c:
/
/purev i
rtualfuncti
onpr
ovi
dingi
nter
facef
ramewor
k.
vi
rt
uali
ntgetArea()=0;
voi
dinput(i
ntw,inth)
{
width=w;
height=h;
}
voi
dinputt
(i
nts)
{
si
de=s;
}
};

//Der i
vedclasses
classRectangle:publ
icShape
{
public:
int
getArea()
{
i
ntr e;
re=widt
h*height;
cout<<"Ar eaofRectangl
e: "<<re<<endl
;
}
};

cl
assSquar e:publi
cShape
{
publ
ic:
intgetArea()
{
i
ntst;
st=si
de*
side;
cout
<<" AreaofSquareis:"<<st
<<endl
;

}
}
;

i
ntmai
n(v
oid)
{
i
ntx,y;
Rectangl
erect
;
Squaresq;

cout<<"\nEnterwi
dth&hei
ghtf
orar
ect
angl
e:"
;
cin>>x>>y;
rect.
input
(x,y
);
rect.
getArea()
;

cout<<"\
nEntert
hesi
def
orasquar
e:"
;
ci
n>>x;
sq.
inputt
(x)
;
sq.
get
Area();

get
ch(
);
r
etur
n0;
}

Out
put
:
c)Wr
it
eacasest
udyonv
irt
ual
funct
ions(
150Wor
ds)
Vi
rt
ualFunct
ioni
saf
unct
ioni
nbasecl
ass,whi
chi
sov
err
idedi
ntheder
ivedcl
ass,and
whi
cht
ell
sthecompi
l
ert
oper
for
m Lat
eBi
ndi
ng 
ont
hisf
unct
ion.

Vi
rt
ual
 
Key
wor
disusedt
omakeamemberf
unct
ionoft
hebasecl
assVi
rt
ual
.InLat
e
Bi
ndi
ngf
unct
ioncal
li
sresol
vedatr
unt
ime.Hence,
nowcompi
l
erdet
ermi
nest
het
ypeof
obj
ect at r
unt
ime, and t
hen bi
nds t
he f
unct
ion cal
l
. Lat
e Bi
ndi
ng i
s al
so
cal
l
ed 
Dynami
c Bi
ndi
ngor
 Runt
ime 
Bindi
ng.

Exer
cise-
12(
Templ
ates)
a)Wr
it
eaC++Pr
ogr
am t
oil
l
ust
rat
etempl
atecl
ass

#i
ncl
ude<i
ost
ream>
#i
ncl
ude<coni
o.h>
usi
ngnamespacest
d;
t
empl
ate<cl
assS>
cl
asssqr
{
publ
i
c:
sqr
(Sc)
{
cout
<<"
\nC="
<<c*
c;
}
}
;
mai
n()
{
sqr
<int
>h(
2);
sqr
<fl
oat
>i(
2.5)
;

get
ch(
);
}
Out
put
:

b)Wr
it
eaPr
ogr
am t
oil
l
ust
rat
ecl
asst
empl
ateswi
thmul
ti
plepar
amet
ers

#include<iostream>
#include<conio.h>
usingnamespacest d;
templ ate<cl
assT1, classT2>
classdat a
{
publi
c:
data(T1a, T2b)
{
cout <<"\na="
<<a<<"
b="
<<b;
}
};
mai n()
{
data<i
nt ,
fl
oat>h( 2,2.5)
;
data<i
nt ,
char>i(2,'
C');
data<i
nt ,
fl
oat>j(2.5,44);
getch(
);
}
Out
put
:

c)Wr
it
eaPr
ogr
am t
oil
l
ust
rat
ememberf
unct
iont
empl
ates

#include<iostream>
#include<conio. h>
usingnamespacest d;
templ ate<cl
assT>
classdat a
{
publi
c:
dat a(
Tc) ;
};
templ ate<cl
assT>
data<T>: :
data(Tc)
{
cout<<"\nC=" <<c;
}
mai n()
{
data<i
nt >h(2);
data<fl
oat >i(
2.5);
data<char >k('
c'
);

get
ch(
);
}
Out
put
:

Exer
cise-
13(
Except
ionHandl
i
ng)

a)
.Wr
it
eaPr
ogr
am f
orExcept
ionHandl
i
ngDi
vi
debyzer
o

#include<i ostr
eam. h>
#include<coni o.h>
voidmai n( )
{
 
 inta,b,
c;
 
 float d;
 
 clrscr();
 
 cout <<"Ent erthevalueofa:"
;
 
 cin>>a;
 
 cout <<"Ent erthevalueofb:"
;
 
 cin>>b;
 
 cout <<"Ent erthevalueofc:"
;
 
 cin>>c;
 
  
 
 try
 
 {
 
        i
f (
(a-b)!
=0)
 
        {
 
        d=c/ (a-b);
 
        cout <<"Resulti
s:"
<<d;
 
        }
 
        else
 
        {
 
        t hrow(a-b);
 
        }
 
 }
 
 
 cat ch(inti)
 
 {
 
       
cout<<"EXCEPTION::
DIVI
SIONBYZERO
Answerisinfi
nitebecausea-
bis:
"<<i
;
 
 }
 
 
 
 getch()
;
}
Output:

b)
.Wr
it
eaPr
ogr
am t
oret
hrowanExcept
ion

#incl
ude<i
ostr
eam>
#incl
ude<coni
o.h>
usingnamespacestd;

v
oidsub(i
nt j
,
intk)
{
cout<<"\nINSIDEFUNCTI ONSUB()\ n"
;
tr
y
{
if
(j==0)
throwj;
else
cout<<"
\ nsubtr
acti
on="<<j
-k<<"
\n"
;
}
catch(i
nt)
{
cout<<"
\ ncaughtnul
lval
ues\ n"
;
throw;
}
cout
<<"
\nENDOFSUB(
)**
*";
}
mai
n()
{
cout<<"\ninsidefunct
ionmain\n";
try
{
sub(8,
5) ;
sub(0,8);
}
catch(i
nt)
{
cout<<"\ncaughtnulli
nsi
demain()"
;
}
cout<<"caughtendoff unct
ionmain()";
getch()
;

}
Out
put
:
Exer
cise-
14(
STL)
a)Wr i
teaPr ogr am t oi mpl ementLi standLi stOper ati
ons
#include<i ost ream>
#include<l i
st >
#include<st ring>
#include<coni o. h>
usingnamespacest d;
i
ntmai n()
{
intmy ints[ ]={ 5, 6, 3, 2,7};
li
st<int>l ,l1( my int s,my i
nts+si zeof (my ints)/sizeof(
int
));
li
st<int>::i
t er atori t;
intchoi ce, item;
whi l
e( 1)
{
cout <<" \n- ----
--
-------
---
----
"<<endl ;
cout <<" Li stI mpl ement ationi nSt l
" <<endl ;
cout <<" \n- ----
--
-------
---
----
"<<endl ;
cout <<" 1. Inser tEl ementatt heFr ont "<<endl ;
cout <<" 2. Inser tEl ementatt heEnd" <<endl ;
cout <<" 3. Del eteEl ementatt heFr ont "<<endl;
cout <<" 4. Del eteEl ementatt heEnd" <<endl ;
cout <<" 5. Fr ontEl ementofLi st"<<endl ;
cout <<" 6. LastEl ementoft heLi st" <<endl ;
cout <<" 7. Sizeoft heLi st"<<endl ;
cout <<" 8. Resi zeLi st"<<endl ;
cout <<" 9. Remov eEl ement swi thSpeci ficValues"
<<endl;
cout <<" 10. Remov eDupl icat eVal ues" <<endl ;
cout <<" 11. Rev er set heor derofel ement s"<<endl;
cout <<" 12. SortFor war dLi st"<<endl ;
cout <<" 13. Mer geSor tedLi sts"<<endl ;
cout <<" 14. Displ ayFor war dLi st"<<endl ;
cout <<" 15. Exit"<<endl ;
cout <<" Ent ery ourChoi ce: ";
cin>>choi ce;
swi tch( choi ce)
{
case1:
cout <<" Ent erv aluet obei nser tedatt hefront:"
;
ci
n>>i tem;
l
.push_ front( i
t em) ;
break;
case2:
cout <<" Ent erv aluet obei nser tedatt heend: "
;
ci
n>>i tem;
l
.push_ back( it
em) ;
break;
case3:
i
tem =l .
front();
l
.pop_ front();
cout <<"Element" <<it
em<<"del eted"<<endl
;
break;
case4:
i
tem =l .
back( );
l
.pop_ back( );
cout <<"Element" <<it
em<<"del eted"<<endl
;
break;
case5:
cout <<"FrontEl ementoft heList:";
cout <<l.f
ront ()<<endl ;
break;
case6:
cout<<" LastEl ementoft heLi st:
";
cout<<l .
back( )<<endl ;
break;
case7:
cout<<" Sizeoft heLi st:"<<l.si
ze()<<endl;
break;
case8:
cout<<" Enternewsi zeoft heList: "
;
ci
n>>item;
i
f( i
tem <=l .
size( ))
l.
resize( i
tem) ;
else
l.
resize( i
tem, 0);
break;
case9:
cout<<" Enterel ementt obedel eted: "
;
ci
n>>item;
l
.remov e(it
em) ;
break;
case10:
l
.unique( );
cout<<" Duplicat eI t
emsDel eted"
<<endl ;
break;
case11:
l
.reverse() ;
cout<<" Li
str ever sed" <<endl ;
break;
case12:
l
.sort(
) ;
cout<<"Li
stSorted"<<endl;
break;
case13:
l
1.sort()
;
l
.sort()
;
l
.mer ge(l
1);
cout<<"Li
stsMer gedaf t
ersorting"
<<endl
;
break;
case14:
cout<<"El
ement soft heList
:";
for(i
t=l .
begin(
);it!
=l .
end();
it++)
cout<<*i
t<<"";
cout<<endl;
break;
case15:
exit
(1);
break;
default
:
cout<<"WrongChoi ce"<<endl
;
}
}
getch(
);
ret
urn0;
}
b)Wr
it
eaPr
ogr
am t
oimpl
ementVect
orandVect
orOper
ati
ons
#i
ncl
ude<i
ostr
eam>
#i
ncl
ude<coni
o.h>
#incl
ude<v
ector>
usingnamespacest
d;

mai
n()
{
vect
or<i
nt>e;

for(
intx=0;x<3;
x++)
e.push_back(x+1)
;

cout<<"
\nt
heelement
sar
e"<<"
\n"
;
for
(i
ntx=0;
x<3;
x++)

cout
<<"
\n"
<<e[
x];

v
ector<i
nt>:
:i
ter
atori
t=e.
begi
n()
;
i
t=i
t+1;

e.
inser
t(i
t,
999)
;

cout<<"\
ntheelement
saf
teri
nser
ti
onsar
e\n"
;
for
(i
ntx=0;x<4;
x++)
cout<<endl
<<e[
x];

vector<i
nt>:
:i
terat
ori
i
t=e.
begi
n()
;
e.er
ase(i
it
+1,
ii
t+3);

i
nts=e.
size(
);

cout<<"\
ntheelement
saf
terer
ase"
;
for
(i
ntx=0;x<s;
x++)
cout<<endl
<<e[
x];

get
ch(
);

}
Out
put
:

Exer
cise-
15(
STLCont
inued)
a)Wr
it
eaPr
ogr
am t
oimpl
ementDequeandDequeOper
ati
ons
#include<i ostream>
#include<deque>
#include<st ring>
#include<cst dl i
b>
usingnamespacest d;
i
ntmai n()
{
deque<i nt>dq;
deque<i nt>::it
erat orit;
intchoi ce,i
t em;
whi l
e( 1)
{
cout <<"\n-----
--
----
---
---
--
--
"<<endl;
cout <<"DequeI mpl ement at
ioni nSt l
"<<endl;
cout <<"\n-----
--
----
---
---
--
--
"<<endl;
cout <<"1.InsertEl ementatt heEnd" <<endl ;
cout <<"2.InsertEl ementatt heFr ont"<<endl;
cout<<" 3.Delet eElementatt heEnd" <<endl
;
cout <<"4.Del eteEl ementatt heFr ont"<<endl;
cout <<"5.FrontEl ementatDeque" <<endl ;
cout <<"6.LastEl ementatDeque" <<endl ;
cout <<"7.Sizeoft heDeque" <<endl ;
cout <<"8.Di splayDeque" <<endl;
cout <<"9.Exi t
"<<endl ;
cout <<"Ent ery ourChoi ce: "
;
ci
n>>choi ce;
switch(choice)
{
case1:
cout<<"Ent ervaluet obei nsert
edatt heend: "
;
cin>>it
em;
dq.push_ back(item) ;
break;
case2:
cout<<"Ent ervaluet obei nsert
edatt hefront:"
;
cin>>it
em;
dq.push_ front(i
tem) ;
break;
case3:
i
tem =dq. back( );
dq.pop_ back( )
;
cout<<" Element" <<it
em<<"del eted"<<endl;
break;
case4:
i
tem =dq. front();
dq.pop_ fr
ont ()
;
cout<<"Element" <<it
em<<"del eted"<<endl;
break;
case5:
cout<<"FrontEl ementoft heDeque: ";
cout<<dq. front()<<endl ;
break;
case6:
cout<<"BackEl ementoft heDeque: ";
cout<<dq. back( )<<endl;
break;
case7:
cout<<"Sizeoft heDeque: "
<<dq.si
ze()<<endl;
break;
case8:
cout<<"Element sofDeque:" ;
for(i
t=dq. begin( );i
t!=dq. end()
;it
++)
cout<<* it
<<"" ;
cout<<endl ;
break;
case9:
exit(
1);
break;
default:
cout<<"Wr ongChoi ce"<<endl;
}
}
r
etur
n0;
}

Out
put
:
b)Wr
it
eaPr
ogr
am t
oimpl
ementMapandMapOper
ati
ons

#incl
ude<i
ostr
eam>
#incl
ude<coni
o.h>
#incl
ude<map>
usingnamespacestd;

t
ypedefmap<st
ri
ng,
i
nt>i
tem_
map;

mai
n()
{
intsz;
st r
ingit
em_name;
intcodeno;
item_ mapi
tem;

cout<<"
\nENTERITEM NAMEANDCODENOFOR2I
TEMS"
;
for
(i
nti=0;
i
<2;
i++)
{
ci
n>>i
tem_name;
ci
n>>codeno;
i
tem[i
tem_name]
=codeno;
}

cout
<<"
\nl
istofit
em nameandcodeno\
n";
i
tem_map:
:
iter
atort;

for
(t=i
tem.begi
n()
;t
!=i
tem.end()
;t
++)
cout
<<(
*t).
fi
rst
<<""<<(
*t).
second<<endl
;

i
tem.inser
t(pai
r<st
ri
ng,
i
nt>(
"pr
int
er"
,8888)
);
sz=i
tem.si
ze( )
;

cout
<<"
\nsi
zeofmap"
<<sz;

i
tem.cl
ear(
);
sz=i
tem.si
ze(
);

cout
<<"
\naf
tercl
earsi
zeofmap"
<<sz;

get
ch(
);
}

Out
put
:

You might also like